Solved

SQL Trigger Help - Update another table after INSERT

Posted on 2012-03-23
3
337 Views
Last Modified: 2012-08-14
Hi Experts, I need a trigger to update table tScheduleRequests when an INSERT is performed on table AR1.  This is actually my first attempt at a trigger and I know it's not too difficult, but I need a push in the right direction... This is what I have so far using the SQL template as a guide:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE TRIGGER trUpdateScheduleRequestsA1
   ON  dbo.AR1
   AFTER INSERT
AS

IF EXISTS (SELECT OrderNumber, SchDte FROM Schedule.dbo.tScheduleRequests, inserted WHERE OrderNumber=Ordno)

BEGIN

	SET NOCOUNT ON;

	UPDATE Schedule.dbo.tScheduleRequests
	SET ScheduledDate=i.SchDte
	INNER JOIN inserted As i
		ON tScheduleRequests.OrderNumber=i.Ordno

END
GO

Open in new window


Thanks in advance

-Jeremy
0
Comment
Question by:FamousMortimer
  • 2
3 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 37758467
CREATE TRIGGER trUpdateScheduleRequestsA1
   ON  dbo.AR1
   AFTER INSERT
AS

UPDATE t
SET ScheduledDate=i.SchDte
FROM Schedule.dbo.tScheduleRequests As t
INNER JOIN inserted As i
      ON t.OrderNumber=i.Ordno

GO
0
 
LVL 10

Author Closing Comment

by:FamousMortimer
ID: 37758663
Thanks a lot
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 37758828
You're welcome.

The INNER JOIN automatically prevents unwanted rows from being UPDATEd.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Designing and Implementing a Data Warehouse 3 43
SQL 2008 with .NET 4.5.2 4 35
sql 2014,  lock limit 5 37
Loop through SQL parameters and insert to temp table? 4 45
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

792 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