0

I have the following conditional sql statement, which works.. but I have an empty statement in the if part... I want to negate it so that I can just use the if part with the logic

    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

so I just want to know how to negate what I have, so I can do everything just with an If, rather than use the else

Thanks

user2206329
  • 2,722
  • 10
  • 49
  • 77

5 Answers5

4

Perhaps simply

IF NOT(@WorkPrescribed = 1 and (@DefectNo1 = @WorkPrescribedDefectNo or @DefectNo2 = @WorkPrescribedDefectNo or @DefectNo3 = @WorkPrescribedDefectNo or @DefectNo4 = @WorkPrescribedDefectNo))
Tim Schmelter
  • 429,027
  • 67
  • 649
  • 891
1

You could simply negate the logic by enclosing with a NOT(), or you could write like this:

IF @WorkPrescribed != 1 or (@DefectNo1 != @WorkPrescribedDefectNo and @DefectNo2 != @WorkPrescribedDefectNo and @DefectNo3 != @WorkPrescribedDefectNo and @DefectNo4 != @WorkPrescribedDefectNo)
    BEGIN
         --My current logic is all here
    END
Mitch Wheat
  • 288,400
  • 42
  • 452
  • 532
0

Just put NOT "around" the statement to be negated:

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
DrCopyPaste
  • 3,913
  • 1
  • 21
  • 55
0

You can use IF @WorkPrescribed <> 1 instead of = operator

check this post. Should I use != or <> for not equal in TSQL?

or you can use

IF NOT @WorkPrescribed = 1 and 
(@DefectNo1 = @WorkPrescribedDefectNo or 
 @DefectNo2 = @WorkPrescribedDefectNo or 
 @DefectNo3 = @WorkPrescribedDefectNo or 
 @DefectNo4 = @WorkPrescribedDefectNo)
Community
  • 1
  • 1
Arvinder
  • 9
  • 2
0
IF @WorkPrescribed <> 1 
or @WorkPrescribedDefectNo not in (@DefectNo1, @DefectNo2, @DefectNo3, @DefectNo4)
BEGIN
 -- code
END
t-clausen.dk
  • 42,087
  • 11
  • 52
  • 90