Mysql trigger to insert the desired id

Asked

Viewed 47 times

0

I have two mysql and sqlserver databases, I share the same data with each other, but in order not to have id conflict numerei each mysql database = 2, sqlserver = 1, I would like the table id to be entered by mysql, database number + request id. For example:

banco mysql -> 2
id cidade auto_incremente ->38
salvo na tabela cidade primary key id = 238

So I made this code

DELIMITER $

CREATE TRIGGER Tgr_Cidade_Insert AFTER INSERT
ON cidade
FOR EACH ROW
BEGIN

declare @idString varchar(10);
declare @idInt int;
declare @idLast int;
declare @nomeCidade varchar(50);
declare @totalCaracteres int;

set @idInt = NEW.idCidade
set @idInt = @idInt - 1
set @idString = CONVERT(@idInt, char)
set @totalCaracteres =  LENGTH(@idString)
set @idString = SUBSTRING(@idString, 2, @totalCaracteres - 1)
set @idInt = CONVERT(@idString, int)
set @idInt = @idInt + 1
set @idString = '1'
set @idString = @idString + CONVERT(@idInt, varchar(10))
set @idInt = CONVERT(@idString, int)

set @idLast = NEW.idCidade
set @nomeCidade = NEW.nomeCidade

insert cidade(id_cidade, nome_cidade) values(@idInt, @nomeCidade)
delete from cidade where id_cidade = @idLast

END$

DELIMITER ;

So I’m trying to create a Trigger, already created in sql server now missing Mysql, is giving following error:

Mensagens do MySQL : Documentação

#1064 - Você tem um erro de sintaxe no seu SQL próximo a '@idString varchar(10);
declare @idInt int;
declare @idLast int;
declare @nome' na linha 6

1 answer

0

I solved the problem using: auto_increment_increment e auto_increment_offset. thus:

auto_increment_increment = 10
auto_increment_offset = 2

So the id will be filled in this way:

id
2
12
22
32
...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.