Professional Documents
Culture Documents
Commonly Asked Oracle PLSQL Interview Question with Real time scenario Based Question
PLSQL Exceptions
1. What are the two types of exceptions?
i. user defined
ii. Predefined.
DUP_VAL_ON_INDEX
ZERO_DIVIDE
NO_DATA_FOUND
TOO_MANY_ROWS
CURSOR_ALREADY_OPEN
INVALID_NUMBER
INVALID_CURSOR
PROGRAM_ERROR
TIMEOUT _ON_RESOURCE
STORAGE_ERROR
LOGON_DENIED
VALUE_ERROR
4. How to retrying the transaction (continue) even after Exception raised OR How do you
handle exception when you want to do DML and Skip Error Values.
I. Encase the transaction in a sub-block.
II. Place the sub-block inside a loop that repeats the transaction.
III. Before starting the transaction, mark a save point. If the transaction succeeds,
commit, then exit from the loop. If the transaction fails, control transfers to the
exception handler, where you roll back to the savepoint undoing any changes,
then try to fix the problem.
5. DECLARE
6. name VARCHAR2(20);
7. ans1 VARCHAR2(3);
8. ans2 VARCHAR2(3);
9. ans3 VARCHAR2(3);
10. suffix NUMBER := 1;
11. BEGIN
12. FOR i IN 1..10 LOOP -- try 10 times
13. BEGIN -- sub-block begins
14. SAVEPOINT start_transaction; -- mark a savepoint
15. /* Remove rows from a table of survey results. */
16. DELETE FROM results WHERE answer1 = 'NO';
17. /* Add a survey respondent's name and answers. */
18. INSERT INTO results VALUES (name, ans1, ans2, ans3);
19. -- raises DUP_VAL_ON_INDEX if two respondents have the same
name
20. COMMIT;
21. EXIT;
22. EXCEPTION
23. WHEN DUP_VAL_ON_INDEX THEN
24. ROLLBACK TO start_transaction; -- undo changes
25. suffix := suffix + 1; -- try to fix
problem
26. name := name || TO_CHAR(suffix);
27. END; -- sub-block ends
28. END LOOP;
29. END;
30. /
OR
Declare
v1 varchar2(100);
v2 varchar2(100);
begin
for i in (select * from a) loop
begin
insert into
b(EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE_NUMBER,HIRE_DATE,JOB_I
D,SALARY,COMMISSION_PCT,MANAGER_ID,DEPARTMENT_ID)
values
(i.EMPLOYEE_ID,i.FIRST_NAME,i.LAST_NAME,i.EMAIL,i.PHONE_NUMBER,i.HIRE_DATE,i
.JOB_ID,i.SALARY,i.COMMISSION_PCT,i.MANAGER_ID,i.DEPARTMENT_ID);
commit;
exception
when others then
v1:=sqlcode;
v2:=sqlerrm;
insert into log_tab(sql_code,sql_err)
values (v1,v2);
commit;
end;
end loop;
end;
4. How do you track exception handler for a sequence of DML statements that caused an error
If you need to know which statement failed, you can use a locator variable.
DECLARE
stmt INTEGER;
name VARCHAR2(100);
BEGIN
stmt := 1; -- designates 1st SELECT statement
SELECT table_name INTO name FROM user_tables WHERE table_name LIKE
'ABC%';
stmt := 2; -- designates 2nd SELECT statement
SELECT table_name INTO name FROM user_tables WHERE table_name LIKE
'XYZ%';
EXCEPTION
WHEN NO_DATA_FOUND THEN
dbms_output.put_line('Table name not found in query ' || stmt);
END;
/
5. Can I use one or more exceptions to execute the same sequence of statements?
If you want two or more exceptions to execute the same sequence of statements, list the
exception names in the WHEN clause, separating them by the keyword OR, as follows:
EXCEPTION
WHEN over_limit OR under_limit OR VALUE_ERROR THEN
-- handle the error
6. Can I use When OTHERS exception in the beginning and others predefined exception in
subsequent.
No, The optional OTHERS exception handler, which is always the last handler in a block or
subprogram, acts as the handler for all exceptions not named specifically. Thus, a block or
subprogram can have only one OTHERS handler.
You can also raise a predefined exception explicitly. That way, an exception
handler written for the predefined exception can process other errors, as the
following example shows:
DECLARE
acct_type INTEGER := 7;
BEGIN
IF acct_type NOT IN (1, 2, 3) THEN
RAISE INVALID_NUMBER; -- raise predefined exception
END IF;
EXCEPTION
WHEN INVALID_NUMBER THEN
ROLLBACK;
END;
end inline_demo;
/
v_name VARCHAR2(2);
Short_Data_size EXCEPTION;
BEGIN
EXCEPTION
DBMS_OUTPUT.PUT_line(SQLERRM);
END;
where error_number is a negative integer in the range -20000 .. -20999 and message is
a character string up to 2048 bytes long. If the optional third parameter is TRUE, the
error is placed on the stack of previous errors. If the parameter is FALSE (the default),
the error replaces all previous errors. RAISE_APPLICATION_ERROR is part of package
DBMS_STANDARD, and as with package STANDARD, you do not need to qualify
references to it.
curr_sal NUMBER;
BEGIN
ELSE
END IF;
END raise_salary;