Solved

SQL trigger change data in inserted table??

Posted on 2006-07-21
6
1,902 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MS SQL Bulk load data error 5 34
c# code 19 61
Date conversion in sql server 2012 6 26
SQL Server 2012 Row Selection 2 30
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

747 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

10 Experts available now in Live!

Get 1:1 Help Now