how to declare variable in trigger and use it with mysql?
-
what is the error ? DELIMITER $$ CREATE TRIGGER `Task_insert_trig` AFTER INSERT ON `task` FOR EACH ROW begin declare userID int; Set userID =(select userID from assigned_task where Atk_Task_Id = new.Tsk_Id and Atk_Project_Id = new.Tsk_Project_Id); insert into dashboard_event set Dsh_Project_Id = new.Tsk_Project_Id, Dsh_Actor = userID, Dsh_Action = 'Assign', Dsh_Type = 'Task', Dsh_Target = new.Tsk_Id, Dsh_Date = now(); $$ end DELIMITER ; Error Code : 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 12 Error Code : 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'end DELIMITER' at line 1
-
Answer:
I believe the error was related to the $$ delimiter, $$ end delimiter; is not correct. Also, I wouldn't use variable names that might be confused to a table column (such as userID in assigned_task table. Also the insert syntax is broken. (UPDATE: actually the insert was just fine, I didn't know you could do it that way). Try DELIMITER $$ CREATE TRIGGER Task_insert_trig AFTER INSERT ON task FOR EACH ROW begin Set @userID =(select userID from assigned_task where Atk_Task_Id = new.Tsk_Id and Atk_Project_Id = new.Tsk_Project_Id limit 1); insert into dashboard_event (Dsh_Project_Id , Dsh_Actor , Dsh_Action , Dsh_Type , Dsh_Target , Dsh_Date ) values (new.Tsk_Project_Id, @userID, 'Assign', 'Task', new.Tsk_Id, now()); end $$ DELIMITER ;
mohamed at Stack Overflow Visit the source
Other answers
Here's all my findings on the subject: This is a quote from a manual: "You need a BEGIN/END block when you have more than one statement in the procedure. You use the block to enclose multiple statements. But that's not all. The BEGIN/END block, also called a compound statement, is the place where you can define variables and flow of control." In other words: (These rules appear to apply to triggers and stored procedures in the same way, as it seems the same syntax is used in both.) First, notice that a flow control group of keywords such as IF ... END IF or WHILE ... END WHILE is seen as a single statement as far as its termination with a semicolon is concerned, that is, it is terminated as a whole by a single semicolon at the end of it: IF ... END IF; WHILE ... END WHILE;. Then, if the body of a trigger or stored procedure contains just one stament, and that statement is not a variable declaration nor a flow control group of keywords as above, that statement may not be terminated by a semicolon (;) and not enclosed by a BEGIN ... END block. On the contrary, if the body of a trigger or stored procedure contains more than one stament, and particularly if it contains variable declarations and/or flow control groups of keywords, then it must be enclosed in a BEGIN ... END block. Finally, the BEGIN ... END block itself must not be terminated by a semicolon. Hope this helps
LucaE
Related Q & A:
- How to create a Restful web service in .Net Using MySQL?Best solution by stackoverflow.com
- How to select all articles and their similar articles from MySQL?Best solution by Stack Overflow
- How to create variable from value in variable?Best solution by Stack Overflow
- How to input variable inside regex javascript?Best solution by stackoverflow.com
- How can it be more beneficial to use drugs then harmful?Best solution by Ask.Metafilter.Com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.