ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 将表格的某一列转换成逗号分隔的字符串的另类方法

将表格的某一列转换成逗号分隔的字符串的另类方法

原创 Linux操作系统 作者:myfriend2010 时间:2019-07-16 21:30:01 0 删除 编辑

将表格的某一列转换成逗号分隔的字符串的另类方法

A:dbms_utility.table_to_comma的使用
一:首先建立测试表
create table table1
(
dz varchar2(100)
)
insert into table1 values('v');
insert into table1 values('dywm');
commit;

SQL> select * from table1;

DZ
--------------------------------------------------------------------------------
v
dywm

二:建立存储过程
create or replace procedure test_column_row as
qq1 dbms_utility.uncl_array;
l_tablen number;
l_tab varchar2(1000);
begin
select dz bulk collect into qq1 from table1;
dbms_utility.table_to_comma(qq1, l_tablen, l_tab);
dbms_output.put_line(l_tab);
end;

三:运行结果如下:
SQL> set serveroutput on
SQL> execute test_column_row;

v,dywm

PL/SQL procedure successfully completed

附:字符串<=>table相互转换的例子
create or replace procedure schema_table as
/*
把字符串变成table
或者把table变成字符串
*/
type vcArray is table of varchar2(4000);
l_names vcArray := vcArray('sswe,oejic,wwer', 'a , b , c',
'e123o,a688j9,d557i5', 'd334a,q225d,w67s7');
l_tablen number;
l_tab dbms_utility.uncl_array;
begin
for i in 1 .. l_names.count
loop
dbms_output.put_line('i_names(' || i || ')=' || l_names(i));
begin
dbms_utility.comma_to_table(l_names(i), l_tablen, l_tab);
for j in 1 .. l_tablen
loop
dbms_output.put_line('l_tab(' || j || ')=' || l_tab(j));
end loop;
l_names(i) := null;
dbms_utility.table_to_comma(l_tab, l_tablen, l_names(i));
dbms_output.put_line(l_names(i));
end;
end loop;
end;


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/694276/viewspace-51414/,如需转载,请注明出处,否则将追究法律责任。

上一篇: 更难的SQL题目!!!
请登录后发表评论 登录
全部评论

注册时间:2018-09-01

  • 博文量
    187
  • 访问量
    143700