Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

junaooaks

triggers update

Recommended Posts

ola pessoal!

 

eu nunca fiz esta tal de triggers, ja post pesquisa no google, mas não consegui fazer uma update entre duas tabelas

 

tabela 01

CREATE TABLE IF NOT EXISTS `retorno` (

`idRetrono` int(11) NOT NULL AUTO_INCREMENT,

`retorNum` int(11) NOT NULL,

`retorDataPaga` date NOT NULL,

`dataProc` date NOT NULL,

`retorValorPago` float(18,2) NOT NULL,

PRIMARY KEY (`idRetrono`)

) ENGINE=InnoDB;

 

tabela 02

CREATE TABLE IF NOT EXISTS `cobranca` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `id_cliente` int(11) NOT NULL DEFAULT '0',
 `dataproc` date NOT NULL DEFAULT '0000-00-00',
 `dataUpdate` date NOT NULL,
 `venc` date NOT NULL DEFAULT '0000-00-00',
 `valor` varchar(10) NOT NULL DEFAULT '',
 `nosnum` varchar(17) NOT NULL DEFAULT '',
 `status` int(11) NOT NULL DEFAULT '0',
 `numdoc` varchar(17) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM;

 

preciso de uma triggers quando o valor da tabela 01

retorno

campo

retorNum

for igual a tabela02

cobranca

campo

nosnum

so que tem um porem na tabela 01 este campo retorna com numero zero na frente ficando assim 00000001 00000002 00000003.... o tabela 02 o numero nao tem zero preenchendo espaço

 

nao sei se tem como fazer esta comparação usando triggers tirar o que for 0 a esquerda e comparar o numero e alterar o campo da tabela 02 "status" para 3

 

 

tipo assim

 

if (tabela01-campo-retorNum = tabela02-campo-nosnum ){update na tabela 02 campo "status" para 03;}

 

preciso de ajuda instruções se realmente e possível fazer esta comparação no MYSQL obrigado..

Compartilhar este post


Link para o post
Compartilhar em outros sites

Isso ?

mysql> create table tbl1 ( id integer unsigned zerofill not null auto_increment primary key ) ;
Query OK, 0 rows affected (0.07 sec)

mysql> create table tbl2 ( id integer not null , status tinyint ( 2 ) ) ;
Query OK, 0 rows affected (0.05 sec)

mysql> delimiter $$
mysql> create trigger updateStatus after update on tbl1
   -> for each row begin
   ->  update tbl2 set `status` = 3 where tbl2.id = NEW.id;
   -> end$$
Query OK, 0 rows affected (0.17 sec)

mysql> delimiter ;
mysql> insert into tbl1 values ( 1 ) , ( 2 ) , ( 3 );
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from tbl1;
+------------+
| id         |
+------------+
| 0000000001 |
| 0000000002 |
| 0000000003 |
+------------+
3 rows in set (0.00 sec)

mysql> insert into tbl2 values ( 1 , '5' ) , ( 2 , '5' ) ;
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> update tbl1 set id = 1 where id = 1;
Query OK, 0 rows affected (0.01 sec)
Rows matched: 1  Changed: 0  Warnings: 0

mysql> select * from tbl2;
+----+--------+
| id | status |
+----+--------+
|  1 |      3 |
|  2 |      5 |
+----+--------+
2 rows in set (0.00 sec)

mysql>

 

É somente a ideia, depois você altera para os seus critérios ..

create trigger updateStatus after update on tbl1 
for each row begin
if NEW.[campo] = ... then
	update tbl2 set `status` = 3 where tbl2.id = NEW.id;
end if ;
end

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.