November 20, 2002 at 12:59 pm
I'm having problems with the following
trigger, which works fine in SQL6.5,
but doesn't work the same in SQL2000.
In 6.5, col3 changes only when col2
changes. In 2000, col3 changes every
time an update to ANY column occurs.
Can anybody tell me what I need to do
to make the trigger work the same in
2000 as it does in 6.5?
CREATE TRIGGER [trTEMPTBL_UPD] ON [dbo].[temptbl]
FOR UPDATE AS
declare @col1 varchar(50)
select @col1 = i.col1
from inserted i, temptbl t
where i.col1 = t.col1
begin
if update(col2)
update temptbl
set col3 = col3 + 'x'
where col1 =@col1
end
Thanks in advance. All help will be appreciated.
Paul
Edited by - paul corbin on 11/20/2002 1:02:17 PM
November 20, 2002 at 2:17 pm
The UPDATE function will return true if that column was included in the SET clause of the UPDATE statement even if the value does not change.
Your trigger will only work with singleton updates try this
update temptbl
set col3 = col3 + 'x'
FROM inserted i
join deleted on i.col1 = d.col1 AND ISNULL(i.col2,-1) <> ISNULL(d.col2,-1)
where temptbl.col1 = i.col1
Simon Sabin
Co-author of SQL Server 2000 XML Distilled
http://www.amazon.co.uk/exec/obidos/ASIN/1904347088
Simon Sabin
SQL Server MVP
http://sqlblogcasts.com/blogs/simons
November 20, 2002 at 2:39 pm
Thank you! Thank you! THANK YOU!
With just a little tweaking it works just
the way it did in SQL6.5.
Here's how I rewrote it. You can see the
changes were very small.
update temptbl
set col3 = i.col3 + 'x'
FROM inserted i
join deleted d on i.col1 = d.col1 AND ISNULL(i.col2,-1) != ISNULL(d.col2,-1)
where temptbl.col1 = i.col1
THANK YOU again!
November 21, 2002 at 2:58 am
Also be aware that col3 + 'x' will be NULL if col3 is NULL unlike in 6.5 where it would be 'x', you might want to include ISNULL(col3) + 'x'
Simon Sabin
Co-author of SQL Server 2000 XML Distilled
http://www.amazon.co.uk/exec/obidos/ASIN/1904347088
Simon Sabin
SQL Server MVP
http://sqlblogcasts.com/blogs/simons
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply