Oracle 如何将光标值传递到变量中?
我试图使用游标从表1的两列1、第2列读取值。然后我想将这些值传递给另一个游标或select into语句 因此,我的PL/Sql脚本将使用这两列的值从另一个名为table2的表中获取数据 这可能吗?做这样的事情最好最快的方法是什么Oracle 如何将光标值传递到变量中?,oracle,Oracle,我试图使用游标从表1的两列1、第2列读取值。然后我想将这些值传递给另一个游标或select into语句 因此,我的PL/Sql脚本将使用这两列的值从另一个名为table2的表中获取数据 这可能吗?做这样的事情最好最快的方法是什么 谢谢:)是的,可以将光标值传递到变量中。只需使用fetchinto即可从光标中再获取一行。之后,您可以使用一些选择进入语句的where子句中的变量。例如: declare cursor c1 is select col1, col2 from table1;
谢谢:)是的,可以将光标值传递到变量中。只需使用
fetchinto
即可从光标中再获取一行。之后,您可以使用一些选择进入语句的where
子句中的变量。例如:
declare
cursor c1 is select col1, col2 from table1;
l_col1 table1.col1%type;
l_col2 table1.col2%type;
l_col3 table2.col3%type;
begin
open c1;
loop
fetch c1 into l_col1, l_col2;
exit when c1%notfound;
select col3
into l_col3
from table2 t
where t.col1 = l_col1 --Assuming there is exactly one row in table2
and t.col2 = l_col2; --satisfying these conditions
end loop;
close c1;
end;
如果使用隐式光标,则更简单:
declare
l_col3 table2.col3%type;
begin
for i in (select col1, col2 from table1)
loop
select col3
into l_col3
from table2 t
where t.col1 = i.col1 --Assuming there is exactly one row in table2
and t.col2 = i.col2; --satisfying these conditions
end loop;
end;
在这些示例中,使用子查询更有效
begin
for i in (select t1.col1
, t1.col2
, (select t2.col3
from table2 t2
where t2.col1 = t1.col1 --Assuming there is atmost one such
and t2.col2 = t1.col2 --row in table2
) col3
from table1 t1)
loop
...
end loop;
end;
refcursors是用于此的,但我始终认为,可以为相同的对象执行子查询。。也可以考虑使用嵌套表。(PL SQL表)。可以将嵌套表转换为实际表,并在查询中使用。