2013-01-31 42 views
0

我寫了下面的程序如何運行在Oracle過程多個SELECT語句

create or replace procedure sp_abc_profile 
(
    f_symbol_in abc.colname%TYPE 
) 
is profile abc%rowtype; 
is profile2 abc2%rowtype; 
begin 
    SELECT fname, lname,mname,age 
    INTO profile 
    FROM abc 
    WHERE f_symbol = f_symbol_in; 

    SELECT initiaiinvestment AS minInitialInvestment, pr as qt, class1 as clss 
    into profile2 
    FROM 
     abc2 
    WHERE f_symbol = f_symbol_in; 
end; 

在執行上面的我得到錯誤信息如下:

錯誤(7,3): PL/SQL:SQL語句忽略

錯誤(21,5):PL/SQL:ORA-00913:值過多

我不想選擇兩個表中的所有行。

如何在過程中編寫多個select語句,以便過程中的每個select語句都返回一個結果集。

+0

這是第7,並在代碼中的第21行? –

+0

其實我改變了程序的sql語句。發佈的錯誤消息屬於具有更多sql select語句的原始過程。 –

回答

0

解決方案是在oracle中使用CURSORS,以便過程中的每個select語句都返回一個結果集。

然後可以用您偏好的腳本語言遍歷該結果集以獲得所需的輸出。

create or replace 
procedure sp_abc_profile 
(
    symbol_in in tablename.fieldname%type, 
    cursor1 out SYS_REFCURSOR, 
    cursor2 out SYS_REFCURSOR, 
) 
as 
begin 


    open cursor1 for 
     {your select statement here} 


    open cursor2 for 
     {your second select statement here} 


end sp_abc_profile; 
2

嘗試以下操作:

is 
profile abc.fname%type; 
profile2 abc2.initiaiinvestment%type; 

有一個與具有程序多個SELECT語句沒有問題。這與所選列和PL/SQL類型不匹配有關。

除此之外,您的代碼中似乎有太多is

有關使用select into更多信息,請點擊此鏈接:Oracle PL/SQL "select into" clause

+0

我在程序下的幾個select語句中選擇了多列(編輯我的問題),我想讓它們在一個變量中。這怎麼可能。? –

+0

select * into profile –

+0

我不想要所有的列,但只有少數。 –

0

試試這個,我們可以在過程中多選擇語句:

有在你的代碼太多價值的問題。

create or replace procedure sp_abc_profile 
(
    f_symbol_in abc.colname%TYPE 
) 
is profile abc.fname%type; 
is profile2 abc2.initiaiinvestment%type; 
begin 
    SELECT fname 
    INTO profile 
    FROM abc 
    WHERE f_symbol = f_symbol_in; 

    SELECT initiaiinvestment into profile2 
    FROM 
     abc2 
    WHERE f_symbol = f_symbol_in; 
end; 
0

正如其他人指出你有太多「是」的陳述。

進行選入行變量時,您需要選擇的一切:

select * 
into profile 
from abc 
where f_symbol = f_symbol_in; 

select * 
into profile2 
from abc2 
where f_symbol = f_symbol_in; 

您還運行投擲,當你有多個f_symbol_in比賽異常的風險。您的代碼可以捕獲此異常,或者可以限制行數(即rownum < = 1),或者查看集合以加載與該參數匹配的所有行。

0
  1. 刪除多餘的 「IS」
  2. 地址:ROWNUM = 1
create or replace procedure sp_abc_profile 
(
    f_symbol_in abc.colname%TYPE 
) 
is profile abc%rowtype; 
/*is*/ profile2 abc2%rowtype; 
begin 
    SELECT fname, lname,mname,age 
    INTO profile 
    FROM abc 
    WHERE f_symbol = f_symbol_in 
    AND ROWNUM = 1; 

    SELECT initiaiinvestment AS minInitialInvestment, pr as qt, class1 as clss 
    into profile2 
    FROM 
     abc2 
    WHERE f_symbol = f_symbol_in 
    AND ROWNUM = 1; 
end;