如果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;
1条答案
按热度按时间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;
/