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
 General SQL Server Forums
 New to SQL Server Programming
 Trigger Question

Author  Topic 

sinva
Starting Member

23 Posts

Posted - 2010-05-19 : 18:39:53
Dear all,

As the trigger is checking on the table, when I have update on multiple rows of a table A, the statement select columnA from deleted will return a single row or multiple rows??

Thanks in advance

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-19 : 18:49:58
It'll contain the same number of rows as are impacted by the delete (or other DML statement). Please see this: http://weblogs.sqlteam.com/tarad/archive/2004/09/14/2077.aspx

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

Subscribe to my blog
Go to Top of Page

sinva
Starting Member

23 Posts

Posted - 2010-05-19 : 18:54:18
Thank you for your reply. That means I could not use "select columnA from deleted <> select columnA from inserted" to check if the value in columnA has been changed? Am I right?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-19 : 18:58:21
From BOL:
quote:

Testing for UPDATE or INSERT Actions to Specific Columns
You can design a Transact-SQL trigger to perform certain actions based on UPDATE or INSERT modifications to specific columns. Use UPDATE() or COLUMNS_UPDATED in the body of the trigger for this purpose. UPDATE() tests for UPDATE or INSERT tries on one column. COLUMNS_UPDATED tests for UPDATE or INSERT actions that are performed on multiple columns and returns a bit pattern that indicates which columns were inserted or updated.



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

Subscribe to my blog
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-05-19 : 20:59:34
quote:
Originally posted by sinva

Thank you for your reply. That means I could not use "select columnA from deleted <> select columnA from inserted" to check if the value in columnA has been changed? Am I right?


Yes. You can't do that. INSERTED or DELETED may contain more than 1 record.

it should be

select *
from inserted i
inner join deleted d on i.pkcol = d.pkcol
where i.columnA <> d.columnA
or (i.columnA is null and d.columnA is not null)
or (i.columnA is not null and d.columnA is null)



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

sinva
Starting Member

23 Posts

Posted - 2010-05-19 : 21:51:26
KH, Thank you for your advice

Then do you think if there are some more checking I should first join the inserted and deleted and then put it to a temp table and loop the records in the temp table and have the checking??

Thanks
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-05-19 : 22:23:25
i don't quite get the temp table & looping part. If you are doing some sort of audit trail you can insert to the audit trail table with the result of joining the inserted with deleted table.


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

sinva
Starting Member

23 Posts

Posted - 2010-05-19 : 23:51:22
Sorry KH,

As I have some more checking for the deleted and inserted, for example I may need to join other tables for comparison. Then should I create temp table and use cursor to loop the temp table and compare??

Thank you for your reply
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-05-20 : 01:30:57
you don't have to do that.
just join it to the required table

from inserted i
inner join deleted d on i.pkcol = d.pkcol
inner join othertable o on i.somecol = o.somecol



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

sinva
Starting Member

23 Posts

Posted - 2010-05-20 : 04:35:10
KH,

Thank you. But if I have to perform many checkings some join table X some join table Y then should I use the cursor and a temp table??

Thank you
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-05-20 : 04:55:10
no. if you can have the checking within a query why do you want to use temp table + cursor ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -