Avatar of ottenm
ottenm

asked on 

trigger help with columns_updated

In SQL 2000, is it possible to create a trigger that only fires when the data in a particular column has been changed?  I can't find an explanation or and example of columns_updated that would answer this for me.  What is the syntax for columns_updated?  And does it "report" all the columns in the "UPDATE" statement?  Or just the ones where the new data being assigned is actually different from what was there?

My hope is to create a trigger for the table order_entry, that will set the field 'exported' to null, whenever the value in the field 'freight_amount' actually changes.

Thanks for any help.
Microsoft SQL Server

Avatar of undefined
Last Comment
chapmandew
ASKER CERTIFIED SOLUTION
Avatar of dportas
dportas

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of chapmandew
chapmandew
Flag of United States of America image

article I wrote on how to use columns_updated()

http://blogs.techrepublic.com.com/datacenter/?p=326
Avatar of ottenm
ottenm

ASKER

dportas: proc sounds good enough, thanks for the suggestion.

chapmandew: wrong article? (link is on "dates tables")

thanks to both-
It's much clearer and easier to use "UPDATE(column_name)", for example:

CREATE TRIGGER order_entry_trig_set_exported
ON order_entry
AFTER UPDATE
AS
IF @@ROWCOUNT = 0
    RETURN
IF UPDATE(freight_amount)
BEGIN
    UPDATE order_entry
    SET exported = NULL
    FROM order_entry
    INNER JOIN inserted ON inserted.keyCol = order_entry.keyCol --AND ~.keyCol2 = ~~.keyCol2 ...
    INNER JOIN deleted ON deleted.keyCol = order_entry.keyCol --AND ~.keyCol2 = ~~.keyCol2 ...
    WHERE (inserted.freight_amount <> deleted.freight_amount)
    OR (inserted.freight_amount IS NULL AND deleted.freight_amount IS NOT NULL)
    OR (inserted.freight_amount IS NOT NULL AND deleted.freight_amount IS NULL)
END --IF

GO
Avatar of chapmandew
chapmandew
Flag of United States of America image

Sorry about that...I had posted that on another question, and I guess it was still on my clipboard.  At any rate, have a look at this article....it will give you an idea on how columns_updated() works.

http://www.zdnetasia.com/techguide/database-management/0,3800010795,62042223,00.htm
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo