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.
Author |
Topic |
sqlfresher2k7
Aged Yak Warrior
623 Posts |
Posted - 2011-01-27 : 14:28:40
|
[code]I want a trigger which should update prvaddress1 if the address1 was updated with new address.Example:stu_id sname address1 prvaddress1------ ----- -------- ------------1 sam Newyork When a address1 has been updated DC it previous address (prvaddress1) should be updated to NewYorkstu_id sname address1 prvaddress1------ ----- -------- ------------1 sam DC NewyorkThanks for your help in advance[/code] |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2011-01-27 : 14:34:56
|
CREATE TRIGGER dbo.trgTable1ON dbo.Table1FOR UPDATEASSET NOCOUNT ONUPDATE xSET x.prvAddress1 = d.address1FROM dbo.Table1 AS xINNER JOIN deleted AS d ON d.stu_id = x.stu_id N 56°04'39.26"E 12°55'05.63" |
 |
|
sqlfresher2k7
Aged Yak Warrior
623 Posts |
Posted - 2011-01-27 : 16:07:04
|
Thanks a lot PesoI want the updates whenever there is change in the address1 value however with the below trigger it is updating whenever the other column updatesEx: sname has updated then also the trigger fires and prvaddress1 has been updated as per the req which should not happen.Please help for above scenario. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2011-01-27 : 16:54:26
|
Did you try to find the missing piece of information in Books Online?CREATE TRIGGER dbo.trgTable1ON dbo.Table1FOR UPDATEASSET NOCOUNT ONIF UPDATE(address1)UPDATE xSET x.prvAddress1 = d.address1FROM dbo.Table1 AS xINNER JOIN deleted AS d ON d.stu_id = x.stu_idINNER JOIN inserted AS i ON i.stu_id = x.stu_idWHERE i.address1 <> d.address1OR d.address1 IS NULL N 56°04'39.26"E 12°55'05.63" |
 |
|
|
|
|