有个问题请教高手们:
比如 表a中某个字段放的是表名称(a1,a2,a3,a4....),(a1,a2,a3,a4....)这几个表是实际存在的,现在的问题是想查询(a1,a2,a3,a4....)这里面的数据,请高手帮我解决一下,谢谢

解决方案 »

  1.   

    用个循环把 字段里的表名(a1,a2,a3,a4....) 分解出来
    然后 组成 sqlstr=‘select * from ’||分解出的表名execute immediate sqlstr ;
      

  2.   

    你是想用sql实现?我也是来学习的  估计要写个过程
      

  3.   

    sql  估计是不行的,,可能要用存储过程
      

  4.   


    --类似下面这个样子
    SQL> --表
    SQL> create table a(id int,col varchar2(20));
    SQL> insert into a values(1,'a1,a2,a3,a4');
    SQL> create table a1(id int);
    SQL> insert into a1 select 1 from dual;
    SQL> create table a2(id int);
    SQL> insert into a2 select 1 from dual union select 2 from dual;
    SQL> create table a3(id int);
    SQL> insert into a3 select 1 from dual union select 2 from dual;
    SQL> create table a4(id int);
    SQL> insert into a4 select 1 from dual;
    SQL> commit;
    SQL> --取记录
    SQL> declare
      2   v_col varchar2(20);
      3   v_pos int := 1;
      4   v_cou int := 0;
      5   v_sql varchar2(1000);
      6  begin
      7   select col into v_col from a where id=1;
      8   loop
      9   exit when regexp_substr(v_col,'[^,]+',1,v_pos) is null;
     10   v_sql := 'select count(*) from '||regexp_substr(v_col,'[^,]+',1,v_pos);
     11   execute immediate v_sql into v_cou;
     12   dbms_output.put_line('Rows of table '||regexp_substr(v_col,'[^,]+',1,v_pos)||' is:'||v_cou);
     13   v_pos := v_pos+1;
     14   end loop;
     15  end;
     16  /
    Rows of table a1 is:1                                                           
    Rows of table a2 is:2                                                           
    Rows of table a3 is:2                                                           
    Rows of table a4 is:1  
      

  5.   

    因为你需要查询的表名事先不知道,需要去查询出来,但是查询出来的是字符串,这样不能用来执行,所以,你需要去用动态SQL来拼接查询语句,然后执行。
    按照2L哥们的方法来做吧。