| Author |
Topic |
|
amsqlguy
Yak Posting Veteran
89 Posts |
Posted - 2008-09-22 : 13:38:20
|
| I am trying to execute a stored procedure from a trigger, Is this the right way to do it interms of syntax and logiccreate trigger [TRG_EMPLOYEE] ON EMPLOYEE AFTER INSERT, UPDATE ASBEGIN DECLARE @PARTYID BIGINT DECLARE @PARTYTYP INTSELECT @PARTY_ID = PARTY_ID, @PARTYTYP = PARTYTYP FROM INSERTED IF @PARTYTYP = 1 EXEC P_CREATE_PERSON (@PARTYID)ELSE <NOTHING>ENDAny suggestions/inputs would helpThanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-22 : 13:40:17
|
| Whats the purpose of procedure can you explain?Also your code is assuming that INSERTED will be having a single row which is not always the case. |
 |
|
|
jordanam
Yak Posting Veteran
62 Posts |
Posted - 2008-09-22 : 13:42:44
|
| Looks good -- I don't think you need to specify ELSE if nothing else happens. If the IF condition isn't met, the trigger will just end. I'm not sure if there are benefits to either method.http://msdn.microsoft.com/en-us/library/ms189799.aspxRemember that it will run after every action you specify (insert and update in your case), so if this is going to be a highly transactional table, performance could suffer. |
 |
|
|
amsqlguy
Yak Posting Veteran
89 Posts |
Posted - 2008-09-22 : 13:50:38
|
| Is the syntax ok while executing the SP and I will remove else statement Visakh16 - it only inserts one in the table at each time and this is for demo to get around a bug in the functionalityThanks |
 |
|
|
amsqlguy
Yak Posting Veteran
89 Posts |
Posted - 2008-09-22 : 15:03:42
|
| I getting the following error Msg 156, Level 15, State 1, Procedure TRG_EMPLOYEE, Line 10Incorrect syntax near the keyword 'PROCEDURE'.create trigger [TRG_EMPLOYEE] ON EMPLOYEE AFTER INSERT, UPDATE ASBEGINDECLARE @PARTYID BIGINTDECLARE @PARTYTYP INTSELECT @PARTY_ID = PARTY_ID, @PARTYTYP = PARTYTYP FROM INSERTEDIF @PARTYTYP = 1EXEC P_CREATE_PERSON (@PARTYID)END |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-09-22 : 15:16:20
|
| You either have a syntax error in the called procedure (can you execute just the procedure outside the trigger?) or you have not posted all the code. The word "procedure" is not in the code you posted.Be One with the OptimizerTG |
 |
|
|
amsqlguy
Yak Posting Veteran
89 Posts |
Posted - 2008-09-22 : 15:37:23
|
quote: Originally posted by TG You either have a syntax error in the called procedure (can you execute just the procedure outside the trigger?) or you have not posted all the code. The word "procedure" is not in the code you posted.Be One with the OptimizerTG
Sorry I was trying to troubleshoot it different times for different error messagehere is what I get Msg 102, Level 15, State 1, Procedure TRG_SESN_ITM_ENTITY, Line 10Incorrect syntax near '@PARTYID'.create trigger [TRG_EMPLOYEE] ON EMPLOYEE AFTER INSERT, UPDATE ASBEGINDECLARE @PARTYID BIGINTDECLARE @PARTYTYP INTSELECT @PARTY_ID = PARTY_ID, @PARTYTYP = PARTYTYP FROM INSERTEDIF @PARTYTYP = 1EXEC P_CREATE_PERSON (@PARTYID)END |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-09-22 : 15:40:59
|
| Assuming P_CREATE_PERSON is a stored procedure, take the parenthisis out from around the input parameter.Be One with the OptimizerTG |
 |
|
|
|