plsql里面怎么实现如下for循环的效果?

xlh0053 2013-09-13 10:44:46
我要执行 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看到我要的效果,这个要怎么写呢?
...全文
656 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
如果不是很长 可以直接打印出来再执行

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个表的数据 循环插入到新表 然后直接访问新表就可以了
  • 打赏
  • 举报
回复
表名是什么规则 1-9 是01-09 那后面呢10 是010还是10 ?
无敌小二傻 2013-09-13
  • 打赏
  • 举报
回复
引用 8 楼 f_ky 的回复:
真的是误人,艹!!!
不要跟风就是雨,你好像没有过错误似的,人家也不是像你这样的傻子,拿过去编译不通过,人家也知道是错的,不需要你在这评论吧?看你说的话,就知道你的素质很低,像你这样的人,还是不要说我的好了,你比我好不到哪去的
wumugulu 2013-09-13
  • 打赏
  • 举报
回复
引用 4 楼 u010412956 的回复:
[quote=引用 3 楼 lyliu602 的回复:] [quote=引用 楼主 xlh0053 的回复:] 我要执行 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看到我要的效果,这个要怎么写呢?
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;
[/quote] 不要误人,,过程里面能直接单独使用 select 语句吗?[/quote] 真的是误人,艹!!!
无敌小二傻 2013-09-13
  • 打赏
  • 举报
回复
引用 4 楼 u010412956 的回复:
[quote=引用 3 楼 lyliu602 的回复:] [quote=引用 楼主 xlh0053 的回复:] 我要执行 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看到我要的效果,这个要怎么写呢?
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;
[/quote] 不要误人,,过程里面能直接单独使用 select 语句吗?[/quote] 楼主是要将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;
  • 打赏
  • 举报
回复

--先建个表存放结果,然后把查到德数据插入
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;
u010412956 2013-09-13
  • 打赏
  • 举报
回复
1楼已经说的很详细了,,不了解 语法 就去google了解spool。。。
u010412956 2013-09-13
  • 打赏
  • 举报
回复
引用 3 楼 lyliu602 的回复:
[quote=引用 楼主 xlh0053 的回复:] 我要执行 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看到我要的效果,这个要怎么写呢?
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;
[/quote] 不要误人,,过程里面能直接单独使用 select 语句吗?
无敌小二傻 2013-09-13
  • 打赏
  • 举报
回复
引用 楼主 xlh0053 的回复:
我要执行 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看到我要的效果,这个要怎么写呢?
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;
xlh0053 2013-09-13
  • 打赏
  • 举报
回复
语法不是很了解,能直接根据我的写一个出来给我参考一下么
wumugulu 2013-09-13
  • 打赏
  • 举报
回复
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
xlh0053 2013-09-13
  • 打赏
  • 举报
回复
各位拿分了,谢谢各位了

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧