oraclesql中的Bash For循环逻辑
我正在寻找类似于oracle中bash for循环的东西oraclesql中的Bash For循环逻辑,sql,oracle,bash,for-loop,Sql,Oracle,Bash,For Loop,我正在寻找类似于oracle中bash for循环的东西 for i in 1 5 3 8; do echo "print $i"; done 因此,这将导致 print 1 print 5 print 3 print 8 select * from TABLE where column1='1'; select * from TABLE where column1='5'; select * from TABLE where column1='3'; select * from TABLE
for i in 1 5 3 8; do echo "print $i"; done
因此,这将导致
print 1
print 5
print 3
print 8
select * from TABLE where column1='1';
select * from TABLE where column1='5';
select * from TABLE where column1='3';
select * from TABLE where column1='8';
我想要Oracle SQL中类似的逻辑,比如
for i in 1 5 3 8; do echo " select * from TABLE where column1='$i';"; done
因此,这将导致
print 1
print 5
print 3
print 8
select * from TABLE where column1='1';
select * from TABLE where column1='5';
select * from TABLE where column1='3';
select * from TABLE where column1='8';
那么如何在Oracle SQL中获得类似的逻辑呢?我希望我正确理解了您的请求:
SELECT r FROM (
SELECT ROWNUM r
FROM DUAL
CONNECT BY ROWNUM <= 8)
WHERE r IN (1, 5, 3, 8)
基本上,我们正在创建一列从1到8的所有数字,然后选择您要求的数字。如果要处理字符串或大范围的数字,但要处理少量的行,此解决方案将不会非常有效,然后应将值存储在表中。您可以在plsql中构建循环,例如:
begin
for i in (
select 1 as num from dual union all
select 5 from dual union all
select 3 from dual union all
select 8 from dual
)
loop
dbms_output.put_line('number: ' || i.num);
/* do whatever you need */
end loop;
end;
/
如果运行查询时需要此选项,或许您可以简单地在中使用:
或者,要构建更复杂的列表:
select *
from table
where column1 in (
select something
from ...
)
您是在寻找SQL还是PL/SQL?SQL没有循环。很可能,您只希望列1位于1,5,3,8中。但这实际上取决于您要完成的任务——一条SQL语句不能运行4个单独的查询。它可以运行一个包含所有四组结果的查询。