Link to home
Start Free TrialLog in
Avatar of CipherIS
CipherISFlag for United States of America

asked on

SQL - Pass Value to Trigger

I do not know if this can be done.  I know with a stored procedure it can be accomplished.  I want to know if it can be accomplished with a Trigger.  Ok, lets say there are 3 tables - tableA, tableB, and tableC.   The result is to get data from tableA and tableB into tableC.

I call a stored procedure and in this stored procedure I pass in the values for TableA to perform an insert.  I am also inserting into TableB and getting the ID by SCOPE_IDENTITY.

So lets say I need the following fileds TableA.Name, TableA.Address, and TableC.ResultsID (these tables are not joined in anyway and the value of TableC.ResultsID will be obtained using SCOPE_IDENTITY).

The question is if I put a trigger on TableA to pass the data to TableC --  how do I get the TableC.ResultID data in the Trigger?

Hope this makes sense.
Avatar of Philippe Damerval
Philippe Damerval
Flag of United States of America image

The answer to your question of whether this can be done or not is yes, it can be done and is fairly easy. SCOPE_IDENTITY returns the last ID used in the current scope (current SP, UDF or batch).
The real question I think is what are you trying to achieve? Why are you copying data from one table to another? Once we determine what you want to achieve, we can best recommend how to do it, which may or may not involve launching inserts from inside triggers.

Let us know!

Philippe
Avatar of CipherIS

ASKER

I am building an alert system.  TableC will contain information for the Alerts.  

Certain fields in TableA (lets say its an employee table) need to be passed to TableC when an insert, update, or delete is performed.  

TableB is a log table.

The requirement is to pass the pkid of TableB to TableC while inserting TableA data into TableC.  

(hope I didn't confuse anyone).
ASKER CERTIFIED SOLUTION
Avatar of CipherIS
CipherIS
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Alpesh Patel
You can not pass parameter to Trigger. But you can access the deleted, inserted tables in it.
test
It's okay with me, although in the spirit of the experts-exchange community I wish the author would post a brief description of what solution worked for them. It is not clear whether the advice given by experts was useful or not.
Thanks!
figured it out