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

标签 sql sql-server-2008 stored-procedures

我有以下存储过程。

只要@Logo为null,就会删除当前值。如果Logo@Logo,我不想更新NULL的值。

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

关于sql - 如果存储过程中的参数为null,则不更新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6391054/

相关文章:

sql - 在SQLite中用完PRIMARY KEY的INT数据类型

c# - 从 SQL 查询返回参数

MySQL - 如何使用左连接从 on 子句中的连接表中精确获取一行?

java - 将字符串或整数数组转换为逗号分隔的整数或条件中的 sql 字符串

sql-server - 将小数时间转换为小时和分钟

sql-server-2008 - 如何在 SQL Server 2008 中使用临时表从复杂查询中获取单个 SQL 查询

C# - 使用存储过程插入多行

stored-procedures - 如何在现有程序包中创建新的存储过程

c# - 存储过程不适用于 c# 代码

c++ - 使用带有 wxWidgets 的 mysql lib 的类崩溃