2016-03-23 42 views
1

這是我迄今爲止所做的。當我輸入有效數據並運行它時,表格正確更新。當我使用我認爲不存在的作者號碼運行它時,它仍然運行,並且不輸出異常語句。有誰知道爲什麼我的例外似乎沒有工作。任何幫助,將不勝感激謝謝!使用存儲過程更新行的Sqlplus

CREATE OR REPLACE PROCEDURE update_authorname 
(selected_author_num IN NUMBER, 
new_author_first IN CHAR, 
new_author_last IN CHAR) AS 

BEGIN 
UPDATE author 
SET author_first = new_author_first, author_last = new_author_last 
WHERE author_num = selected_author_num; 

EXCEPTION 
    WHEN NO_DATA_FOUND 
    THEN 
    DBMS_OUTPUT.PUT_LINE('No author with this number exists: ' || selected_author_num); 

    WHEN ROWTYPE_MISMATCH 
    THEN 
    DBMS_OUTPUT.PUT_LINE('Error: There was a row type mismatch when updating'); 

END; 
/

這是我用來調用的程序:

BEGIN 
update_authorname(6,'Emma','White'); 
END; 
/

回答

0

一個update語句時where子句不匹配不會拋出異常,它只是默默地失敗。相反,你需要檢查兩種:

  1. exists試圖更新之前或
  2. 更新改變了一些東西。

1.行存在

CREATE OR REPLACE PROCEDURE update_authorname 
(selected_author_num IN NUMBER, 
new_author_first IN CHAR, 
new_author_last IN CHAR) AS 

BEGIN 

IF NOT EXISTS (SELECT 1 FROM author WHERE author_num = selected_author_num; 
    DBMS_OUTPUT.PUT_LINE('No author with this number exists: ' || selected_author_num); 
ELSE 
    UPDATE author 
    SET author_first = new_author_first, author_last = new_author_last 
    WHERE author_num = selected_author_num; 

EXCEPTION 
    WHEN ROWTYPE_MISMATCH 
    THEN 
    DBMS_OUTPUT.PUT_LINE('Error: There was a row type mismatch when updating'); 

END; 
/

2.更新所做的更改

CREATE OR REPLACE PROCEDURE update_authorname 
(selected_author_num IN NUMBER, 
new_author_first IN CHAR, 
new_author_last IN CHAR) AS 

BEGIN 


UPDATE author 
SET author_first = new_author_first, author_last = new_author_last 
WHERE author_num = selected_author_num; 

IF @@ROWCOUNT <= 0 
    DBMS_OUTPUT.PUT_LINE('No author with this number exists: ' || selected_author_num); 

EXCEPTION 
    WHEN ROWTYPE_MISMATCH 
    THEN 
    DBMS_OUTPUT.PUT_LINE('Error: There was a row type mismatch when updating'); 

END; 
/

當然,還有其他的方法來達到同樣的效果,但這些都是兩個最常見。

+0

非常感謝!它現在有效。 – Jessica