Plsql

如何將參數傳遞給刪除語句?

  • November 30, 2015

在執行這個 PL/SQL 塊時:

CREATE OR REPLACE PROCEDURE procAuth_Title_Delete (auth_ID NUMBER,
                                       title_Nos IN NUMBER)    
IS  
BEGIN  
DELETE FROM AUTHOR_TITLE where author_id='||auth_ID||' and title_number='||title_Nos||';  
dbms_output.Put_line ( 'all records have been deleted with the values auth_ID: '||auth_ID||' and title_Nos: '||title_Nos||'.' );  
END;  
/  
EXECUTE procAuth_Title_Delete ( 1234,1 );  

我得到了錯誤

ORA-01722: invalid number

我知道 delete 語句的“where”子句有問題。如何讓 delete 語句用那些參數 ‘(1234,1)’ 代替 ‘auth_ID’ 和 ’title_Nos’?

您不能像在 SQL 語句中那樣連接變數,而且無論如何都不需要這樣做:

CREATE OR REPLACE PROCEDURE procAuth_Title_Delete (auth_ID NUMBER,
                                       title_Nos IN NUMBER)    
IS  
BEGIN  
 DELETE FROM AUTHOR_TITLE 
   where author_id = auth_ID  --<< just use the parameter
     and title_number = title_Nos;

 dbms_output.Put_line ( 'all records have been deleted with the values auth_ID: '||auth_ID||' and title_Nos: '||title_Nos||'.' );  
END;  
/  

引用自:https://dba.stackexchange.com/questions/122426