使用 For 循环在 Oracle 过程中检索多行
我正在研究存储过程,我需要检索一组结果并单独处理每个元素,然后返回整个结果。(使用 3 个不同的表)
我对数据库不太熟悉,但这就是我能够想到的。 执行时,
create or replace procedure GET_EMP_RSLT
IS
CURSOR ecursor IS select emp_id from temp_employee where 'some condition';
BEGIN
FOR empidset in ecursor
LOOP
Select * from
(select * from payroll_info where emp_id = empidset.emp_id) a
left join
(select * from benefit_info where emp_id = empidset.emp_id) b
on a.emp_id = b.emp_id
END LOOP;
END;
我收到以下错误。
an INTO clause is expected in this SELECT statement : "Select * from"
任何人都可以解释如何纠正此错误并获得所需的结果吗?
附言。我正在使用 Oracle 9i &蟾蜍 9
谢谢,
汤姆
Im working on stored procedure where I need to retrieve a set of results and process each element individually and then return the entire result.(using 3 different tables)
Im not too familiar with databases, but heres what I was able to come up with..
create or replace procedure GET_EMP_RSLT
IS
CURSOR ecursor IS select emp_id from temp_employee where 'some condition';
BEGIN
FOR empidset in ecursor
LOOP
Select * from
(select * from payroll_info where emp_id = empidset.emp_id) a
left join
(select * from benefit_info where emp_id = empidset.emp_id) b
on a.emp_id = b.emp_id
END LOOP;
END;
On execution, I get the following error..
an INTO clause is expected in this SELECT statement : "Select * from"
can anyone please explain on how do I correct this error and get the required results?
PS. Im using Oracle 9i & TOAD 9
Thanks,
Tom
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
循环内的 SELECT 需要有一个 INTO 子句来处理值 - 从代码中不清楚您要做什么,但我怀疑游标循环内的嵌套 SELECT's/JOIN 可以更好地写为主游标中的三表连接。
The SELECT inside of your loop needs to have an INTO clause to process the values - it is not clear from your code what you're trying to do, but I suspect the nested SELECT's/JOIN inside of the cursor loop could be better written as a three table join in the main cursor.
您需要添加 INTO 子句来指定放置所选数据的局部变量,例如。
You need to add an INTO clause to specify which local variables to place the selected data, eg.
下面是一个可能的解决方案,做出了相当多的假设。正如所写,它以引用游标的形式返回结果,其中包含来自所有 3 个表的数据(使其为每个表返回一个引用游标是很简单的,但对于可疑的结果来说,这将是更多的工作)。
但是,除非您确实在 PL/SQL 中执行了在 SQL 中无法执行的操作,否则最好直接在 SQL 中执行此操作。
Below is a possible solution, making a fair number of assumptions. As written, it returns the result as a ref cursor containing data from all 3 tables (it would be trivial to make it return a ref cursor for each table, but that would be more work for a dubious result).
However, unless you're really doing something in the PL/SQL that you can't do in SQL, you'd be much better off doing this directly in SQL.
你的代码中有太多语法和思想错误。
因此,请阅读此处的 PL/SQL 文档< /a>,特别是 PL/SQL 架构部分 了解 SQL 和 PL/SQL(通常是 SQL - 查询语言,PL/SQL - 编程语言)之间的区别以及适合您的案例的部分:
Oracle 9i R2 的完整 PL/SQL 参考位于 此链接。
所有 Oracle 9i R2 文档集均可在此处找到。
There are too many syntactical and ideological errors in your code.
Therefore, read, please, PL/SQL documentation here, especially PL/SQL Architecture section to understand difference between SQL and PL/SQL (generally SQL - query language, PL/SQL - programming language) and sections for your case:
Full PL/SQL reference for Oracle 9i R2 available at this link.
Set of all Oracle 9i R2 documentation can be found here.