Solved

SQL trigger change data in inserted table??

Posted on 2006-07-21
6
1,916 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:
Brendt Hess 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
Independent Software Vendors: 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

719 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