Solved

SQL trigger change data in inserted table??

Posted on 2006-07-21
6
1,911 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
[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
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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

737 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