拼接查詢sql中指定列的結果集

2021-08-31 23:19:16 字數 2917 閱讀 8856

函式適用於:需要將指定查詢sql中的某列拼接成以指定字元分隔連線的字串。

不足:因函式的返回值為varchar2,且通常拼接得到的字串會用於查詢sql(因用在sql中,不能大於varchar2的4000個字元限制)中,因此限制了返回長度小於等於4000

create or replace function f_con_colunn_set(p_sql in varchar2,

--傳入的sql語句

p_column_index integer default 1,

--需要連線的字段索引位置(從1開始,預設為1)

p_con_str in varchar2 default ','

--連線時的分隔符,預設為逗號

) return varchar2 is

/*適用於需要將sql語句的某列查詢結果(大於一條記錄)拼接成乙個字段返回,

限制了返回長度小於等於4000(因用在sql中,不能大於varchar2的4000個字元限制)

*/v_value varchar2(4000);

v_return varchar2(8000) := '';

sqlstr_cursor integer;

v_ignore integer;

v_name integer;

v_col_count integer;

v_column_desc dbms_sql.desc_tab;

begin

v_name := dbms_sql.open_cursor;

dbms_sql.parse(v_name, p_sql, dbms_sql.native);

dbms_sql.describe_columns(v_name, v_col_count, v_column_desc);

dbms_sql.close_cursor(v_name);

if p_column_index > 0 and p_column_index <= v_col_count then

sqlstr_cursor := dbms_sql.open_cursor;

dbms_sql.parse(sqlstr_cursor, p_sql, dbms_sql.native);

dbms_sql.define_column(sqlstr_cursor,

p_column_index,

v_column_desc(p_column_index).col_name,

4000);

v_ignore := dbms_sql.execute(sqlstr_cursor);

loop

if dbms_sql.fetch_rows(sqlstr_cursor) > 0 then

dbms_sql.column_value(sqlstr_cursor, p_column_index, v_value);

if length(v_return || v_value) > 4000 then

exit;

else

v_return := v_return || p_con_str || v_value;

end if;

else

exit;

end if;

end loop;

v_return := substr(v_return, length(p_con_str) + 1, 4000);

dbms_sql.close_cursor(sqlstr_cursor);

end if;

return v_return;

exception

when others then

if dbms_sql.is_open(v_name) then

dbms_sql.close_cursor(v_name);

end if;

if dbms_sql.is_open(sqlstr_cursor) then

dbms_sql.close_cursor(sqlstr_cursor);

end if;

return '';

end f_con_colunn_set;

--測試方法

--1)只傳sql,預設返回col1的以逗號分隔連線值:test1,test2

select f_con_colunn_set('select ''test1'' col1,1 col2 from dual union all select ''test2'' col1,2 col2 from dual')

from dual;

--2)傳sql,傳col2的索引值(從1開始),返回col2以逗號分隔的連線值: 1,2

select f_con_colunn_set('select ''test1'' col1,1 col2 from dual union all select ''test2'' col1,2 col2 from dual',

2)from dual;

--3)傳sql,傳col2的索引值(從1開始),分隔值';',返回col2以分號號分隔的連線值1;2

select f_con_colunn_set('select ''test1'' col1,1 col2 from dual union all select ''test2'' col1,2 col2 from dual',

2,';')

from dual;

--4)傳sql,傳索引值(從1開始)3,傳入的索引值超過sql的列索引集合,不存在此列,返回空值

select f_con_colunn_set('select ''test1'' col1,1 col2 from dual union all select ''test2'' col1,2 col2 from dual',

3)from dual;

拼接查詢sql中指定列的結果集

函式適用於 需要將指定查詢sql中的某列拼接成以指定字元分隔連線的字串。不足 因函式的返回值為varchar2,且通常拼接得到的字串會用於查詢sql 因用在sql中,不能大於varchar2的4000個字元限制 中,因此限制了返回長度小於等於4000 create or replace functi...

sql 查詢結果的拼接 填充

一 拼接函式 1 oracle select concat str1,str2 from table 只能有兩個引數 select str1 str2 strn from table 任意數量引數 2 mysql select concat str1,str2,strn from table 二 填...

sql server 查詢結果集 列 轉行 過程

該方法,實現了,sql server 查詢出的結果集,先放入臨時表,通過臨時表,把列轉為行。1.把結果放入臨時表 select into tab from dbo.operatedefine 2.定義返回表變數 declare rettable table rowid int,values nvar...