• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 140
  • Last Modified:

Help with Trigger

I need a trigger that will update a table with values inserted into another table.

Say an insert is done to table "A".  This table has some data columns and a FK column to table "B".   Upon insert, I want to update table "B" using the FK in a WHERE clause and setting 3 other data columns, all of which exist in both tables.
0
HLRosenberger
Asked:
HLRosenberger
  • 3
  • 2
1 Solution
 
Scott PletcherSenior DBACommented:
CREATE TRIGGER A_TRG_INSERT
ON A
AFTER INSERT
AS
SET NOCOUNT ON
UPDATE B
SET col1 = i.col1,
       col2 = i.col2,
       col3 = i.col3
FROM inserted i
INNER JOIN B ON
    B.B_key = i.B_key
GO
0
 
HLRosenbergerAuthor Commented:
I got it working.  thanks,.
0
 
HLRosenbergerAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for HLRosenberger's comment #a40551014

for the following reason:

I got it working.  thanks,.
0
 
Scott PletcherSenior DBACommented:
I provided a working example of how to use the inserted table in a trigger to join to and update a different table.  I believe that had to be of some value in coding such a trigger.
0
 
HLRosenbergerAuthor Commented:
Sorry.  I meant to accept your solution.

I got it working.  thanks,.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now