gpt4 book ai didi

sql - (Oracle SQL)捕获唯一约束错误

转载 作者:行者123 更新时间:2023-12-03 08:22:49 25 4
gpt4 key购买 nike

这是我的代码:

Declare
violation_of_constraint EXCEPTION;
BEGIN
-- (A FEW INSERTS HERE: A, B, C)
SAVEPOINT X;
-- (ANOTHER INSERT HERE: D)
IF DUP_VAL_ON_INDEX THEN
ROLLBACK TO X;
COMMIT;
RAISE violation_of_constraint;
END IF;
EXCEPTION
WHEN violation_of_constraint THEN
DBMS_OUTPUT.PUT_LINE('Value already exists');
COMMIT;
END;
/

当我运行它(从sql命令行)时,在第11行出现此错误:
expression is of wrong type (line 11 is "IF DUP_VAL_ON_INDEX THEN")

怎么了

最佳答案

您需要定义一个嵌套的PL/SQL块,并将EXCEPTION BLOCK中的异常作为WHEN DUP_VAL_ON_INDEX处理...

不应为IF DUP_VAL_ON_INDEX

Declare
violation_of_constraint EXCEPTION;
BEGIN
BEGIN
-- (A FEW INSERTS HERE: A, B, C)
SAVEPOINT X;
-- (ANOTHER INSERT HERE: D)
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
ROLLBACK TO X;
COMMIT;
RAISE violation_of_constraint;
END;
EXCEPTION
WHEN violation_of_constraint THEN
DBMS_OUTPUT.PUT_LINE('Value already exists');
COMMIT;
END;
/

关于sql - (Oracle SQL)捕获唯一约束错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22175894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com