dbTalk Databases Forums  

MSSQL: Clear a field if not explicitly set in UPDATE or INSERT

comp.databases comp.databases


Discuss MSSQL: Clear a field if not explicitly set in UPDATE or INSERT in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Leif Neland
 
Posts: n/a

Default MSSQL: Clear a field if not explicitly set in UPDATE or INSERT - 02-10-2010 , 04:11 AM






I want to audit which program/module changes the content of a table
table stock:
id: integer // unique key
location: string // where is it
in_stock: integer // How many do we have
wanted: integer // How many do we want to keep in stock
src: string // who and what changed it

My queries are supposed to be like
update stock set in_stock=in_stock+1,src='a=goods_received;u=johnso n'
where id=12345

I have a trigger which copies the values and a timestamp to stock_audit.

But if src is not set in a query
update stock set in_stock=in_stock-1 where id=12345

src keeps its old value, which is misleading.
So I want to put a default value in src if it is not explicitly set in the
query.

Can this be done?

I'm searching the app for every place where an update can take place, so
this should be a way to trace "unexpected" updates.

Leif

Reply With Quote
  #2  
Old   
Kalle Olavi Niemitalo
 
Posts: n/a

Default Re: MSSQL: Clear a field if not explicitly set in UPDATE or INSERT - 02-10-2010 , 04:15 PM






"Leif Neland" <leif (AT) neland (DOT) dk> writes:

Quote:
I have a trigger which copies the values and a timestamp to stock_audit.

But if src is not set in a query
update stock set in_stock=in_stock-1 where id=12345

src keeps its old value, which is misleading.
So I want to put a default value in src if it is not explicitly set in the
query.

Can this be done?
In MSSQL, UPDATE triggers can use IF UPDATE(column) to check
whether the named column was explicitly updated. I suppose that
should work in a CASE expression too, e.g. CASE WHEN UPDATE(src)
THEN inserted.src ELSE 'default value' END.

I'm not sure whether UPDATE(column) in INSERT triggers returns
the correct value or just always TRUE. Although perhaps it does
not matter for your purposes.

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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.