Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code for a trigger help

Status
Not open for further replies.

LeanneGodney

Technical User
Mar 7, 2002
175
GB
Hi there,

Could someone please give me an idea of the code I should put into a trigger for a table?

When a record is ADDED to a table called dbo.tblTrades I would like it to automatically add a record to three other tables using the same ID number that the first entry recieved.

Your help is most appreciated!

Leanne
 
Leanne,

You can get hold of the ID for the newly inserted record by running a select statement like "SELECT RecID FROM inserted" where RecID is the name of the auto-number field you're interested in. To make things simple you could assign this value to a variable and use this variable in your following insert statements. Something like this:

DECLARE @NewID int

SET @NewID = (SELECT RecID FROM inserted)

INSERT INTO TABLE1 (Afield, IDfield) VALUES ('sometext', @NewID)
INSERT INTO Table2 ...

...and so on

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top