oracle的cols,Oracle cols_as_rows 比对数据

这篇博客介绍了Oracle中将查询结果转换为行的两个函数`cols_as_rows`和`cols_as_rows8i`,它们分别用于不同版本的数据库。这两个函数解析并执行SQL查询,然后以管道方式或创建临时数据结构返回结果。通过示例展示了如何使用这些函数对比不同员工的数据。
摘要由CSDN通过智能技术生成

AskTom提供的脚本,用于比对数据.

create or replace type myscalartype as object

( rnum number, cname varchar2(30), val varchar2(4000) )

/

create or replace type mytabletype as table of myscalartype

/

create or replace

function cols_as_rows( p_query in varchar2 ) return mytabletype

-- This function is designed to be installed ONCE per database, and

-- it is nice to have ROLES active for the dynamic sql, hence the

-- AUTHID CURRENT_USER.

authid current_user

-- This function is a pipelined function, meaning that it'll send

-- rows back to the client before getting the last row itself.

-- In 8i, we cannot do this.

pipelined

as

l_thecursor     integer default dbms_sql.open_cursor;

l_columnvalue   varchar2(4000);

l_status        integer;

l_colcnt        number default 0;

l_desctbl       dbms_sql.desc_tab;

l_rnum          number := 1;

begin

-- Parse, describe and define the query. Note, unlike print_table,

-- I am not altering the session in this routine. The

-- caller would use to_char() on dates to format and if they

-- want, they would set cursor_sharing. This routine would

-- be called rather infrequently. I did not see the need

-- to set cursor sharing therefore.

dbms_sql.parse( l_thecursor, p_query, dbms_sql.native );

dbms_sql.describe_columns( l_thecursor, l_colcnt, l_desctbl );

for i in 1 .. l_colcnt loop

dbms_sql.define_column( l_thecursor, i, l_columnvalue, 4000 );

end loop;

-- Now, execute the query and fetch the rows. iterate over

-- the columns and "pipe" each column out as a separate row

-- in the loop. Increment the row counter after each

-- dbms_sql row.

l_status := dbms_sql.execute(l_thecursor);

while ( dbms_sql.fetch_rows(l_thecursor) > 0 )

loop

for i in 1 .. l_colcnt

loop

dbms_sql.column_value( l_thecursor, i, l_columnvalue );

pipe row

(myscalartype( l_rnum, l_desctbl(i).col_name, l_columnvalue ));

end loop;

l_rnum := l_rnum+1;

end loop;

-- Clean up and return...

dbms_sql.close_cursor(l_thecursor);

return;

end cols_as_rows;

/

create or replace function

cols_as_rows8i( p_query in varchar2 ) return mytabletype

authid current_user

as

l_thecursor     integer default dbms_sql.open_cursor;

l_columnvalue   varchar2(4000);

l_status        integer;

l_colcnt        number default 0;

l_desctbl       dbms_sql.desc_tab;

l_data          mytabletype := mytabletype();

l_rnum          number := 1;

begin

dbms_sql.parse( l_thecursor, p_query, dbms_sql.native );

dbms_sql.describe_columns( l_thecursor, l_colcnt, l_desctbl );

for i in 1 .. l_colcnt loop

dbms_sql.define_column( l_thecursor, i, l_columnvalue, 4000 );

end loop;

l_status := dbms_sql.execute(l_thecursor);

while ( dbms_sql.fetch_rows(l_thecursor) > 0 )

loop

for i in 1 .. l_colcnt

loop

dbms_sql.column_value( l_thecursor, i, l_columnvalue );

l_data.extend;

l_data(l_data.count) :=

myscalartype( l_rnum, l_desctbl(i).col_name, l_columnvalue );

end loop;

l_rnum := l_rnum+1;

end loop;

dbms_sql.close_cursor(l_thecursor);

return l_data;

end cols_as_rows8i;

/

以HR表为例,比对员工编号200和201的员工数据

column val format a20;

select a.cname,a.val,b.val from

table(cols_as_rows('select * from hr.employees where employee_id=200')) a,

table(cols_as_rows('select * from hr.employees where employee_id=201')) b

where a.cname=b.cname and (a.val is not null or b.val is not null)

order by a.cname;

b8984d08802d9ae82e1e262f1ebf5ae7.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值