更新同一表中的列

时间:2016-10-27 05:15:01

标签: sql oracle

我想更新表分支中的BRCD_NEW列,并在同一个表中的另一列BRCD上应用条件,这是我的代码,但它返回错误

  

单行子查询返回多行

update branches set brcd_new=(
select 
case 
when BRCD between '5000' and '5999' then  CONCAT('PK001',BRCD)
else CONCAT('PK002',BRCD)
end 
from branches);

1 个答案:

答案 0 :(得分:1)

您不需要子查询来实现您正在做的事情。使用CASE语句获取所需的值,并在SET语句中分配到您的列:

update branches 
set brcd_new =
    case 
        when BRCD between '5000' and '5999' then  CONCAT('PK001',BRCD)
        else CONCAT('PK002',BRCD)
    end 
-- WHERE <your filters (if needed)>