如何在 pl/sql 中的另一个游标的 select 语句中使用游标中的变量
我想运行一个查询,获取结果,然后使用第二个语句(光标)中第一个语句的值与另一个 select 语句迭代该查询的结果。
我的数据库中有 40 个用户。 所有用户都具有相同的数据库模式结构。 我想通过 : 获取用户名,
SELECT distinct username
from all_users
然后使用用户名运行如下查询:
Select lastname, firstname, email, email2 from username.member.
我的结果集将返回多行,因此我还需要一个行类型。
我尝试了许多不同的 pl/sql 组合:
DECLARE
CURSOR client_cur IS
SELECT distinct username
from all_users
where length(username) = 3;
-- client cursor
CURSOR emails_cur (cli all_users.username%TYPE) IS
SELECT id, name
FROM cli.org;
BEGIN
FOR client IN client_cur LOOP
dbms_output.put_line('Client is '|| client.username);
FOR email_rec in client_cur(client.username) LOOP
dbms_output.put_line('Org id is ' ||email_rec.id || ' org nam ' || email_rec.name);
END LOOP;
END LOOP;
END;
/
以及
DECLARE
CURSOR c1 IS
SELECT distinct username from all_users where length(username) = 3;
client c1%rowtype;
cursor c2 is Select id, name, allow_digest_flg from c1.username.org;
digest c2%rowtype;
-- declare record variable that represents a row fetched from the employees table
-- employee_rec c1%ROWTYPE;
BEGIN
-- open the explicit cursor and use it to fetch data into employee_rec
OPEN c1;
loop
FETCH c1 INTO client;
open c2;
loop
fetch c2 into digest;
DBMS_OUTPUT.PUT_LINE('digest is : ' || c2.id || ' and name is ' || c2.name || ' flg is ' || c2.allow_digest_flg );
end loop;
end loop;
END;
/
这些组合的许多变体。
有人可以帮助我吗? 谢谢
I want to run a query, get the results and then iterate through the results of that query with another select statement using the values of the first statement in my 2nd statement (cursor).
I have 40 users in my db.
All the users have the same db schema structure.
I want to get the username via :
SELECT distinct username
from all_users
then use the user name to run a query like this:
Select lastname, firstname, email, email2 from username.member.
My results set will return multiple rows so I need a row type as well.
I have tried many different pl/sql combinations:
DECLARE
CURSOR client_cur IS
SELECT distinct username
from all_users
where length(username) = 3;
-- client cursor
CURSOR emails_cur (cli all_users.username%TYPE) IS
SELECT id, name
FROM cli.org;
BEGIN
FOR client IN client_cur LOOP
dbms_output.put_line('Client is '|| client.username);
FOR email_rec in client_cur(client.username) LOOP
dbms_output.put_line('Org id is ' ||email_rec.id || ' org nam ' || email_rec.name);
END LOOP;
END LOOP;
END;
/
and
DECLARE
CURSOR c1 IS
SELECT distinct username from all_users where length(username) = 3;
client c1%rowtype;
cursor c2 is Select id, name, allow_digest_flg from c1.username.org;
digest c2%rowtype;
-- declare record variable that represents a row fetched from the employees table
-- employee_rec c1%ROWTYPE;
BEGIN
-- open the explicit cursor and use it to fetch data into employee_rec
OPEN c1;
loop
FETCH c1 INTO client;
open c2;
loop
fetch c2 into digest;
DBMS_OUTPUT.PUT_LINE('digest is : ' || c2.id || ' and name is ' || c2.name || ' flg is ' || c2.allow_digest_flg );
end loop;
end loop;
END;
/
AND MANY VARIATIONS OF THESE.
Can someone help me.
THANKS
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你需要使用动态SQL来实现这一点;类似于:
编辑以更正两个错误,并添加指向
OPEN-FOR
和 示例。编辑以使内部游标查询字符串变量。
You need to use dynamic SQL to achieve this; something like:
Edited to correct two errors, and to add links to 10g documentation for
OPEN-FOR
and an example.Edited to make the inner cursor query a string variable.
您当然可以使用显式游标执行类似
或等效的操作。
但是,如果您发现自己使用嵌套游标 FOR 循环,那么让数据库为您连接两个结果几乎总是更有效。毕竟,关系数据库非常非常擅长连接。我在这里猜测你的表格是什么样子以及它们如何根据你发布的代码相互关联,但类似于
You can certainly do something like
Or something equivalent using explicit cursors.
However, if you find yourself using nested cursor FOR loops, it is almost always more efficient to let the database join the two results for you. After all, relational databases are really, really good at joining. I'm guessing here at what your tables look like and how they relate based on the code you posted but something along the lines of
使用
alter session set current_schema =
,在您的情况下作为立即执行。有关详细信息,请参阅 Oracle 文档。
在你的情况下,这可能会归结为(未经测试)
Use
alter session set current_schema = <username>
, in your case as an execute immediate.See Oracle's documentation for further information.
In your case, that would probably boil down to (untested)