Solved

SQL Trigger Help - Update another table after INSERT

Posted on 2012-03-23
3
340 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
[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
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Parse this column 6 38
Database Owner 3 41
EF5: Update Model from Database not working 3 73
Please explain the difference between EXCLUDE, INTERSECT and JOIN 7 49
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…
In this article I will describe the Detach & Attach 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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 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