Solved

SQL - Pass Value to Trigger

Posted on 2010-11-10
7
598 Views
Last Modified: 2012-08-13
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.
0
Comment
Question by:CipherIS
  • 4
  • 2
7 Comments
 
LVL 9

Expert Comment

by:damerval
ID: 34106584
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
0
 
LVL 1

Author Comment

by:CipherIS
ID: 34106726
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).
0
 
LVL 1

Accepted Solution

by:
CipherIS earned 0 total points
ID: 34106974
i figured out how to do it.  thx.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 21

Expert Comment

by:Alpesh Patel
ID: 34110075
You can not pass parameter to Trigger. But you can access the deleted, inserted tables in it.
0
 
LVL 1

Author Comment

by:CipherIS
ID: 34313898
test
0
 
LVL 9

Expert Comment

by:damerval
ID: 34313965
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!
0
 
LVL 1

Author Closing Comment

by:CipherIS
ID: 34341638
figured it out
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now