PLSQL-过程中的异常

时间:2019-01-30 08:15:39

标签: oracle plsql exception-handling

在过程中使用异常。

插入语句的过程。

CREATE OR REPLACE PROCEDURE pSaveProductGroup (p_parentCode VARCHAR2, p_nameGroup VARCHAR2) IS

v_var "ProductGroups"."code"%type;
BEGIN

select p."code" into v_var
from "ProductGroups" p
where p."code"=p_parentCode;

if v_var is not null then

INSERT INTO "ProductGroups"
("parentCode","nameGroup")
VALUES(p_parentCode,p_nameGroup);
end if;

EXCEPTION
WHEN v_var is null then
dbms_output.put_line('Undefined group.');

END;

在需要时应打印异常。

Error: Error(18,12): PLS-00103: Encountered the symbol "IS" when expecting one of the following:     . then or

1 个答案:

答案 0 :(得分:2)

您可以执行以下操作来捕获异常。

CREATE OR REPLACE PROCEDURE pSaveProductGroup (p_parentCode    VARCHAR2,
                                               p_nameGroup     VARCHAR2)
IS
   v_var   "ProductGroups"."code"%TYPE;
   myex    EXCEPTION;
   PRAGMA EXCEPTION_INIT (myex, -20016);
BEGIN
   SELECT p."code"
     INTO v_var
     FROM "ProductGroups" p
    WHERE p."code" = p_parentCode;

   IF v_var IS NOT NULL
   THEN
      INSERT INTO "ProductGroups" ("parentCode", "nameGroup")
           VALUES (p_parentCode, p_nameGroup);
   ELSE
      IF (v_var IS NULL)
      THEN
         RAISE myex;
      END IF;
   END IF;
EXCEPTION
   WHEN myex
   THEN
      DBMS_OUTPUT.put_line (
         'ERROR_STACK: "Undefined group --> ' || DBMS_UTILITY.format_error_stack);
   WHEN NO_DATA_FOUND
   THEN
      DBMS_OUTPUT.put_line (
         'ERROR_STACK: NO_DATA_FOUND --> ' || DBMS_UTILITY.format_error_stack);
   WHEN OTHERS
   THEN
      DBMS_OUTPUT.put_line (
         'ERROR_STACK: OTHERS --> ' || DBMS_UTILITY.format_error_stack);
END;  

程序的修改版本

CREATE OR REPLACE PROCEDURE pSaveProductGroup (p_parentCode    VARCHAR2,
                                               p_nameGroup     VARCHAR2)
IS
   v_var   "ProductGroups"."code"%TYPE;
   myex    EXCEPTION;
   PRAGMA EXCEPTION_INIT (myex, -20016);
BEGIN
   SELECT p."code"
     INTO v_var
     FROM "ProductGroups" p
    WHERE p."code" = p_parentCode;

   IF v_var IS NOT NULL
   THEN
      INSERT INTO "ProductGroups" ("parentCode", "nameGroup")
           VALUES (p_parentCode, p_nameGroup);
   ELSE
      RAISE myex;
   END IF;
EXCEPTION
   WHEN myex
   THEN
      DBMS_OUTPUT.put_line (
         'ERROR_STACK: Undefined group --> ' || DBMS_UTILITY.format_error_stack);
   WHEN NO_DATA_FOUND
   THEN
      DBMS_OUTPUT.put_line (
         'ERROR_STACK: NO_DATA_FOUND --> ' || DBMS_UTILITY.format_error_stack);
   WHEN OTHERS
   THEN
      DBMS_OUTPUT.put_line (
         'ERROR_STACK: OTHERS --> ' || DBMS_UTILITY.format_error_stack);
END;