2010-08-10 53 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