oracle 循环遍历预定义的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10798161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loop through pre defined values
提问by JGS
Is there a way to do a "for each" in oracle, something like this:
有没有办法在 oracle 中做一个“for each”,像这样:
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.
谢谢。
回答by Tony Andrews
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;
回答by ArtOfWarfare
This comes from A.B.Cade's comment on the currently accepted answer, but I find it a lot cleaner and deserving of more attention:
这来自 ABCade 对当前接受的答案的评论,但我发现它更清晰,值得更多关注:
BEGIN
FOR i IN (SELECT column_value FROM table(sys.dbms_debug_vc2coll(1, 3, 5))) LOOP
dbms_output.put_line(i.column_value);
END LOOP;
END;