当前位置:Gxlcms > 数据库问题 > oracle多个结果集拼接字符串;where id in 字符串 (转)

oracle多个结果集拼接字符串;where id in 字符串 (转)

时间:2021-07-01 10:21:17 帮助过:3人阅读

c_wg cur_wgys%rowtype;

使用游标:

for c_wg in cur_wgys loop
                dbms_output.put_line(c_wg.lastname);
                waiguanstr := waiguanstr ||‘,‘|| c_wg.lastname;
      end loop;
            dbms_output.put_line(ltrim(waiguanstr,‘,‘));

 

这样整体也就解决了这两个问题;在执行触发器的时候都没问题;但在触发时会对这

cursor cur_wgys is select lastname  from hrmresource where id in (waigaunyanshourens);

行,报“无效数字”的错误;

第二种思路:由于第一种思路老是不知道什么原因,也就另寻他路;

select ltrim(max(sys_connect_by_path(lastname, ‘,‘)),‘,‘) 

from (select lastname, rownum t  from hrmresource where  id in(waigaunyanshourens)           connect by prior t = t - 1  start with t = 1;

此种方法比较简洁;不要要游标;直接就可以接收多个结果集,并且将结果集自动拼接成用逗号隔开的字符串;

运用此种发放正常理解这是没问题的;可是也是这样在第二行报“无效数字”。

 

综上两种方法:给自己的感觉是这两种方法都是正确的,不过还是哪的细节没注意,导致报错;

寻找原因发现:id类型是INTEGER;而这个waigaunyanshourens(41,42,43)是一个字符串;

现在问题也找出来了,也试了各种方法,最终终于解决了,对此一番折腾,在此记录,共大家参考!!!

 

第一种解决方法:

定义游标:

cursor cur_wgys is select lastname  from hrmresource where 

INSTR(   (select  ‘,‘ ||   waigaunyanshouren  || ‘,‘   from formtable_main_112 where requestid = :new.requestid),    ‘,‘ ||  TRIM(TO_CHAR(id ))  || ‘,‘  )  > 0;

 c_wg cur_wgys%rowtype;

使用游标:

            for c_wg in cur_wgys loop
                dbms_output.put_line(c_wg.lastname);
                waiguanstr := waiguanstr ||‘,‘|| c_wg.lastname;
            end loop;


                 dbms_output.put_line(ltrim(waiguanstr,‘,‘));

输出结果:

人名A

人名B

人名C

人名A,人名B,人名C 

第二种解决方法:

  select ltrim(max(sys_connect_by_path(lastname, ‘,‘)),‘,‘) into waigaunyanshourens 
  from (select lastname, rownum t  from hrmresource where   INSTR(   (select  ‘,‘ ||  waigaunyanshouren  || ‘,‘   from formtable_main_112 where requestid = :new.requestid),   ‘,‘ ||  TRIM(TO_CHAR(id ))  || ‘,‘  )  > 0) 
connect by prior t = t - 1  start with t = 1;

 

  dbms_output.put_line(waigaunyanshourens||‘==============‘);

 

输出结果:人名A,人名B,人名C==============

 

这样就解决了where id in (string)的问题

关键就是where后面这句话,看似挺长挺乱的,不过就是一个类似一个 like,但绝对比like更安全高效;

问题解决心情舒畅,在此总结一下。希望能给遇到同样问题的朋友点帮助。

oracle多个结果集拼接字符串;where id in 字符串 (转)

标签:

人气教程排行