2010-08-10 65 views
1

林检索多行上存储的过程工作在我要检索一组结果,并分别处理每个元素,然后返回整个结果。(使用3个不同的表)使用For循环来在Oracle过程

林不太熟悉的数据库,但继承人什么我能拿出..

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" 

可以anyo请解释我如何纠正这个错误并获得所需的结果?

PS。即时通讯使用的是Oracle 9i中&蟾蜍9个

感谢,
汤姆

回答

0

您需要添加一个INTO子句指定哪个局部变量将所选择的数据,例如。

select ID, Name 
    into myID, myName 
from emp 
+0

假设select返回一行。 – DCookie 2010-08-10 17:46:39

4

在你的循环内选择需要有一个INTO子句来处理价值 - 它是不是从你的代码你想要做什么明确的,但我怀疑嵌套查询的/ JOIN内游标循环可以更好地写成主游标中的三个表连接。

+2

+1,特别是对于将所有选择移动到主游标的建议。你建议INTO子句能够解决这个问题,假设循环内的select只为游标上的每次迭代返回一行,否则你会得到一个ORA-1422错误。 – DCookie 2010-08-10 17:45:34

1

下面是一个可能的解决方案,做出了相当数量的假设。正如所写的,它将结果作为包含来自所有3个表的数据的引用游标返回(这将使得它为每个表返回一个引用游标是微不足道的,但这对于可疑结果会更有用)。

但是,除非您真的在PL/SQL中执行SQL中不能做的事情,否则直接在SQL中执行此操作会更好。

create object EMP_PAYROLL_BENEFIT as object (
    em_id number, 
    some_payroll_column number, 
    some_benefit_column number); 

create type NT_EMP_PAYROLL_BENEFIT as table of EMP_PAYROLL_BENEFIT; 

create or replace procedure GET_EMP_RSLT(p_output OUT sys_refcursor) IS  
CURSOR ecursor IS select emp_id 
        from temp_employee te 
         join payroll_info pi 
         on te.emp_id = pi.emp_id 
         join benefit_info bi 
         on te.emp_id = bi.emp_id 
        where some_column = 'some condition'; 
v_results NT_EMP_PAYROLL_BENEFIT; 
BEGIN 
    open ecursor; 
    fetch ecursor bulk collect into v_results; 
    close ecursor; 
    for i = v_results.first..v_results.last loop 
     v_results.some_benefit_column := some_payroll_column + i; 
    end loop; 
    open p_output for select * from table(v_results); 
end; 
-1

代码中存在太多的语法和意识形态错误。 因此,阅读,请PL/SQL documentation here,尤其是PL/SQL Architecture section了解SQL和PL/SQL之间的差异(一般SQL - 查询语言,PL/SQL - 编程语言)和章节你的情况:

  1. "Understanding PL/SQL Procedures""Understanding PL/SQL Functions"
  2. "Cursor FOR Loops""Using cursor FOR Loops"

全PL为Oracle 9i R2/SQL参考可在this link

所有Oracle 9i R2文档的集合可以找到here