Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Trigger that updates field within same table

Author  Topic 

ATG
Starting Member

35 Posts

Posted - 2012-05-22 : 19:13:28
I need a trigger that upon one field being updated, it takes that data and puts it into a different field in the same table.

The SQL command would be:

update P
set P.PurchaseAmt=P.EstimatedCost

so I'd imagine the trigger would be something like...


ALTER trigger dccPu on P AFTER UPDATE as
BEGIN

if update(P.EstimatedCost)
begin
update P
set P.PurchaseAmt=P.EstimatedCost
end

END

Logically that makes sense to me but it doesn't work. Also, the trigger will potentially have multiple "if update"s...

Thanks :)

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-05-22 : 19:27:29
Why don't you use a computed column instead?

Your trigger doesn't work because you aren't referencing the inserted trigger table.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

ATG
Starting Member

35 Posts

Posted - 2012-05-22 : 20:04:25
Yes. I wasn't trying to actually execute this code. I've figured it out, however, and it works fine.

Thanks!
Go to Top of Page
   

- Advertisement -