微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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的现有值

原文地址:https://www.jb51.cc/mssql/77733.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐