CASE WHEN 显示错误 [英] CASE WHEN shows error
问题描述
我正在使用 Microsoft SQL Management Studio,我正在尝试使用 CASE WHEN.这是我的问题,我的代码如下:
Im using Microsoft SQL Management Studio and im trying to use CASE WHEN. Here is my problem, my code as below:
SELECT CASE WHEN INCIDENT_RK = 52377
THEN CASE_RK = NULL
ELSE CASE_RK
END
)
FROM ABC ;
我想要做的是,当INCIDENT_RK为52377时,然后将CASE_RK设置为NULL,否则按照原来的CASE_RK返回.
What I want to do is, when INCIDENT_RK is 52377, then set CASE_RK to NULL else follow back the original CASE_RK.
它应该很简单,但是,INCIDENT_RK、=、ELSE 下有红色下划线,)我觉得这很奇怪,因为我在今天早些时候做了一个 CASE 并且它工作正常.以下是工作代码:
It should be pretty straight forward, however, there are red underlines under INCIDENT_RK, =, ELSE,) I find it weird cause I did a CASE WHEN Earlier today and it is working fine. Below is the working code:
CASE WHEN CASE_RK NOT BETWEEN 1 AND 2
THEN CASE_RK+75961
ELSE CASE_RK
END
)
FROM ABC
需要你的建议
编辑 2:我也尝试了以下代码,但仍然没有运气:
EDIT 2: I Also tried the following code but still no luck:
CASE WHEN INCIDENT_RK = '52080'
THEN NULL
ELSE CASE_RK
END AS CASE_RK
)
FROM ABC
编辑 3:我试过这段代码.
EDIT 3: I Tried this code.
SELECT CASE WHEN INCIDENT_RK = 52080 THEN NULL ELSE CASE_RK END AS CASE_RK FROM ABC
终于可以执行了.但是,我只有 1 行 INCIDENT_RK = 100 但有 50 多个 CASE_RK 设置为 NULL
Finally can execute. However, I only have 1 row of INCIDENT_RK = 100 but there are more than 50 CASE_RK is set to NULL
推荐答案
你的 case 语法看起来不正确.
Your case syntax looks to be incorrect.
case 返回一个语句,它不分配语句.
请注意,您的示例有效和无效的示例之间的区别在于您没有尝试分配 CASE_RK = NULL
A case returns a statement, it doesn't assign the statement.
Notice the difference between your example that works and the one that doesn't is that in the one that doesn't you try to assign CASE_RK = NULL
因此,根据查询的其余代码,它应该是这样的:
So depending on the rest of the code of your query it should be something along the line of:
SELECT CASE WHEN INCIDENT_RK = 52377
THEN NULL
ELSE CASE_RK
END AS CASE_RK
FROM ABC
这篇关于CASE WHEN 显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!