Jump to content

MySQL Triggers


[Coyot3]

Recommended Posts

Hi all,everytime I need to create a trigger I get in troubles.This time I have the code above

CREATE TRIGGER generatePin AFTER INSERT ON sbauctions_members		FOR EACH ROW BEGIN			SET @pin = SHA(NOW());			SET @pinLength = 8;			SET @start = FLOOR(1+RAND()*((LENGTH(@pin)-@pinLength)-1));			SET @statusName;			/* get name of status where bids are forbidden and expose allowed */			SELECT @statusName:=name FROM sbauctions_status WHERE bid=1 AND expose=0;			/* insert activation member status */			INSERT INTO sbauctions_members_status (memberID,statusID,pin) VALUES (LAST_INSERT_ID(),@statusName,@pin);		END;

that when executed returns syntax error at line 3.I'm using MySQL 5.0.41. Can you find any problem?Thanks in advance.

Link to comment
Share on other sites

Finally I found the problem.For those who have the same problem I recommend to read Section D.1, “Restrictions on Stored Routines, Triggers, and Events” at MySQL website.Triggers and other Stored Routines are under some restrictions. In this case the problem are the lines where i perform SELECT operations because they return a result set.These operations must be done using user defined variables with SELECT ... INTO ... instruction and the final trigger will look like

CREATE TRIGGER generatePin AFTER INSERT ON sbauctions_members		FOR EACH ROW BEGIN			SET @pin = SHA(NOW());			SET @pinLength = 8;			SET @start = FLOOR(1+RAND()*((LENGTH(@pin)-@pinLength)-1));			SET @statusName;			/* get name of status where bids are forbidden and expose allowed */			SELECT name INTO @statusName FROM sbauctions_status WHERE bid=1 AND expose=0;			/* insert activation member status */			INSERT INTO sbauctions_members_status (memberID,statusID,pin) VALUES (NEW.id,@statusName,@pin);		END;

POST FULL EDITED :)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...