SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


when one column is updated, want to update another column in same table using trigger.


when one column is updated, want to update another column in same table using trigger.

Author
Message
Joy Smith San
Joy Smith San
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12276 Visits: 3208
Dear All,

I have a table in which there are fields like Quantity, Price, Total etc.There's is also a primary key field in this table.
We have an interface where user can change the quantity (only quantity).

I want to write a trigger on this table which calculates Total (qty * price) whenever qantity is updated.

How can I do it..?. How will I know for which row the quantity is updated..?

Pleas help...




Thanks in advance..
Santhosh Nair.
SwePeso
SwePeso
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18449 Visits: 3433
Something similar to this?

CREATE TRIGGER dbo.trgMyTotalUpdate ON dbo.MyTable
AFTER UPDATE
AS

IF UPDATE(Quantity)
UPDATE mt
SET mt.Total = i.Quantity * i.Price
FROM dbo.MyTable AS mt
INNER JOIN inserted AS i ON i.PkCol = mt.PkCol




N 56°04'39.16"
E 12°55'05.25"
Joy Smith San
Joy Smith San
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12276 Visits: 3208
Thanks a lot...It's working...
Jerry Hung
Jerry Hung
SSCertifiable
SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)

Group: General Forum Members
Points: 6506 Visits: 1208
how about a Computed Column that "computes" i.Quantity * i.Price

http://www.sqlservercentral.com/articles/User-Defined+functions/complexcomputedcolumns/2397/

SQLServerNewbie

MCITP: Database Administrator SQL Server 2005
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search