|
|
|
SSChasing Mays
      
Group: General Forum Members
Last Login: Yesterday @ 5:12 AM
Points: 649,
Visits: 686
|
|
Comments posted to this topic are about the item UPDATE() and triggers
----- a haiku...
NULL is not zero NULL is not an empty string NULL is the unknown
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Sunday, April 14, 2013 8:55 AM
Points: 1,383,
Visits: 1,212
|
|
A trickier variation could be the same question, without the "Insert" statement.
Even though there are no rows being updated in that variant, the trigger still finds that the column was being updated.
http://poorsql.com for T-SQL formatting: free as in speech, free as in beer, free to run in SSMS or on your version control server - free however you want it.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: 2 days ago @ 6:25 AM
Points: 1,364,
Visits: 289
|
|
"The UPDATE(Column) function returns TRUE even if an UPDATE does not change the value."
But, the update DOES change the value, right? Even though it may be the same value, it does get updated. It's not like SQL Server says to itself, hey, this value is the same as then one that's already there, so don't even mess with the physical I/O's here.
|
|
|
|
|
Say Hey Kid
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 5:00 AM
Points: 662,
Visits: 239
|
|
i am refferning to the below question posted,
What is the output of the following code (disregarding "X row(s) affected"):
CREATE TABLE TriggerTest (Value int) GO INSERT INTO TriggerTest VALUES (1) GO
CREATE TRIGGER tr_TriggerTest ON TriggerTest AFTER UPDATEAS BEGIN SET NOCOUNT ON; IF UPDATE(Value) PRINT 'The trigger fired' ELSE PRINT 'The trigger did not fire'END GO
--Set Value equal to itself UPDATE TriggerTestSET Value = Value;
DROP TABLE TriggerTest;
=======================
I have tried this and got fallowing error,
Server: Msg 245, Level 16, State 1, Line 1 Syntax error converting the varchar value 'Value' to a column of data type int.
it is said that the trigger gets fired, then why 'The trigger fired' statement doesn't got printed.
Anil Kubireddi
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 5:06 AM
Points: 5,235,
Visits: 7,041
|
|
anil.kubireddi (12/2/2009) i am refferning to the below question posted,
What is the output of the following code (disregarding "X row(s) affected"):
CREATE TABLE TriggerTest (Value int) GO INSERT INTO TriggerTest VALUES (1) GO
CREATE TRIGGER tr_TriggerTest ON TriggerTest AFTER UPDATEAS BEGIN SET NOCOUNT ON; IF UPDATE(Value) PRINT 'The trigger fired' ELSE PRINT 'The trigger did not fire'END GO
--Set Value equal to itself UPDATE TriggerTestSET Value = Value;
DROP TABLE TriggerTest;
=======================
I have tried this and got fallowing error,
Server: Msg 245, Level 16, State 1, Line 1 Syntax error converting the varchar value 'Value' to a column of data type int.
it is said that the trigger gets fired, then why 'The trigger fired' statement doesn't got printed.
Hi Anil,
Is there any possibility that you executed UPDATE TriggerTest SET Value = 'Value';
instead of UPDATE TriggerTest SET Value = Value;
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Sunday, April 14, 2013 8:55 AM
Points: 1,383,
Visits: 1,212
|
|
vkirkpat (12/2/2009) But, the update DOES change the value, right? Even though it may be the same value, it does get updated. It's not like SQL Server says to itself, hey, this value is the same as then one that's already there, so don't even mess with the physical I/O's here.
Right - but even if SQL Server was not doing any update at all (if there were no rows in the table, or if the WHERE clause excluded all rows), it would still consider that the column was updated.
anil.kubireddi (12/2/2009)
I have tried this and got fallowing error,
Server: Msg 245, Level 16, State 1, Line 1 Syntax error converting the varchar value 'Value' to a column of data type int.
it is said that the trigger gets fired, then why 'The trigger fired' statement doesn't got printed.
It looks like the copy/paste from your browser or email client lost some carriage returns ("UPDATEAS" and "UPDATE TriggerTestSET Value = Value;") - is that the cause?
http://poorsql.com for T-SQL formatting: free as in speech, free as in beer, free to run in SSMS or on your version control server - free however you want it.
|
|
|
|
|
Say Hey Kid
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 5:00 AM
Points: 662,
Visits: 239
|
|
Okay..thank you...
Anil Kubireddi
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 7:23 AM
Points: 2,863,
Visits: 2,466
|
|
Great question - thought provoking. ALMOST got me to say trigger did not fire.
Steve Jimmo Sr DBA “If we ever forget that we are One Nation Under God, then we will be a Nation gone under." - Ronald Reagan
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Friday, November 09, 2012 7:25 AM
Points: 298,
Visits: 107
|
|
Tao Klerks (12/2/2009)
Right - but even if SQL Server was not doing any update at all (if there were no rows in the table, or if the WHERE clause excluded all rows), it would still consider that the column was updated.
Now THAT's something I didn't know about but should have. Perhaps the question would have been better if the UPDATE statement was:
UPDATE TriggerTest SET Value = VALUE WHERE VALUE = 0;
In this case the trigger still fires, and more importantly "IF UPDATE(Value)" returns true, even though no rows were updated, so actually no values were updated. (Yes, in the original version the Value column is updated from 1 to 1) I think the output text is a bit misleading though. I would have put "Value field was updated" and "Value field was not updated" in the IF block, and another line before the block for "Trigger Fired".
e.g.
CREATE TABLE TriggerTest (Value int) GO INSERT INTO TriggerTest VALUES (1) GO
CREATE TRIGGER tr_TriggerTest ON TriggerTest AFTER UPDATE AS BEGIN SET NOCOUNT ON; PRINT 'The Trigger Fired'
IF UPDATE(Value) BEGIN PRINT 'The Value column was updated' select * FROM INSERTED END ELSE BEGIN PRINT 'The Value column was not updated' END END GO
--Set Value equal to itself UPDATE TriggerTest SET Value = VALUE WHERE VALUE = 0;
DROP TABLE TriggerTest;
|
|
|
|
|
SSC-Dedicated
           
Group: Administrators
Last Login: Yesterday @ 6:14 PM
Points: 31,421,
Visits: 13,734
|
|
|
|
|