Unix Technical Forum

Only update changed columns

This is a discussion on Only update changed columns within the SQL Server forums, part of the Microsoft SQL Server category; --> I have a stored procedure that inserts values into five columns of a table. I need another stored procedure ...


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, 08:09 PM
binder
 
Posts: n/a
Default Only update changed columns

I have a stored procedure that inserts values into five columns of a
table. I need another stored procedure that will allow the user to pass
one or more of those parameters and update only the column for the data
that was passed. In other words, the update may only have one or two of
the parameters that was originally provided in the insert. Therefore, I
do not want to update the columns that did not change. What is the
proper way to handle this situation?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 08:10 PM
Erland Sommarskog
 
Posts: n/a
Default Re: Only update changed columns

binder (rgondzur@gmail.com) writes:
> I have a stored procedure that inserts values into five columns of a
> table. I need another stored procedure that will allow the user to pass
> one or more of those parameters and update only the column for the data
> that was passed. In other words, the update may only have one or two of
> the parameters that was originally provided in the insert. Therefore, I
> do not want to update the columns that did not change. What is the
> proper way to handle this situation?


In our application, most insert/update procedures exposes about all
columns in the table, and when the client fills the GUI, it reads all
columns from the table. Thus there is little reason to check what
actually changed. (Except for auditing.)

If you want to expose an interface of a procedure where the caller only
specifies what the to change, because it has for some reason not read
all existing columns (and this makes sense for a client that is a
monitori or similar), you could do:

CREATE PROCEDURE update_sp @keyvalue sometype,
@par1 someothertype = NULL,
@par2 yetanothertype = NULL,
.... AS

UPDATE tbl
SET col1 = coalesce(@par1, col1),
col2 = coalesce(@par2, col2),
...
WHERE keycol = @keyvalue

That is, the procedure accepts parameters for all updatable columns,
but the caller passes non-NULL values for those it does not want to
change. This presumes that a caller never want to set a column to
NULL. If this is a required, you need to add one flag parameter for
each value parameter.


--
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 09:36 PM.


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