0

我有以下条件 sql 语句,它有效.. 但我在 if 部分有一个空语句......我想否定它,以便我可以使用 if 部分的逻辑

    IF @WorkPrescribed = 1 and (@DefectNo1 = @WorkPrescribedDefectNo or @DefectNo2 = @WorkPrescribedDefectNo or @DefectNo3 = @WorkPrescribedDefectNo or @DefectNo4 = @WorkPrescribedDefectNo)
  BEGIN
            -- I don't want this at all.. only reason I have this is so I can have the else condition
  END
    else
        BEGIN
             --My current logic is all here
        END

所以我只想知道如何否定我所拥有的,所以我可以只用 If 做所有事情,而不是使用 else

谢谢

4

5 回答 5

4

也许只是简单

IF NOT(@WorkPrescribed = 1 and (@DefectNo1 = @WorkPrescribedDefectNo or @DefectNo2 = @WorkPrescribedDefectNo or @DefectNo3 = @WorkPrescribedDefectNo or @DefectNo4 = @WorkPrescribedDefectNo))
于 2013-09-30T09:42:31.637 回答
1

你可以简单地用 a 括起来否定逻辑NOT(),或者你可以这样写:

IF @WorkPrescribed != 1 or (@DefectNo1 != @WorkPrescribedDefectNo and @DefectNo2 != @WorkPrescribedDefectNo and @DefectNo3 != @WorkPrescribedDefectNo and @DefectNo4 != @WorkPrescribedDefectNo)
    BEGIN
         --My current logic is all here
    END
于 2013-09-30T09:42:08.893 回答
0

只需NOT“围绕”要否定的声明:

IF NOT (@WorkPrescribed = 1 and (@DefectNo1 = @WorkPrescribedDefectNo or @DefectNo2 = @WorkPrescribedDefectNo or @DefectNo3 = @WorkPrescribedDefectNo or @DefectNo4 = @WorkPrescribedDefectNo))
  BEGIN
            -- I don't want this at all.. only reason I have this is so I can have the else condition
  END
    else
        BEGIN
             --My current logic is all here
        END
于 2013-09-30T09:42:09.943 回答
0

您可以使用IF @WorkPrescribed <> 1而不是=运算符

检查这篇文章。 我应该在 TSQL 中使用 != 或 <> 来表示不相等吗?

或者你可以使用

IF NOT @WorkPrescribed = 1 and 
(@DefectNo1 = @WorkPrescribedDefectNo or 
 @DefectNo2 = @WorkPrescribedDefectNo or 
 @DefectNo3 = @WorkPrescribedDefectNo or 
 @DefectNo4 = @WorkPrescribedDefectNo)
于 2013-09-30T09:43:27.967 回答
0
IF @WorkPrescribed <> 1 
or @WorkPrescribedDefectNo not in (@DefectNo1, @DefectNo2, @DefectNo3, @DefectNo4)
BEGIN
 -- code
END
于 2013-09-30T11:01:44.517 回答