开发者

How to update T-SQL record and ignoring UPDATE on parameters where NULL

Is there a way to create UPDATE stored_procedure with parameters like:

@param1 int = null,
@param2 int = null,
@param3 nvarchar(255) = null,
@param4 bit = null,
@id int

and with UPDATE statement which will update only fields which are not NULL

so if I execute

spUpdateProcedure @param1=255, @id=1

if will up开发者_StackOverflow社区date record @id=1 but it will change only field @param1 and will ignore changes to other @param2,3,4.

In other words, it wont change value for null in @param2,3,4

Thanks.


UPDATE YourTable
    SET Column1 = COALESCE(@param1, Column1),
        Column2 = COALESCE(@param2, Column2),
        ...
    WHERE id = @id


on your edit statement, you can do this

update table
set
column1 = isnull(@param1,column1),
column2  isnull(@param2,column2)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜