sql – 如果存储过程中的参数为null,则不更新列

我有以下存储过程.

每当@logo为null时,将删除当前值.如果@logo为NULL,我想不更新logo的值.

IF OBJECT_ID ('kii.p_UpdateDocumentStyle') IS NOT NULL
   DROP PROCEDURE kii.p_UpdateDocumentStyle
GO

CREATE PROCEDURE kii.p_UpdateDocumentStyle
   @DocumentId AS INT,@TitleForegroundColor AS NVARCHAR(10),@TitleBackgroundColor AS NVARCHAR(10),@TitleFontFamily AS NVARCHAR(50),@TitleFontSize AS NVARCHAR(10),@TitleFontStyle AS NVARCHAR(10),@TitleFontWeight AS NVARCHAR(10),@TitleTextDecoration AS NVARCHAR(15),@SectionTitleForegroundColor AS NVARCHAR(10),@SectionTitleBackgroundColor AS NVARCHAR(10),@SectionTitleFontFamily AS NVARCHAR(50),@SectionTitleFontSize AS NVARCHAR(10),@SectionTitleFontStyle AS NVARCHAR(10),@SectionTitleFontWeight AS NVARCHAR(10),@SectionTitleTextDecoration AS NVARCHAR(15),@ParagraphForegroundColor AS NVARCHAR(10),@ParagraphBackgroundColor AS NVARCHAR(10),@ParagraphFontFamily AS NVARCHAR(50),@ParagraphFontSize AS NVARCHAR(10),@ParagraphFontStyle AS NVARCHAR(10),@ParagraphFontWeight AS NVARCHAR(10),@ParagraphTextDecoration AS NVARCHAR(15),@logo AS Image = NULL
AS

UPDATE kii.DocumentStyle
SET 
    TitleForegroundColor = @TitleForegroundColor,TitleBackgroundColor = @TitleBackgroundColor,TitleFontFamily = @TitleFontFamily,TitleFontSize = @TitleFontSize,TitleFontStyle = @TitleFontStyle,TitleFontWeight = @TitleFontWeight,TitleTextDecoration = @TitleTextDecoration,SectionTitleForegroundColor = @SectionTitleForegroundColor,SectionTitleBackgroundColor = @SectionTitleBackgroundColor,SectionTitleFontFamily = @SectionTitleFontFamily,SectionTitleFontSize = @SectionTitleFontSize,SectionTitleFontStyle = @SectionTitleFontStyle,SectionTitleFontWeight = @SectionTitleFontWeight,SectionTitleTextDecoration = @SectionTitleTextDecoration,ParagraphForegroundColor = @ParagraphForegroundColor,ParagraphBackgroundColor = @ParagraphBackgroundColor,ParagraphFontFamily = @ParagraphFontFamily,ParagraphFontSize = @ParagraphFontSize,ParagraphFontStyle = @ParagraphFontStyle,ParagraphFontWeight = @ParagraphFontWeight,ParagraphTextDecoration = @ParagraphTextDecoration,logo = @logo                            
WHERE
    DocumentId = @DocumentId
GO

GRANT EXECUTE on kii.p_UpdateDocumentStyle TO p_role_kii
GO

解决方法

修改你的路线
logo = COALESCE(@logo,logo)

COALESCE将为logo分配一个值:@logo如果已填充,否则它将分配logo的现有值

相关文章

(一)日志传送架构 (1.1)相关服务器 主服务器 :用于生产的服务器,上面运行这生产SQL Server数据库...
(一)事故背景 最近在SQL Server 2012生产数据库上配置完事物复制(发布订阅)后,生产数据库业务出现了...
(一)测试目的 目前公司使用的SQL SERVER 2012高可用环境为主备模式,其中主库可执行读写操作,备库既...
(一)背景个人在使用sql server时,用到了sql server的发布订阅来做主从同步,类似MySQL的异步复制。在...
UNION和OR谓词 找出 product 和 product2 中售价高于 500 的商品的基本信息. select * from product wh...
datawhale组队学习task03