向 IF 语句添加第二行使其无条件执行

问题描述 投票:0回答:1
IF cast(DATEDIFF(DAY, @pd_startDate, @pd_EndDate) as smallint) > 7
  PRINT 'A period larger than 7 days can be used only for Raw data.'

按预期工作 但是

    IF cast(DATEDIFF(DAY, @pd_startDate, @pd_EndDate) as smallint) > 7
    begin
        PRINT 'A period larger than 7 days can be used only for Raw data.'
        RETURN
    end

没有。 即使 datediff = 7,代码也会打印

我尝试了各种方案将 datediff 转换为 int、smallint 等 没有成功

这2个参数可以是 @pd_StartDate = '09/17/2023' ,@pd_EndDate = '09/24/2023'

sql-server datediff
1个回答
0
投票

这只是我的猜测,但由于您没有在 IF 语句中使用括号,因此看起来它使用缩进来查看哪些属于 IF 语句,哪些不属于。

所以我的猜测是在第二个代码片段中,IF 语句只是空的,开始又是独立的。

您是否尝试过缩进开头和结尾,然后进一步缩进该选项卡的内容?

© www.soinside.com 2019 - 2024. All rights reserved.