?
Solved

Help with Trigger

Posted on 2015-01-14
5
Medium Priority
?
123 Views
Last Modified: 2015-01-15
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
Comment
Question by:HLRosenberger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 40550097
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
 
LVL 1

Author Comment

by:HLRosenberger
ID: 40551014
I got it working.  thanks,.
0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 40551434
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
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40551435
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
 
LVL 1

Author Closing Comment

by:HLRosenberger
ID: 40551475
Sorry.  I meant to accept your solution.

I got it working.  thanks,.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

743 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