有个问题请教高手们:
比如 表a中某个字段放的是表名称(a1,a2,a3,a4....),(a1,a2,a3,a4....)这几个表是实际存在的,现在的问题是想查询(a1,a2,a3,a4....)这里面的数据,请高手帮我解决一下,谢谢
比如 表a中某个字段放的是表名称(a1,a2,a3,a4....),(a1,a2,a3,a4....)这几个表是实际存在的,现在的问题是想查询(a1,a2,a3,a4....)这里面的数据,请高手帮我解决一下,谢谢
然后 组成 sqlstr=‘select * from ’||分解出的表名execute immediate sqlstr ;
--类似下面这个样子
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
按照2L哥们的方法来做吧。