Error condition MSG appears correctly, but good condition msg does not appear

I did a sequence of update employe_id of the sequence. I stated a function to check for the existence of the department_id in the departments table. I stated a procedure to enter a new record in the employees table after verifying the existence of the Department in the departments table. When I get a bad department_id the particular error msg that accompanies it is displayed correctly, but, when I get a good present department_id in the departments table, data is entered, but the particular error msg that accompanies it is not displayed.

CREATE SEQUENCE employees_seq

START WITH 1000

INCREMENT BY 1;

SELECT double employees_seq.nextval;

SELECT double employees_seq.currval;

CREATE or REPLACE function valid_deptid (pi_department_id, departments.department_id%TYPE) RETURNS a BOOLEAN IS

char v_temp;

BEGIN

SELECT 'x' INTO v_temp

Ministries

WHERE department_id = pi_department_id;

RETURNS true;

EXCEPTION

WHEN no_data_found THEN

Returns false;

WHILE others THEN

Returns false;

END;

/

CREATE or REPLACE procedure new_emp (pi_first_name IN employees.first_name%TYPE,

pi_last_name IN employees.last_name%TYPE,

pi_email_id IN employees.email%TYPE,

pi_hire_date employees.hire_date%TYPE,

pi_job_id employees.job_id%TYPE DEFAULT 'SA_REP '.

pi_salary IN employees.salary%TYPE 1000 by DEFAULT,

pi_commission_pct IN employees.commission_pct%TYPE default 0,

pi_manager_id IN employees.manager_id%TYPE DEFAULT 145,

pi_department_id IN employees.department_id%TYPE by DEFAULT 30,

po_error_code OUT varchar2,

po_error_msg OUT varchar2) IS

v_temp BOOLEAN;

BEGIN

v_temp: = valid_deptid (pi_department_id);

IF v_temp THEN

INSERT INTO copy_employees(employee_id,first_name,last_name,email,hire_date,job_id,salary,commission_pct,manager_id,department_id) VALUES (employees_seq.nextval, pi_first_name, pi_last_name, pi_email_id, pi_hire_date, pi_job_id, pi_salary, pi_commission_pct, pi_manager_id, pi_department_id);

po_error_code: = '0';

po_error_msg: = "Insert complete."

ON THE OTHER

po_error_code: = "9";

po_error_msg: = ' the department_id entered is invalid, please try again. "

RAISE_APPLICATION_ERROR (-20999, po_error_msg);

END IF;

EXCEPTION

WHILE others THEN

RAISE_APPLICATION_ERROR (-20001, substr(SQLERRM,11));

END;

/

Calling program: -.

DECLARE

v_first_name employees.first_name%TYPE;

v_last_name employees.last_name%TYPE;

v_email_id employees.email%TYPE;

v_hire_date employees.hire_date%TYPE;

v_department_id employees.department_id%TYPE;

v_error_code varchar2 (10);

v_error_msg varchar2 (60);

BEGIN

new_emp (& v_first_name, & v_last_name, & v_email_id, & v_hire_date,' ', NULL, NULL, NULL, & v_department_id, v_error_code, v_error_msg);

If v_error_code = "0" THEN

dbms_output.put_line (v_error_msg);

ELSIF v_error_code = '9' THEN

dbms_output.put_line (v_error_msg);

END IF;

END;

/

When I get an invalid department_id not in the departments table: -.

Enter the value for v_first_name: "a".

Enter the value for v_last_name: 'b '.

Enter the value of v_email_id: ' [email protected] '

Enter the value for v_hire_date: sysdate

Enter the value for v_department_id: 25

10 old: new_emp (& v_first_name, & v_last_name, & v_email_id, & v_hire_date,' ', NULL, NULL, NULL, & v_department_id, v_error_code, v_error_msg);

new 10: new_emp ('a', 'b',' [email protected]', sysdate,' ', NULL, NULL, NULL, 25, v_error_code, v_error_msg);

DECLARE

*

ERROR on line 1:

ORA-20001: the department_id entered is invalid, please try again - the error msg I intend to display appears correctly

ORA-06512: at the 'SUMAN '. NEW_EMP', line 26

ORA-06512: at line 10

When I enter a valid, present department_id in the departments table: -.

Enter the value for v_first_name: "a".

Enter the value for v_last_name: 'b '.

Enter the value of v_email_id: ' [email protected] '

Enter the value for v_hire_date: sysdate

Enter the value for v_department_id: 80

10 old: new_emp (& v_first_name, & v_last_name, & v_email_id, & v_hire_date,' ', NULL, NULL, NULL, & v_department_id, v_error_code, v_error_msg);

new 10: new_emp ('a', 'b',' [email protected]', sysdate,' ', NULL, NULL, NULL, 80, v_error_code, v_error_msg);

PL/SQL procedure is completed with success - the error msg I intend to display is not displayed.

You missed to add SET SERVEROUTPUT ON before calling your program, and I will suggest to maintain the constraint for this scenario (Parent-child relationship).

Anyway, I will correct your mistakes and this is that the code will satisfy your needs. You do not need to write another function to check if the id of service entry in the department table or not. See the bottom of modified code.

CREATE OR REPLACE PROCEDURE new_emp (pi_first_name IN employees.first_name%TYPE,

pi_last_name IN employees.last_name%TYPE,

pi_email_id IN employees.email%TYPE,

pi_hire_date employees.hire_date%TYPE,

pi_job_id employees.job_id%TYPE DEFAULT 'SA_REP '.

pi_salary IN employees.salary%TYPE 1000 by DEFAULT,

pi_commission_pct IN employees.commission_pct%TYPE default 0,

pi_manager_id IN employees.manager_id%TYPE DEFAULT 145,

pi_department_id IN employees.department_id%TYPE by DEFAULT 30,

po_error_code ON the NUMBER,

po_error_msg OUT VARCHAR2)

IS

BEGIN

INSERT INTO copy_employees (employe_id,

first name,

last_name,

E-mail

hire_date,

job_id,

salary,

commission_pct,

manager_id,

department_id)

SELECT employees_seq.nextval,

pi_first_name,

pi_last_name,

pi_email_id,

pi_hire_date,

pi_job_id,

pi_salary,

pi_commission_pct,

pi_manager_id,

pi_department_id

Ministries

WHERE department_id = pi_department_id;

IF (SQL % ROWCOUNT > 0) THEN

po_error_code: = 0;

po_error_msg: = "Insert complete."

ON THE OTHER

po_error_code: = 9;

po_error_msg: = ' the department_id entered is invalid, please try again. "

RAISE_APPLICATION_ERROR (-20999, po_error_msg);

END IF;

COMMIT;

END;

Performances: -.

============

-Invalid department_id not in the departments table.

SET SERVEROUTPUT ON

DECLARE

v_first_name copy_employees.first_name%TYPE: = 'a ';

v_last_name copy_employees.last_name%TYPE: = 'b ';.

v_email_id copy_employees.email%TYPE: = ' [email protected]';

v_hire_date copy_employees.hire_date%TYPE: = SYSDATE;

v_department_id copy_employees.department_id%TYPE: = 25;

v_error_code varchar2 (10);

v_error_msg varchar2 (60);

BEGIN

new_emp (v_first_name, v_last_name, v_email_id, v_hire_date,' ', NULL, NULL, NULL, v_department_id, v_error_code, v_error_msg);

DBMS_OUTPUT. Put_line (v_error_msg);

END;

-Valid, present department_id in the departments table.

DECLARE

v_first_name copy_employees.first_name%TYPE: = 'a ';

v_last_name copy_employees.last_name%TYPE: = 'b ';.

v_email_id copy_employees.email%TYPE: = ' [email protected]';

v_hire_date copy_employees.hire_date%TYPE: = SYSDATE;

v_department_id copy_employees.department_id%TYPE: = 80;

v_error_code varchar2 (10);

v_error_msg varchar2 (60);

BEGIN

new_emp (v_first_name, v_last_name, v_email_id, v_hire_date,' ', NULL, NULL, NULL, v_department_id, v_error_code, v_error_msg);

DBMS_OUTPUT. Put_line (v_error_msg);

END;

Unit tests: -.

========

SQL > SELECT * FROM departments;

DEPARTMENT_ID DNAME

------------- --------------------

DEPT1 80

SQL > SET SERVEROUTPUT ON

SQL > DECLARE

2 v_first_name copy_employees.first_name%TYPE: = 'a ';

3 v_last_name copy_employees.last_name%TYPE: = 'b ';.

4 v_email_id copy_employees.email%TYPE: = ' [email protected]';

5 v_hire_date copy_employees.hire_date%TYPE: = SYSDATE;

6 v_department_id copy_employees.department_id%TYPE: = 25;

v_error_code 7 varchar2 (10);

8 v_error_msg varchar2 (60);

BEGIN 9

10 new_emp (v_first_name, v_last_name, v_email_id, v_hire_date,' ', NULL, NULL, NULL, v_department_id, v_error_code, v_error_msg);

11 DBMS_OUTPUT. Put_line (v_error_msg);

12 END;

13.

DECLARE

*

ERROR on line 1:

ORA-20999: the department_id entered is invalid, please try again

ORA-06512: at «WMIS.» NEW_EMP', line 44

ORA-06512: at line 10

SQL > DECLARE

2 v_first_name copy_employees.first_name%TYPE: = 'a ';

3 v_last_name copy_employees.last_name%TYPE: = 'b ';.

4 v_email_id copy_employees.email%TYPE: = ' [email protected]';

5 v_hire_date copy_employees.hire_date%TYPE: = SYSDATE;

6 v_department_id copy_employees.department_id%TYPE: = 80;

v_error_code 7 varchar2 (10);

8 v_error_msg varchar2 (60);

BEGIN 9

10 new_emp (v_first_name, v_last_name, v_email_id, v_hire_date,' ', NULL, NULL, NULL, v_department_id, v_error_code, v_error_msg);

11 DBMS_OUTPUT. Put_line (v_error_msg);

12 END;

13.

Complete insert

PL/SQL procedure successfully completed.

Thank you

Ann

Tags: Database

Similar Questions

Maybe you are looking for