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

Sql 如果存储过程中的参数为null,则不更新列,sql,sql-server-2008,stored-procedures,Sql,Sql Server 2008,Stored Procedures,我有以下存储过程 每当@Logo为空时,当前值将被擦除。如果@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

我有以下存储过程

每当
@Logo
为空时,当前值将被擦除。如果
@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)

将为徽标分配一个值,该值为:@Logo(如果已填充),否则将分配Logo的现有值

您应该能够使用case语句:

Logo = CASE WHEN (@Logo is null) 
            THEN Logo
       ELSE @Logo
     END

我同意上一个答案中的@coalesce更好。Case语句也很有用,我想,我在最近的一些数据库升级脚本中已经使用了很多。