循环预先定义的值 [英] Loop through pre defined values

查看:147
本文介绍了循环预先定义的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以在oracle中做一个for each,就像这样:

  begin 
对于{1,2,5}
循环中的VAR
dbms_output.put_line('value:'|| VAR);
结束循环;
end;

我知道你可以这样做:

  begin 
for VAR in 1..5
loop
if(1,3,5)然后
dbms_output.put_line ('价值:'|| VAR);
end if;
结束循环;
end;

但是没有办法以更好的方式做到这一点?定义一组值并遍历它们?



谢谢。

解决方案

<你可以做到这一点,虽然可能不像你想要的光滑:

  declare 
type nt_type is号码表
nt nt_type:= nt_type(1,3,5);
begin
for 1..nt.count循环
dbms_output.put_line(nt(i));
结束循环;
end;

如果您在数据库中创建一个类型:

 创建类型number_table是数字表; 

然后您可以这样做:

<$ p (从table(number_table(1,3,5))中选择column_value作为var)循环
dbms_output.put_line(r.var); $ p $ $ $ $ $ $ $ $ $ $ $ $
结束循环;
end;


Is there a way to do a "for each" in oracle, something like this:

begin
  for VAR in {1,2,5}
  loop
    dbms_output.put_line('The value: '||VAR);
  end loop;
end;

I know you can do something like:

begin
  for VAR in 1..5
  loop
    if VAR in(1,3,5) then
      dbms_output.put_line('The value: '||VAR);
    end if;
  end loop;
end;

But isn't there a way to do this in a nicer way? Defining a set of values and iterating through them?

Thanks.

解决方案

You could do this, though probably not as slick as you'd like:

declare
  type nt_type is table of number;
  nt nt_type := nt_type (1, 3, 5);
begin
  for i in 1..nt.count loop
    dbms_output.put_line(nt(i));
  end loop;
end;

If you create a type in the database:

create type number_table is table of number;

then you can do this:

begin
  for r in (select column_value as var from table (number_table (1, 3, 5))) loop
    dbms_output.put_line(r.var);
  end loop;
end;

这篇关于循环预先定义的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆