SQL 案例陈述条件
SQL Case statement conditionals
所以我正在尝试使用 case 语句来更新 table 中具有许多特定条件的列。我下面的代码应该很明显我的目标是什么,但我收到一条错误消息:
ERROR: Operand of WHEN clause 1 is not the same data type as the CASE operand.
ERROR: Operand of WHEN clause 3 is not the same data type as the CASE operand.
ERROR: Operand of WHEN clause 4 is not the same data type as the CASE operand.
ERROR: Operand of WHEN clause 5 is not the same data type as the CASE operand.
ERROR: Operand of WHEN clause 6 is not the same data type as the CASE operand.
我认为这是因为 or 语句是逻辑语句而不是字符串,因为它们应该位于右侧?代码如下,如有任何帮助,我们将不胜感激。
update cat_rotn_data
set rotn_ac_typ_cd = case rotn_ac_typ_cd
when '31J' or '320' or '321' or '319' or'32K' or'32M' or'32R' or'3HF' or'3KR' or'3MR' then '320'
when '717' then '717'
when 'M88' or 'M90' or 'M9K' then 'M88'
when '738' or'739' or '73A' or '73E' or '73W' or '73H' or '700' then '73N'
when '332' or '333' or '33X' or '339' or '330' then '330'
when '757' or '75C' or '75D' or '75G' or '75H' or '75P' or '75S' or '75Y' or '76L' or '76P' or '76Q' or '76T' or '76Z' or '75N' or'7ER' then '7ER'
when '777' or '77B' or '7HD' or '7HB' or '7CD' or '7CB' then '777'
else 'ERROR'
end;
每个 when
条件都需要一个布尔表达式:
set rotn_ac_typ_cd = case
(case when rotn_ac_typ_cd in ('31J', '320', '321', '319', '32K', '32M', '32R', '3HF', '3KR', '3MR')
then '320'
when rotn_ac_typ_cd in ('717')
then '717'
. . .
else 'ERROR'
end);
or
-- 在 SQL 中 -- 连接布尔表达式,你用它来连接字符串,这会导致你的错误。
您可以在语句中使用 IN 而不是使用多个 OR。在更新语句中,当 rotn_ac_typ_cd 值为 'M90' 或 'M9K' 或 'M88' 时,您期望 'M88'。您只能为一个 WHEN 语句提供一张支票。如果您仍然想使用 OR 那么您可以执行以下操作
update cat_rotn_data
set rotn_ac_typ_cd = case WHEN rotn_ac_typ_cd= 'M88' THEN 'M88'
WHEN rotn_ac_typ_cd= 'M0' THEN 'M88'
WHEN rotn_ac_typ_cd= 'M9K' THEN 'M88'
else ... end
但这会使您的查询变大,您可以简单地使用 IN。
update cat_rotn_dat
set rotn_ac_typ_cd = case WHEN rotn_ac_typ_cd IN ('M88', 'M90', 'M9K') THEN 'M88'
else ... end
所以我正在尝试使用 case 语句来更新 table 中具有许多特定条件的列。我下面的代码应该很明显我的目标是什么,但我收到一条错误消息:
ERROR: Operand of WHEN clause 1 is not the same data type as the CASE operand.
ERROR: Operand of WHEN clause 3 is not the same data type as the CASE operand.
ERROR: Operand of WHEN clause 4 is not the same data type as the CASE operand.
ERROR: Operand of WHEN clause 5 is not the same data type as the CASE operand.
ERROR: Operand of WHEN clause 6 is not the same data type as the CASE operand.
我认为这是因为 or 语句是逻辑语句而不是字符串,因为它们应该位于右侧?代码如下,如有任何帮助,我们将不胜感激。
update cat_rotn_data
set rotn_ac_typ_cd = case rotn_ac_typ_cd
when '31J' or '320' or '321' or '319' or'32K' or'32M' or'32R' or'3HF' or'3KR' or'3MR' then '320'
when '717' then '717'
when 'M88' or 'M90' or 'M9K' then 'M88'
when '738' or'739' or '73A' or '73E' or '73W' or '73H' or '700' then '73N'
when '332' or '333' or '33X' or '339' or '330' then '330'
when '757' or '75C' or '75D' or '75G' or '75H' or '75P' or '75S' or '75Y' or '76L' or '76P' or '76Q' or '76T' or '76Z' or '75N' or'7ER' then '7ER'
when '777' or '77B' or '7HD' or '7HB' or '7CD' or '7CB' then '777'
else 'ERROR'
end;
每个 when
条件都需要一个布尔表达式:
set rotn_ac_typ_cd = case
(case when rotn_ac_typ_cd in ('31J', '320', '321', '319', '32K', '32M', '32R', '3HF', '3KR', '3MR')
then '320'
when rotn_ac_typ_cd in ('717')
then '717'
. . .
else 'ERROR'
end);
or
-- 在 SQL 中 -- 连接布尔表达式,你用它来连接字符串,这会导致你的错误。
您可以在语句中使用 IN 而不是使用多个 OR。在更新语句中,当 rotn_ac_typ_cd 值为 'M90' 或 'M9K' 或 'M88' 时,您期望 'M88'。您只能为一个 WHEN 语句提供一张支票。如果您仍然想使用 OR 那么您可以执行以下操作
update cat_rotn_data
set rotn_ac_typ_cd = case WHEN rotn_ac_typ_cd= 'M88' THEN 'M88'
WHEN rotn_ac_typ_cd= 'M0' THEN 'M88'
WHEN rotn_ac_typ_cd= 'M9K' THEN 'M88'
else ... end
但这会使您的查询变大,您可以简单地使用 IN。
update cat_rotn_dat
set rotn_ac_typ_cd = case WHEN rotn_ac_typ_cd IN ('M88', 'M90', 'M9K') THEN 'M88'
else ... end