Jump to content
Sign in to follow this  
[Coyot3]

MySQL Triggers

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.

Share this post


Link to post
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 :)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...