2017-10-08 52 views
0

Oracle /蟾蜍的新手。Oracle聲明變量並在另一個選擇查詢中使用它

我正在定義一個變量並從select中插入它的值。設置其值後,我將其用於另一個選擇語句。但是,這給了我錯誤和INTO子句預計在SELECT

declare maxLineNo number := 0; 

BEGIN 
select Max(b.Line_No) into maxLineNo FROM Brokerage b WHERE b.External_App_Id = 3720  AND b.Account_Id = '16970' ; 

SELECT b.External_App_Id -- this select giving erro 
     , maxLineNo + 1 
     , b.Currency 
     , '' 
     , CAST(SUM(b.Brokerage_Amt) as VARCHAR2(17)) 
     , '' 
     , '' 
     , 1003 -- 1003 for Report summary 
     , '' 
    FROM Brokerage b 
    WHERE b.External_App_Id = 3720 
    GROUP BY External_App_Id, B.CURRENCY 
    ORDER BY 2; 

DBMS_OUTPUT.PUT_LINE(maxLineNo); -- this is giving value 
end; 
+1

,'SELECT'僅適用於'INTO'條款 –

+0

所以你有什麼建議?如果關閉該塊,var MaxLineNo將無法在外部使用。 – PawanS

+1

此外,最好爲空值寫'NULL'而不是''。 –

回答

0

也許你正在尋找Implicit Statement Results in Oracle Database 12c Release 1

declare 
    maxLineNo number := 0; 
    rc sys_refcursor; 
BEGIN 
select Max(b.Line_No) into maxLineNo 
FROM Brokerage b 
WHERE b.External_App_Id = 3720  AND b.Account_Id = '16970' ; 

open rc for 
SELECT b.External_App_Id -- this select giving erro 
     , '' 
     , b.Currency 
     , '' 
     , CAST(SUM(b.Brokerage_Amt) as VARCHAR2(17)) 
     , '' 
     , '' 
     , 1003 -- 1003 for Report summary 
     , '' 
    FROM Brokerage b 
    WHERE b.External_App_Id = 3720 
    GROUP BY External_App_Id, B.CURRENCY 
    ORDER BY 2; 

DBMS_OUTPUT.PUT_LINE(maxLineNo); -- this is giving value 

dbms_sql.return_result(rc); 
end; 
PL/SQL塊中
相關問題