sql >> データベース >  >> RDS >> Oracle

selectステートメントの動的テーブル名

    参照カーソルを使用できますが、お勧めしません。次のようになります

    create table tab_01 as select 1 a , 10 b from dual;
    create table tab_02 as select 2 a , 20 b from dual;
    create table tab_03 as select 3 a , 30 b from dual;
    
    create or replace function get_all_history
    return sys_refcursor
    as
       r sys_refcursor;
       stmt varchar2(32000);
       cursor c_tables is
               select  table_name
               from    user_tables
               where   table_name like 'TAB_%';
    begin
       for x in c_tables loop
               stmt := stmt || ' select * from ' || x.table_name ||' union all';
       end loop;
       stmt := substr(stmt , 1 , length(stmt) - length('union all'));
       open r for stmt;
       return r;
    end;
    /
    
    SQL> select get_all_history() from dual;
    
    GET_ALL_HISTORY()
    --------------------
    CURSOR STATEMENT : 1
    
    CURSOR STATEMENT : 1
    
             A          B
    ---------- ----------
             1         10
             2         20
             3         30
    


    1. 情報システム、データおよび情報

    2. 古いスタイルの結合で左結合を行う

    3. 新しいAzureSQLデータベースの標準階層サイズ

    4. Oracleストアドプロシージャ内でのテキストの検索