Solved

SQL Trigger Help - Update another table after INSERT

Posted on 2012-03-23
3
334 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:
ScottPletcher 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:ScottPletcher
ID: 37758828
You're welcome.

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

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 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

21 Experts available now in Live!

Get 1:1 Help Now