MySQL Forums
Forum List  »  Triggers

Re: Trigger reference table error
Posted by: Roland Bouman
Date: September 14, 2005 04:20PM

It works for me. To be sure, this is what i did:

use test;

create table t1 (name varchar(10), type int);

insert into t1(name)values ('a');

create table t2(type int, name varchar(10));

insert into t2 values (1,'a'),(2,'b');

delimiter $$

create trigger burt1 before update on t1 for each row begin
declare v_type int;
select type into v_type from t2 where name = new.name;
set new.type = v_type;
end;
$$

update t1 set name = 'b' where type is null$$

select * from t1$$
+------+------+
| name | type |
+------+------+
| b | 2 |
+------+------+

The trigger even fires when no colum needs to be updated:

update t2 set type = 3 where type = 2;

update t1 set name = 'b' where type =2$$

select * from t1$$
+------+------+
| name | type |
+------+------+
| b | 3 |
+------+------+

Options: ReplyQuote


Subject
Views
Written By
Posted
3340
September 13, 2005 01:08PM
Re: Trigger reference table error
2196
September 14, 2005 04:20PM
2045
September 15, 2005 02:19AM
2152
September 15, 2005 02:28AM
2124
September 15, 2005 09:28AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.