我要执行    select * from a01.temp
            select * from a02.temp
                   .                                               
                   .
                   .
            select * from a500.temp
有没有办法定义一个变量@a,然后直接从a01到a500,然后我FOR @a = a01 to a500,select * from @a.temp就可以在plsql看到我要的效果,这个要怎么写呢?

解决方案 »

  1.   

    pl/sql正常情况下不提供select的数据至屏幕,所以可以先利用spool和pl/sql块中使用for循环生成需要的sql脚本然后再调用此sql脚本实现数据输出的功能set serveroutput on
    spool my.sql
    declare
      v_int ......
    begin
      for ... loop
        dbms_output.put_line('select * from a'||v_int||'.temp;');
        ...
      end loop;
    end;
    /
    spool offstart my.sql
      

  2.   

    create or replace procedure test1
    is
    a   varchar2(10);
    begin
        for i in 1..500 loop    
            a:='';   
            a:=a||i;
            select * from a.temp;
        end loop;
    end test1;
      

  3.   

    create or replace procedure test1
    is
    a   varchar2(10);
    begin
        for i in 1..500 loop    
            a:='';   
            a:=a||i;
            select * from a.temp;
        end loop;
    end test1;不要误人,,过程里面能直接单独使用 select 语句吗?
      

  4.   

    1楼已经说的很详细了,,不了解 语法 就去google了解spool
      

  5.   


    --先建个表存放结果,然后把查到德数据插入
    create table test as select * from a01.temp where 1<>1;
    DECLARE
      V_TNAME VARCHAR2(10);
    BEGIN
      FOR V_CNT IN 1 .. 500 LOOP
        IF V_CNT < 10 THEN
          V_TNAME := '00' || V_CNT || '.temp';
        ELSIF V_CNT >= 10 AND V_CNT < 100 THEN
          V_TNAME := '0' || V_CNT || '.temp';
        ELSE
          V_TNAME := V_CNT || '.temp';
        END IF;
        --DBMS_OUTPUT.PUT_LINE('insert into test select * from '||v_tname||';');
        EXECUTE IMMEDIATE 'insert into test select * from '||v_tname||';'
      END LOOP;
    END;
      

  6.   

    create or replace procedure test1
    is
    a   varchar2(10);
    begin
        for i in 1..500 loop    
            a:='';   
            a:=a||i;
            select * from a.temp;
        end loop;
    end test1;不要误人,,过程里面能直接单独使用 select 语句吗?
    楼主是要将a500.temp表中每一项输出吗?create or replace procedure test1 is
      a  varchar2(100);
      aa varchar2(100);
      cursor dd is select 'aa' aa from a;
    begin
      for i in 1 .. 500 loop
        a := '';
        a := a || lpad(i, 3, '0');
        open dd;
        loop
          fetch dd
            into aa;
          exit when dd%notfound;
          dbms_output.put_line('aa:' || aa);
        end loop;
        close dd;
      end loop;
    exception
      when others then
        rollback;
    end test1;
      

  7.   

    create or replace procedure test1
    is
    a   varchar2(10);
    begin
        for i in 1..500 loop    
            a:='';   
            a:=a||i;
            select * from a.temp;
        end loop;
    end test1;不要误人,,过程里面能直接单独使用 select 语句吗?
    真的是误人,艹!!!
      

  8.   

    如果不是很长 可以直接打印出来再执行  
    create or replace procedure test
    as
     t_sql varchar2(4000);
    begin
      for i in 1..10 LOOP
        t_sql := t_sql || 'select * from a'|| lpad(i,3,'0')||'.temp union all ';
      end LOOP;   
      t_sql := substr(t_sql,1,length(t_sql)-10);
      dbms_output.put_line(t_sql); 
    end test; 
    500个 太长了 可以考虑建表 将500个表的数据 循环插入到新表 然后直接访问新表就可以了