plsql里面怎么实现如下for循环的效果?
我要执行 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看到我要的效果,这个要怎么写呢?
[解决办法]
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 off
start my.sql
[解决办法]
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;
--先建个表存放结果,然后把查到德数据插入
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;
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;