以下存储过程无法捕捉到正确的条件。如果我提交两个图像,它会正确地写入两个图像。如果我上传1张图片并留空,则会将空白保存为空,覆盖现有数据。最后,如果我将两者都留空,则会保留空白覆盖现有数据。任何帮助都会很棒。感谢。
ALTER PROCEDURE fstage.spUpdateCMSTrendLanding
(
@TrendLandingText nvarchar(max),
@TrendGuideLandingImage1 nvarchar(max),
@TrendGuideLandingImage2 nvarchar(max)
)
AS
BEGIN
SET NOCOUNT ON;
IF @TrendGuideLandingImage1 IS NULL AND @TrendGuideLandingImage2 IS NULL
BEGIN
UPDATE fstage.staticCMS SET TrendGuideLanding = @TrendLandingText;
END
If (@TrendGuideLandingImage1 IS NOT NULL) AND (@TrendGuideLandingImage2 IS NOT NULL)
BEGIN
UPDATE fstage.staticCMS SET TrendGuideLanding = @TrendLandingText, TrendGuideLandingImage1=@TrendGuideLandingImage1, TrendGuideLandingImage2=@TrendGuideLandingImage2;
END
If (@TrendGuideLandingImage1 IS NOT NULL) AND (@TrendGuideLandingImage2 IS NULL)
BEGIN
UPDATE fstage.staticCMS SET TrendGuideLanding = @TrendLandingText, TrendGuideLandingImage1=@TrendGuideLandingImage1;
END
If (@TrendGuideLandingImage2 IS NOT NULL) AND (@TrendGuideLandingImage1 IS NULL)
BEGIN
UPDATE fstage.staticCMS SET TrendGuideLanding = @TrendLandingText, TrendGuideLandingImage2=@TrendGuideLandingImage2;
END
END
答案 0 :(得分:2)
你的NULL是空字符串。不是数据库NULL值。空字符串是一个值。
这意味着代码始终使用相同的条件
在proc的顶部添加:
SELECT
@TrendGuideLandingImage1 = NULLIF(@TrendGuideLandingImage1, ''),
@TrendGuideLandingImage2 = NULLIF(@TrendGuideLandingImage2, '')
或添加默认值,不要在客户端代码中设置参数
ALTER PROCEDURE fstage.spUpdateCMSTrendLanding
@TrendLandingText nvarchar(max),
@TrendGuideLandingImage1 nvarchar(max) = NULL,
@TrendGuideLandingImage2 nvarchar(max) = NULL
AS
答案 1 :(得分:1)
尝试使用ISNull
IF IsNull(@TrendGuideLandingImage1,"") AND IsNull(@TrendGuideLandingImage2,"") =""
BEGIN
UPDATE fstage.staticCMS SET TrendGuideLanding = @TrendLandingText;
END
答案 2 :(得分:0)
您可以使用UPDATE
和NULLIF
将您的程序重写为单个ISNULL
语句。
CREATE PROCEDURE fstage.spUpdateCMSTrendLanding
(
@TrendLandingText nvarchar(max),
@TrendGuideLandingImage1 nvarchar(max),
@TrendGuideLandingImage2 nvarchar(max)
)
AS
UPDATE fstage.staticCMS
SET TrendGuideLanding = @TrendLandingText
,TrendGuideLandingImage1
= ISNULL(NULLIF(@TrendGuideLandingImage1, ''), TrendGuideLandingImage1)
, TrendGuideLandingImage2
= ISNULL(NULLIF(@TrendGuideLandingImage2, ''), TrendGuideLandingImage2)