2013-11-14 42 views
1

嗨,這是我的第一篇文章,很抱歉,如果這已被問到。我試圖創建一個程序,將增加存款賬戶(針對特定銀行)的利息,並將屏幕上的客戶名稱和新的存款賬戶餘額輸出到屏幕上。但是,我似乎無法使其工作。使用顯式光標的SQL過程

CREATE OR REPLACE PROCEDURE addInterestNew(bank VARCHAR, interestRate INT out) AS 
CURSOR CustomerBalance IS 
SELECT customerName, balance 
FROM Deposit 
WHERE branchName = bank; 


aCustomerName Deposit.customerName%TYPE; 
newBalance Deposit.balance%TYPE 

BEGIN 
UPDATE DEPOSIT 
SET balance = balance * (1 + (interestRate/100)) 
WHERE branchName = bank; 

FETCH 
OPEN CustomerBalance; 
LOOP 
FETCH CustomerBalance into aCustomerName,newbalance; 
EXIT WHEN CustomerBalance%notfound; 
dbms_output.put_line('CustomerName='||aCustomerName ||'balance='||newBalance); 
END LOOP; 
CLOSE CustomerBalance; 

END; 
/

任何幫助是讚賞

感謝

+0

你使用什麼數據庫?它以什麼方式工作,出現錯誤信息還是意外結果?另外爲什麼利率參數在它之後有「出」?似乎它應該是一個簡單的唯一參數。 – nickebbitt

+0

請詳細說明你在哪一行發生了什麼錯誤,或者爲什麼你的結果不正確 – LINQ2Vodka

+1

這是Oracle(我喜歡它的%TYPE) – LINQ2Vodka

回答

0

似乎大部分代碼是OK,你只要將光標的開放,應該將其刪除之前只是有一個額外的FETCH。另外,在newBalance變量聲明後缺少分號。除此之外,您將interestRate參數聲明爲IN OUT,但您似乎無法在任何地方對其進行修改。仍然,這應該工作:

CREATE OR REPLACE PROCEDURE addInterestNew(bank VARCHAR, interestRate INT out) AS 

    CURSOR CustomerBalance IS 
    SELECT customerName, balance 
     FROM Deposit 
    WHERE branchName = bank; 

    aCustomerName Deposit.customerName%TYPE; 
    newBalance Deposit.balance%TYPE; 
BEGIN 
    UPDATE DEPOSIT 
    SET balance = balance * (1 + (interestRate/100)) 
    WHERE branchName = bank; 

    OPEN CustomerBalance; 

    LOOP 
    FETCH CustomerBalance into aCustomerName,newbalance; 
    EXIT WHEN CustomerBalance%notfound; 
    dbms_output.put_line('CustomerName='||aCustomerName ||'balance='||newBalance); 
    END LOOP; 

    CLOSE CustomerBalance; 
END; 
/
+0

謝謝你soo它的工作原理,是的,我沒有刪除'out'參數不知道它是如何到達那裏的 – user2993831