SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to use Trigger Before Insert or Update
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

hspatil31
Posting Yak Master

182 Posts

Posted - 04/08/2009 :  00:50:52  Show Profile  Reply with Quote
Dear Freind,

In the following Trigger all values not saved in database. Means i want to fire this Trigger before Insert or Update. Can anybody suggest me what is change in that Trigger.

ALTER TRIGGER AmountForPCTitle
ON dbo.ORDR
AFTER INSERT,
UPDATE
AS

SET NOCOUNT ON

UPDATE x
SET x.DocTotal = COALESCE(i.DocTotalSy, 0) + COALESCE(i.U_Ref, 0) + COALESCE(i.U_Sub, 0)
FROM dbo.ORDR AS x
INNER JOIN inserted AS i ON i.DocEntry = x.DocEntry

Thanks
Harish

svicky9
Posting Yak Master

United Kingdom
232 Posts

Posted - 04/08/2009 :  03:13:51  Show Profile  Visit svicky9's Homepage  Reply with Quote
I am assuming you want to do something before you update or insert the table.

In the above code, use the For Trigger and add the Insert Statement after your Trigger Update Code.


ALTER TRIGGER AmountForPCTitle
ON dbo.ORDR
FOR INSERT,
UPDATE
AS

SET NOCOUNT ON

Begin

UPDATE x
SET x.DocTotal = COALESCE(i.DocTotalSy, 0) + COALESCE(i.U_Ref, 0) + COALESCE(i.U_Sub, 0)
FROM dbo.ORDR AS x
INNER JOIN inserted AS i ON i.DocEntry = x.DocEntry

<Your Insert goes here>

End

Hope this helps

http://www.sqlserver007.com
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30265 Posts

Posted - 04/08/2009 :  03:19:53  Show Profile  Visit SwePeso's Homepage  Reply with Quote
What do you mean with "all values not saved in database"?



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 04/10/2009 :  06:46:25  Show Profile  Reply with Quote
quote:
Originally posted by hspatil31

Dear Freind,

In the following Trigger all values not saved in database. Means i want to fire this Trigger before Insert or Update. Can anybody suggest me what is change in that Trigger.

ALTER TRIGGER AmountForPCTitle
ON dbo.ORDR
INSTEAD OF AFTER INSERT,
UPDATE
AS

SET NOCOUNT ON

UPDATE x
SET x.DocTotal = COALESCE(i.DocTotalSy, 0) + COALESCE(i.U_Ref, 0) + COALESCE(i.U_Sub, 0)
FROM dbo.ORDR AS x
INNER JOIN inserted AS i ON i.DocEntry = x.DocEntry

Thanks
Harish



if you want to fire trigger before insert/update use INSTEAD OF rather than AFTER
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.28 seconds. Powered By: Snitz Forums 2000