NC工程项目update多个jobname(每个责任人下)的三种sql

如下图
傲游截图20121229141308.png
我这边工作有一个需求,需要把每个jobcdoe长度为6的责任人部(比如这里的尚连飞 部)名字加到其负责的带有“综合”的签名,也就是说让这里的“综合”变成“尚连飞 部综合”
因为责任人有许多,我希望通过update sql完成
我的想法是

update  BD_JOBBASFIL  set  jobname=(select jobname from bd_jobbasfil
where length(jobcode)=6   and jobcode like '0802%')
||jobname  where jobcode like '0802%' and jobname='综合'

 
可是这里括号内的(select jobname from bd_jobbasfil
where length(jobcode)=6   and jobcode like '0802%') 会返回多个责任人名字。update报错。
请教改怎么写
2.png

解决方法1:辰影

这里的and b.jobcode = c.jobcode不能去掉,保证更新的时候一对一

update bd_jobbasfil  c
   set c.jobname = (select a.jobname || b.jobname
                      from (select jobcode, jobname
                              from bd_jobbasfil
                             where length(jobcode) = 6) a,
                           (select substr(jobcode, 1, 6) fcode,
                                   jobcode,
                                   jobname
                              from bd_jobbasfil
                             where jobname = '综合') b
                     where a.jobcode = b.fcode
                       and b.jobcode = c.jobcode)
where c.jobname = '综合'

 解决方法2:ITPUB  yongzhuhe99

http://www.itpub.net/forum.php?mod=viewthread&tid=1752900&page=2#pid20810305

他的解决方法是写PL/SQL 块,申请的是居然可以在PL/SQL Developer的sql window执行

这里需要注意的是l_jobcode:=substr(to_char(cur.jobcode),1,6);里的jobcode是字符型,需要to_char一下,否则提示未找到任何数据。

declare
l_jobcode bd_jobbasfil.jobcode%type;
l_jobname bd_jobbasfil.jobname%type;
begin
for cur in (select * from bd_jobbasfil where jobname='综合')
loop
l_jobcode:=substr(to_char(cur.jobcode),1,6);
select jobname into l_jobname from bd_jobbasfil
where jobcode=l_jobcode;
update bd_jobbasfil set jobname=l_jobname||jobname
where jobcode=cur.jobcode;
end loop;
end;

  解决方法3:ITPUB ssqtjf

这个最简练,核心思想是把bd_jobmnfil重命名,作为两个表处理

update bd_jobbasfil b1
      set b1.jobname = (select b2.jobname||'综合'
                          from bd_jobbasfil b2
                         where b2.jobcode = substr(b1.jobcode, 1, 6))
    where b1.jobname = '综合'
原文地址:https://www.cnblogs.com/sumsen/p/2840355.html