You can use case in the update statement.
Hope this helps:
Let's assume your table is as below:
create table tblTest
(
ID int,
column1 varchar(10),
column2 varchar(10),
column3 varchar(10),
column4 varchar(10),
column5 varchar(10)
)
Insert values for test:
Insert into tblTest (ID, Column1,Column2,Column3,Column4, Column5) values (1,'a','b','c','d','e')
Set up variables for test.
declare @ID int
declare @col1 varchar(10)
declare @col2 varchar(10)
declare @col3 varchar(10)
declare @col4 varchar(10)
declare @col5 varchar(10)
set @ID = 1
set @col1 = 'a'
set @col2 = ''
set @col3 = 'y'
set @col4 = 'd'
set @col5 = 'z'
update tblTest
set
column1 = case when LEN(@col1) > 0 then @col1 else column1 end,
column2 = case when LEN(@col2) > 0 then @col2 else column2 end ,
column3 = case when LEN(@col3) > 0 then @col3 else column3 end ,
column4 = case when LEN(@col4) > 0 then @col4 else column4 end ,
column5 = case when LEN(@col5) > 0 then @col5 else column5 end
where
ID = @ID
select * from tblTest
I've created a sample in SQL Fiddle here: http://sqlfiddle.com/#!3/e666d/6
@col1and@col3values, the update will only update those two columns? Is this in a stored procedure or in C# directly - meaning, where/how does this query get generated and run? These factors will determine the answers.