2016-12-06 312 views
2

ORACLE:到目前为止我没有尝试过任何工作。我希望在屏幕上显示select * from my_table的结果。在这种情况下my_table = select table_name from all_tables where owner='ABC' and name like 'ABC%'。表名称是一个加号,但列名是必需的。我可以在几秒钟内用DB2做到这一点,但不能完全转化为Oracle。oracle select *从变量表名称

我尝试:

variable refcur refcursor; 
    declare 
    my_select  varchar2(64); 
    cursor c_tables is 
     select table_name 
      from all_tables 
     where owner='ABC' and table_name like 'ABC%'; 
    begin 
    for x in c_tables 
     loop 
     dbms_output.put_line(x.table_name); 
     my_select := 'select * from ' || x.table_name; 
     open :refcur for my_select; 
     end loop; 

    exception 
    when no_data_found 
    then dbms_output.put_line('Nothing is found'); 
    end; 
/

在我所有的努力,我已经得到了最好的是表不存在 感谢

回答

0

我不知道你是如何登录,但如果你'没有登录为ABC,您需要将该模式与表名一起包含

my_select := 'select * from ' || x.owner || '.' || x.table_name; 

此外,打开游标不会从中获取任何东西,或者在任何地方显示数据。您需要添加逻辑来从光标获取数据,显示数据并关闭光标。而且由于表名不固定,数据库不能提前知道你的行是什么样的,所以你需要熟悉DBMS_SQL包,它用于处理这样的动态SQL。

祝你好运。