Unix Technical Forum

SQL Server trigger that fires only on update of certain field?

This is a discussion on SQL Server trigger that fires only on update of certain field? within the SQL Server forums, part of the Microsoft SQL Server category; --> Dear Experts, I'm an Oracle guy, who is being given more SQL Server assignments lately. I've been looking for ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 09:09 PM
dba_222@yahoo.com
 
Posts: n/a
Default SQL Server trigger that fires only on update of certain field?

Dear Experts,


I'm an Oracle guy, who is being given more SQL Server assignments
lately.

I've been looking for things on the web about this, but I can't
anything so far.

In Oracle, I you can create a trigger on a table that -only- fires if
certain fields are updated.

create or replace trigger trg_some_trigger
BEFORE insert
OF some_field1, some_field2
on tbl_some_table
for each row

....



Is this also possible in SQL Server?
What is the syntax please?


Thanks a lot!

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 09:09 PM
Michael Hotek
 
Posts: n/a
Default Re: SQL Server trigger that fires only on update of certain field?

Not directly. SQL Server is going to fire the trigger for the operation.
It doesn't conditionally fire it based on a column being changed. You can
accomplish this within your code by using the IF UPDATE(<columname>) clause.

--
Mike Hotek
MHS Enterprises, Inc
http://www.mssqlserver.com


<dba_222@yahoo.com> wrote in message
news:1155564108.206051.326610@74g2000cwt.googlegro ups.com...
> Dear Experts,
>
>
> I'm an Oracle guy, who is being given more SQL Server assignments
> lately.
>
> I've been looking for things on the web about this, but I can't
> anything so far.
>
> In Oracle, I you can create a trigger on a table that -only- fires if
> certain fields are updated.
>
> create or replace trigger trg_some_trigger
> BEFORE insert
> OF some_field1, some_field2
> on tbl_some_table
> for each row
>
> ...
>
>
>
> Is this also possible in SQL Server?
> What is the syntax please?
>
>
> Thanks a lot!
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 09:09 PM
Erland Sommarskog
 
Posts: n/a
Default Re: SQL Server trigger that fires only on update of certain field?

(dba_222@yahoo.com) writes:
> I'm an Oracle guy, who is being given more SQL Server assignments
> lately.
>
> I've been looking for things on the web about this, but I can't
> anything so far.
>
> In Oracle, I you can create a trigger on a table that -only- fires if
> certain fields are updated.
>
> create or replace trigger trg_some_trigger
> BEFORE insert
> OF some_field1, some_field2
> on tbl_some_table
> for each row
>
> ...
>


As Mike said, the best you can do is to check in the trigger whether
a columns was updated:

IF UPDATE(col)
BEGIN
-- Do stuff
END

But keep in mind that this not tell you whether any values in the
column were changed, only that it was mention in the SET clause of
an UPDATE statement. And for INSERT the condition is alwauys tru.

Also keep in mind that in SQL Server a trigger fires once per
statement, not once per row.

And, finally, there are no BEFORE triggers in SQL Server. Only INSTEAD OF
triggers and AFTER triggers.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:45 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com