Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 t-sql 2008 r2 for stored procedure

Author  Topic 

scamper
Yak Posting Veteran

52 Posts

Posted - 2012-05-21 : 22:27:16
I want to setup a stored procedure for a sql server 2008 r2 that can update between 0 to 8 columns on one table. The stored procedure will take the 8 parameters and compare the parameter values to the actual values for the corresponding columns in the table.
If there is a difference beween the value(s) in the table and the value(s) in the parameter, the value(s) in the table will be updated. However the problem is when there are null values in the parameters, I do not want to update the acutal column(s) with null values.
Thus can you suggest t-sql statements I can use to solve this problem?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-21 : 23:05:01
the statement should be like

UPDATE table
SET Column1 = CASE WHEN Column1 <> @Column1 THEN @Column1 ELSE Column1 END,
Column2 = CASE WHEN Column2 <> @Column2 THEN @Column2 ELSE Column2 END,
...



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -