免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4200 | 回复: 2
打印 上一主题 下一主题

根据表名动态获取表的数据的pl sql问题? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-12-30 11:35 |只看该作者 |倒序浏览
我根据变量中的表名,查询user_tab_columns已经获得了表的所有列名称,并存储在cols的联合数组中
接下来,我用循环拼接出了类似 select col1,col2,col3 from tablename 的查询语句
但问题是,我用游标变量open这个查询后,该怎么把各列值存到变量里呢?因为列数是根据表不同而不同的

我原来想这么做:
open cur for dsql; --cur为游标变量,dsql为拼接出来的查询语句
--col1_table,col2_table,col3_table为联合数组,一个代表一个列
fetch cur bulk collect into col1_table,col2_table,col3_table;
close cur;
但是,根据传进来的表不同,列数是不同的,因此,这里就没法在这写col1_table,col2_table,col3_table,或更多的联合数据变量
该用什么方法,才能实现把各列值都存到变量里阿?

注:
我的环境为oracle9i 9.2.0.8

论坛徽章:
0
2 [报告]
发表于 2006-12-31 05:37 |只看该作者
Study the following code from Tom Kyte.  I think it can help you.


/*
   example: exec print_table( 'select * from v$database')
*/

create or replace procedure print_table( p_query in varchar2 )
AUTHID CURRENT_USER
is
    l_theCursor     integer default dbms_sql.open_cursor;
    l_columnValue   varchar2(4000);
    l_status        integer;
    l_descTbl       dbms_sql.desc_tab;
    l_colCnt        number;
begin
    execute immediate
    'alter session set
        nls_date_format=''dd-mon-yyyy hh24:mi:ss'' ';

    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 );
            dbms_output.put_line
            ( rpad( l_descTbl(i).col_name, 30 )
              || ': ' ||
              l_columnValue );
        end loop;
        dbms_output.put_line( '-----------------' );
    end loop;
    execute immediate
        'alter session set nls_date_format=''dd-MON-rr'' ';
exception
    when others then
      execute immediate
          'alter session set nls_date_format=''dd-MON-rr'' ';
      raise;
end;
/

论坛徽章:
0
3 [报告]
发表于 2006-12-31 18:12 |只看该作者
原帖由 sshd 于 2006-12-31 05:37 发表
Study the following code from Tom Kyte.  I think it can help you.


/*
   example: exec print_table( 'select * from v$database')
*/

create or replace procedure print_table( p_query in varch ...

Thanks sshd, that is what i need.
but i wonder if there is another way to do this using Native Dynamic SQL?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP