我有以下条件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
由于
答案 0 :(得分:4)
也许只是
IF NOT(@WorkPrescribed = 1 and (@DefectNo1 = @WorkPrescribedDefectNo or @DefectNo2 = @WorkPrescribedDefectNo or @DefectNo3 = @WorkPrescribedDefectNo or @DefectNo4 = @WorkPrescribedDefectNo))
答案 1 :(得分:1)
您可以通过用NOT()
括起来简单地否定逻辑,或者您可以这样写:
IF @WorkPrescribed != 1 or (@DefectNo1 != @WorkPrescribedDefectNo and @DefectNo2 != @WorkPrescribedDefectNo and @DefectNo3 != @WorkPrescribedDefectNo and @DefectNo4 != @WorkPrescribedDefectNo)
BEGIN
--My current logic is all here
END
答案 2 :(得分: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
答案 3 :(得分:0)
您可以使用IF @WorkPrescribed <> 1
代替=
运营商
查看这篇文章。 Should I use != or <> for not equal in TSQL?
或者您可以使用
IF NOT @WorkPrescribed = 1 and
(@DefectNo1 = @WorkPrescribedDefectNo or
@DefectNo2 = @WorkPrescribedDefectNo or
@DefectNo3 = @WorkPrescribedDefectNo or
@DefectNo4 = @WorkPrescribedDefectNo)
答案 4 :(得分:0)
IF @WorkPrescribed <> 1
or @WorkPrescribedDefectNo not in (@DefectNo1, @DefectNo2, @DefectNo3, @DefectNo4)
BEGIN
-- code
END