Solved

SQL trigger change data in inserted table??

Posted on 2006-07-21
6
1,910 Views
Last Modified: 2010-05-18
How can I change data in inserted table before MS SQL insert data from inserted table to real table?
Thanks for any helping
0
Comment
Question by:hungxadieu
6 Comments
 
LVL 32

Accepted Solution

by:
bhess1 earned 150 total points
ID: 17155381
What version of SQL Server?  

Assuming SQL2K or up, use an INSTEAD OF INSERT Trigger (look in BoL for information on this type of trigger).  Substitute the data you need for the data you get.
0
 
LVL 34

Assisted Solution

by:Brian Crowe
Brian Crowe earned 100 total points
ID: 17155388
you would need to create an INSTEAD OF INSERT trigger.
0
 

Author Comment

by:hungxadieu
ID: 17155431
Can you give me a simple example?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Assisted Solution

by:Sirees
Sirees earned 50 total points
ID: 17155463
0
 
LVL 1

Expert Comment

by:brigzy
ID: 23027527
You could use an AFTER UPDATE trigger.

I think it is an alternative solution to consider because an INSTEAD OF trigger has wide ranging maintenance issues: ie when a new field is added the INSTEAD OF trigger must allways be modified.
Here is my example:
I want to blank out a Solictor and PartyType fields when a Guardian field has been assigned .  (see code snippet)
Note : I have recursive triggers Disabled
Cheers
Brigzy

ALTER TRIGGER [dbo].[trg_usrChildren_Update]
   ON  [dbo].[Usr_Children] 
   AFTER UPDATE
AS 
BEGIN
	-- If Guardian set - blank out Solictor and PartyType
	UPDATE usr_Children
	SET Solicitor = NULL, PartyType = NULL
	FROM usr_Children uc INNER JOIN Inserted i ON uc.ID = i.ID
	WHERE RTRIM(ISNULL(i.GuardianAdLitem,'')) <> ''
END

Open in new window

0
 
LVL 1

Expert Comment

by:brigzy
ID: 23027567

Additional note :
Just to explain that the same above method can be applied to an AFTER INSERT, as more relevent to your initial question.
Cheers
Brigzy

ALTER TRIGGER [dbo].[trg_usrChildren_AfterInsert]
   ON  [dbo].[Usr_Children] 
   AFTER INSERT
AS 
BEGIN
	-- If Guardian set - blank out Solictor and PartyType
	UPDATE usr_Children
	SET Solicitor = NULL, PartyType = NULL
	FROM usr_Children uc INNER JOIN Inserted i ON uc.ID = i.ID
	WHERE RTRIM(ISNULL(i.GuardianAdLitem,'')) <> ''
END

Open in new window

0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

679 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