我的查询需要一个参数,并且需要对来自表的所有值执行。如何调用我的查询?请帮忙。
要执行的查询: 从 PERSON 中选择 &&pnbr、pname,其中 pno=&&pnbr;
查询给出参数的所有值: 从客户中选择不同的人员
您发布的代码建议您使用 SQL*Plus(因为替换变量)。
但是,你真的需要那个参数吗?因为,第一个查询必须返回第二个查询返回的值的一些行。那么 - 为什么不采取不同的方法呢?这是其中的一些。
select distinct p.pnbr, p.pname
from person p join customer c on c.personnbr = p.pnbr
或
select p.pnbr, p.pname
from person p
where p.pnbr in (select c.personnbr
from customer c)
或匿名 PL/SQL 块
declare
l_pname person.pname%type;
begin
for cur_r in (select distinct c.personnbr from customer c) loop
select p.pname
into l_pname
from person p
where p.pnbr = cur_r.personnbr;
dbms_output.put_line(cur_r.personnbr ||' - '|| l_pname);
end loop;
end;
/
或返回 refcursor 的函数
create or replace function f_person return sys_refcursor is
l_rc sys_refcursor;
begin
open l_rc for select p.pnbr, p.pname
from person p
where p.pnbr in (select c.personnbr
from customer c);
return l_rc;
end;
/
select f_person from dual;
我认为您不能在纯 SQL 中执行您所要求的操作(意思是:让第二个查询返回一些值,并且以某种方式自动将它们用作第一个查询中的参数)。