pl/sql过程的in参数无效

0lvr5msh  于 2021-08-13  发布在  Java
关注(0)|答案(1)|浏览(500)

如果p\u id不存在(我的id从1到1000)或者添加错误消息“id not found in this table”无效,那么如何在此过程中进行id验证?
这是我的密码:

CREATE OR REPLACE PROCEDURE MyProc
(
  p_id IN NUMBER,
  ret_val OUT NUMBER
) AS
BEGIN
  ret_val := 0;
  BEGIN
    UPDATE Table1
       SET n_id = 2,
           n_date = SYSDATE
       WHERE n_id = p_id;
    ret_val := 1;
  EXCEPTION
    WHEN OTHERS THEN
         ret_val := -1;
  END;
END MyProc;
rjee0c15

rjee0c151#

如果p\u id无效,即提供一个非数字值作为输入,则过程将隐式出错。oracle将抱怨数据类型不匹配。
若要处理“在此表中找不到id”,可以借助oracle隐式游标属性sql%rowcount显式引发异常。它将返回 <p_id> ID not found in this table error. ```
CREATE OR REPLACE PROCEDURE MyProc
(
p_id IN NUMBER,
ret_val OUT NUMBER
) AS

BEGIN
ret_val := 0;

UPDATE Table1
SET n_id = 2,
n_date = SYSDATE
WHERE n_id = p_id;

ret_val := 1;

IF (SQL%ROWCOUNT = 0) THEN
ret_val := -1;
RAISE_APPLICATION_ERROR(-20000,p_id||' - ID not found in this table ');
END IF;

END MyProc;
/

参考dbfiddle进行演示https://dbfiddle.uk/?rdbms=oracle_18&fiddle=8436a79b7a2f7f37a4e9b8f324e37ab4

相关问题