PRAGMA EXCEPTION_INIT
A pragma is a special instruction to the PL/SQL compiler. It is important to note that pragmas
are processed at the time of the compilation. The EXCEPTION_INIT pragma allows you to associate an
Oracle error number with the name of a user-defined error. After you associate an error name with an
Oracle error number, you can reference the error and write a handler for it.
/*
PLSQL Program to demonstrate how to handle PRE-DEFINED EXCEPTIONs
*/
DECLARE
E_EmpRemaining EXCEPTION;
PRAGMA EXCEPTION_INIT ( E_EmpRemaining , -2292 );
V_Deptno Emp.Deptno%TYPE := &Deptno;
BEGIN
DELETE FROM Dept
WHERE Deptno = V_Deptno;
IF SQL%NOTFOUND THEN
DBMS_OUTPUT.PUT_LINE('The given information is not found in the Database >. Check
for proper values........');
ELSE
NULL;
END IF;
ROLLBACK;
EXCEPTION
WHEN E_EmpRemaining THEN
DBMS_OUTPUT.PUT_LINE ( 'Unable to delete the Department Number ' || V_Deptno ||
' as the Employees are existing . Validate your relations and then try once again.' );
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE( ' The given information is missing in the Database . Check for
proper vslues.......' );
END;
/
/*
PLSQL Program to demonstrate PRAGMA.
*/
DECLARE
E_EmpExists EXCEPTION;
V_Count NUMBER(2);
V_SalSum NUMBER(6);
PRAGMA EXCEPTION_INIT ( E_EmpExists , -2292);
V_Empno Emp.Empno%TYPE := &Empno;
BEGIN
DELETE FROM Emp
WHERE Empno = V_Empno;
IF SQL%NOTFOUND THEN
DBMS_OUTPUT.PUT_LINE (' The given Employee Number '|| V_Empno || ' is misssing
in the Database . Check for proper values.......');
ROLLBACK;
ELSE
COMMIT;
END IF;
EXCEPTION
WHEN E_EmpExists THEN
DBMS_OUTPUT.PUT_LINE( ' Unable to delete the Employee Details '|| V_Empno|| ' as
the Employee are existing. Validate your relations and then try once again...........' );
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE ( ' The given information in the Database . Please Check for
proper values................' );
END;
/
/*
PLSQL Program to demonstrate PRAGMA.
*/
DECLARE
E_NotNullViolation EXCEPTION;
PRAGMA EXCEPTION_INIT( E_NotNullViolation , -1400 );
BEGIN
INSERT INTO Emp ( Empno , Ename , Job , Sal , Comm , Deptno )
VALUES( &Empno , 'HIMAN' , 'ANALYST', 2500 , NULL, &Deptno);
COMMIT;
EXCEPTION
WHEN E_NotNullViolation THEN
DBMS_OUTPUT.PUT_LINE(' A field which cannot be NULL is not attended . Please check
properly......');
END;
/
/*
PLSQL Program to demonstrate PRAGMA.
*/
DECLARE
E_NotNullViolation EXCEPTION;
PRAGMA EXCEPTION_INIT( E_NotNullViolation , -1400);
E_CheckViolation EXCEPTION;
PRAGMA EXCEPTION_INIT( E_CheckViolation , -2290);
BEGIN
INSERT INTO Emp VALUES
( &Empno , '&Ename' , '&Job' , &MGR , SYSDATE , &Sal , &Comm , &Deptno);
COMMIT;
EXCEPTION
WHEN E_CheckViolation THEN
DBMS_OUTPUT.PUT_LINE ( ' A field with CKECK Constraint is not attended
properly..............' );
WHEN E_NotNullViolation THEN
DBMS_OUTPUT.PUT_LINE( ' A Field with NOT NULL Constraint is not attended
properly...........' );
END;
/
A pragma is a special instruction to the PL/SQL compiler. It is important to note that pragmas
are processed at the time of the compilation. The EXCEPTION_INIT pragma allows you to associate an
Oracle error number with the name of a user-defined error. After you associate an error name with an
Oracle error number, you can reference the error and write a handler for it.
/*
PLSQL Program to demonstrate how to handle PRE-DEFINED EXCEPTIONs
*/
DECLARE
E_EmpRemaining EXCEPTION;
PRAGMA EXCEPTION_INIT ( E_EmpRemaining , -2292 );
V_Deptno Emp.Deptno%TYPE := &Deptno;
BEGIN
DELETE FROM Dept
WHERE Deptno = V_Deptno;
IF SQL%NOTFOUND THEN
DBMS_OUTPUT.PUT_LINE('The given information is not found in the Database >. Check
for proper values........');
ELSE
NULL;
END IF;
ROLLBACK;
EXCEPTION
WHEN E_EmpRemaining THEN
DBMS_OUTPUT.PUT_LINE ( 'Unable to delete the Department Number ' || V_Deptno ||
' as the Employees are existing . Validate your relations and then try once again.' );
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE( ' The given information is missing in the Database . Check for
proper vslues.......' );
END;
/
/*
PLSQL Program to demonstrate PRAGMA.
*/
DECLARE
E_EmpExists EXCEPTION;
V_Count NUMBER(2);
V_SalSum NUMBER(6);
PRAGMA EXCEPTION_INIT ( E_EmpExists , -2292);
V_Empno Emp.Empno%TYPE := &Empno;
BEGIN
DELETE FROM Emp
WHERE Empno = V_Empno;
IF SQL%NOTFOUND THEN
DBMS_OUTPUT.PUT_LINE (' The given Employee Number '|| V_Empno || ' is misssing
in the Database . Check for proper values.......');
ROLLBACK;
ELSE
COMMIT;
END IF;
EXCEPTION
WHEN E_EmpExists THEN
DBMS_OUTPUT.PUT_LINE( ' Unable to delete the Employee Details '|| V_Empno|| ' as
the Employee are existing. Validate your relations and then try once again...........' );
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE ( ' The given information in the Database . Please Check for
proper values................' );
END;
/
/*
PLSQL Program to demonstrate PRAGMA.
*/
DECLARE
E_NotNullViolation EXCEPTION;
PRAGMA EXCEPTION_INIT( E_NotNullViolation , -1400 );
BEGIN
INSERT INTO Emp ( Empno , Ename , Job , Sal , Comm , Deptno )
VALUES( &Empno , 'HIMAN' , 'ANALYST', 2500 , NULL, &Deptno);
COMMIT;
EXCEPTION
WHEN E_NotNullViolation THEN
DBMS_OUTPUT.PUT_LINE(' A field which cannot be NULL is not attended . Please check
properly......');
END;
/
/*
PLSQL Program to demonstrate PRAGMA.
*/
DECLARE
E_NotNullViolation EXCEPTION;
PRAGMA EXCEPTION_INIT( E_NotNullViolation , -1400);
E_CheckViolation EXCEPTION;
PRAGMA EXCEPTION_INIT( E_CheckViolation , -2290);
BEGIN
INSERT INTO Emp VALUES
( &Empno , '&Ename' , '&Job' , &MGR , SYSDATE , &Sal , &Comm , &Deptno);
COMMIT;
EXCEPTION
WHEN E_CheckViolation THEN
DBMS_OUTPUT.PUT_LINE ( ' A field with CKECK Constraint is not attended
properly..............' );
WHEN E_NotNullViolation THEN
DBMS_OUTPUT.PUT_LINE( ' A Field with NOT NULL Constraint is not attended
properly...........' );
END;
/
No comments:
Post a Comment