我正在使用SQL Server 2008 R2
我的SQL语句是这样的,因为我在插入之前避免了双重数据:
select * from LETTRE_VOIT_TEMP where NOT EXISTS
(
select * from LETTRE_VOIT_FINAL lv
where lv.NOID = LETTRE_VOIT_TEMP.NOID AND
lv.CODE_DEST = LETTRE_VOIT_TEMP.CODE_DEST AND
lv.CODE_CLIENT = LETTRE_VOIT_TEMP.CODE_CLIENT AND
lv.DATE_CLOTURE = LETTRE_VOIT_TEMP.DATE_CLOTURE AND
lv.DATE_CLOTUR_REEL = LETTRE_VOIT_TEMP.DATE_CLOTUR_REEL
)
这很好,但现在我想添加一个条件:
LETTRE_VOIT_TEMP.DATE_CLOTURE is null
时,我不想比较。LETTRE_VOIT_TEMP.DATE_CLOTUR_REEL is null
时,我不想比较我改变了这样:
select * from LETTRE_VOIT_TEMP where NOT EXISTS
(
select * from LETTRE_VOIT_FINAL lv
where lv.NOID = LETTRE_VOIT_TEMP.NOID AND
lv.CODE_DEST = LETTRE_VOIT_TEMP.CODE_DEST AND
lv.CODE_CLIENT = LETTRE_VOIT_TEMP.CODE_CLIENT AND
case LETTRE_VOIT_TEMP.DATE_CLOTURE
when is not null then lv.DATE_CLOTURE = LETTRE_VOIT_TEMP.DATE_CLOTURE AND
end
case LETTRE_VOIT_TEMP.DATE_CLOTUR_REEL
when is not null then lv.DATE_CLOTUR_REEL = LETTRE_VOIT_TEMP.DATE_CLOTUR_REEL
end
)
我收到语法错误。
答案 0 :(得分:0)
您的CASE声明不正确。正确的语法是CASE WHEN {condition} THEN {result} END
SELECT *
FROM LETTRE_VOIT_TEMP where NOT EXISTS
(
SELECT *
FROM LETTRE_VOIT_FINAL lv
WHERE lv.NOID = LETTRE_VOIT_TEMP.NOID
AND lv.CODE_DEST = LETTRE_VOIT_TEMP.CODE_DEST
AND lv.CODE_CLIENT = LETTRE_VOIT_TEMP.CODE_CLIENT
AND CASE WHEN LETTRE_VOIT_TEMP.DATE_CLOTURE IS NOT NULL THEN lv.DATE_CLOTURE = LETTRE_VOIT_TEMP.DATE_CLOTURE END
AND CASE WHEN LETTRE_VOIT_TEMP.DATE_CLOTUR_REEL IS NOT NULL THEN lv.DATE_CLOTUR_REEL = LETTRE_VOIT_TEMP.DATE_CLOTUR_REEL END
)
您可能需要在1 = 1
的CASE中添加ELSE语句,以避免出现其他语法错误
答案 1 :(得分:0)
您无法以此格式测试null
CASE field WHEN IS NULL THEN x
但你可以这样测试:
CASE WHEN field IS NULL THEN x
应该注意的是,在SQL Server中,您可以使用以下语法,而不是使用NULL
CASE field WHEN condition1 THEN x
WHEN condition2 THEN y
ELSE z
END