时间:2014-10-17 09:51:58 来源: 复制分享
MSSQL 存储过程实现更新,如果要更新的字段内容为空,则该字段不作变更,只更新其他字段
一项目有这个需求,顺便将代码贴上,共同学习。
/****** 定义输入参数 ******/
ALTERproc[dbo].[upkeywords]
@inkeyvalnvarchar(100),
@inkeycompeteint,
@inkeyincludeint,
@inKeyIDint
as
DECLARE
/****** 定义变量 ******/
@keyvalnvarchar(100),
@keycompeteint,
@keyincludeint,
@KeyIDint
/****** 给变量赋原来的值 ******/
set@keyval=(select KeyVal from Keywords where KeyID=@inKeyID)
set@keycompete=(select keycompete from Keywords where KeyID=@inKeyID)
set@keyinclude=(select keyinclude from Keywords where KeyID=@inKeyID)
/****** 如果传入的参数是NULL,就将原来的值还给它 ******/
update Keywords set
KeyVal=ISNULL(@inkeyval,@keyval),
keycompete=ISNULL(@inkeycompete,@keycompete),
keyinclude=ISNULL(@inkeyinclude,@keyinclude)
where KeyID=@inKeyID