plsqlinterviewquestions(1) (1)
plsqlinterviewquestions(1) (1)
Interview Question
Bank
Named: Lists the actual values in random order and uses the
association operator to associate each actual parameter with its
formal parameter by name The PL/SQL association operator is =>.
Example:
--passing by combination
execute add_dept('Transport',locid=>1700,mgrid=>100)
Example:
exec proc1(90);
Can BOOLEAN datatype be used in functions that are called from SQL
statements?
No, functions called from SQL statements can only use valid SQL
datatypes, such as NUMBER, VARCHAR2, and DATE.
However, they cannot use PL/SQL datatypes such as BOOLEAN,
RECORD, and TYPE.
Example:
create or replace function fun1(n number)
return boolean
is
r number;
begin
r:=mod(n,2);
if r=0 then
return true;
else
return false;
end if;
end;
/
Function created.
Yes, it is.
But make sure that both records same structure(number of fields
and their datatypes).
Example1:
declare
r1 employees%rowtype;
r2 employees%rowtype;
begin
select * into r1 from employees where employee_id=100;
dbms_output.put_line(r1.employee_id||' '||r1.salary);
r2:=r1;
dbms_output.put_line(r2.employee_id||' '||r2.salary);
end;
/
output
100 24000
100 24000
declare
type r1 is record (eid number, ename varchar2(20));
type r2 is record (empid number, empname varchar2(20));
rec1 r1;
rec2 r2;
begin
select employee_id, last_name into rec1
from employees
where employee_id=100;
dbms_output.put_line(rec1.eid||' '||rec1.ename);
rec2.empid:=rec1.eid;
rec2.empname:=rec1.ename;
dbms_output.put_line(rec2.empid||' '||rec2.empname);
end;
/
output
100 King
100 King
SQL> /
Warning: Procedure created with compilation errors.
SQL> select name, type, text from user_errors where
name='PROC1';
What is an identifier?
Example:
Example:
declare
lname varchar2(20);
begin
select last_name into lname
from employees
where employee_id=102;
dbms_output.put_line(lname);
end;
/
Example:
create or replace trigger trg1
before insert on departments
begin
raise_application_error(-20124,’Can not insert into
departments’);
end;
/
select object_name, object_type
from user_objects
where object_type='TRIGGER' and object_name=’TRG1’;
Write the SQL query to view all the procedures within a schema.
Example:
begin
for i in 1..10 loop
if i>=6 then
exit;
end if;
dbms_output.put_line(i);
end loop;
end;
SQL> /
1
2
3
4
5
What are the system privileges that are required by a schema owner
(user) to create a trigger on a table?
Example:
No, only one OTHERS clause can be use in exception section and
OTHERS handler must be last among the exception handlers of a
block.
Package created.
Procedures and functions are named PL/SQL blocks. They are also
known as subprograms.
These subprograms are compiled and stored in the database.
Because they are named and stored you can invoke them whenever
you want.
begin
declare
empid number;
begin
select employee_id into empid
from employees
where department_id=89;
dbms_output.put_line(empid);
end;
dbms_output.put_line('Hi this vivek');
exception
when no_data_found then
dbms_output.put_line('no data found exception raised');
end;
/
no data found exception raised
Example:
declare
sal number;
begin
select salary into sal from employees where employee_id=101;
dbms_output.put_line('salary of employee 101 is :'||sal);
if sql%isopen then
dbms_output.put_line('implicit cursor is open');
else
dbms_output.put_line('implicit cursor is closed');
end if;
end;
SQL> /
salary of employee 101 is :17000
implicit cursor is closed
Example:
declare
procedure p1(str varchar2) – Local subprogram
is
begin
dbms_output.put_line(str);
end;
begin
p1('If you wanna be the best you need to take out the best');
end;
/
output: If you wanna be the best you need to take out the best
Benefits of PL/SQL
2) Improved performance
If the CREATE fails after a DROP, you are left with nothing, not
even the old procedure.
If the CREATE OR REPLACE fails (e.g. a syntax or semantics error
in the code), the previous version is still available and usable.
Bind variables
for example,
DEPTID
----------
10
SQL> declare
2 lname varchar2(20);
3 begin
4 select last_name into lname
5 from employees
6 where department_id=:deptid;
7 dbms_output.put_line(lname);
8 end;
9 /
Whalen
syntax:
execute schema_name.package_name.procedure_name(parametervalue);
example:
execute a_user.a_package.a_proc(5);
The SAVEPOINT statement names and marks the current point in the
processing of a transaction.
With the ROLLBACK TO statement, savepoints undo parts of a
transaction instead of the whole transaction.
1 row created.
Savepoint created.
Savepoint created.
Rollback complete.
Which data dictionary views have the information on the triggers that
are available in the database?
Example:
SQL> set serveroutput on
SQL>begin
2 dbms_output.put_line('Hello World');
3 end;
/
Hello World
What is a cursor FOR loop and how does it differ from an explicit
cursor?
Cursor for loop is the way by which you can reduce the size of
code.
It opens the cursor automatically, fetches the new record for
each iteration and closes the cursor automatically when it
doesn't find any record to fetch.
It reduces few steps from standard implementation of explicit
cursor.
Example:
declare
cursor c1 is select * from departments;
begin
for r1 in c1 loop
dbms_output.put_line(r1.department_id||’ ’
||r1.department_name);
end loop;
end;
/
Example:
Procedure created.
Grant succeeded.
Explain how the %NOTFOUND attribute can be used to exit from a loop.
Fetching data from the cursor- The FETCH statement retrieves the
rows from the cursor one at a time.
Example:
declare
cursor c1 is select * from departments;
r1 c1%rowtype;
begin
open c1;
loop
fetch c1 into r1;
exit when c1%notfound;
dbms_output.put_line(r1.department_id||’ ’
||r1.department_name);
end loop;
close c1;
end;
/
Example:
declare
cursor c1 is select * from departments;
r1 c1%rowtype;
begin
open c1;
if c1%notfound then
dbms_output.put_line('true');
elsif c1%notfound is null then
dbms_output.put_line('null');
else
dbms_output.put_line('false');
end if;
close c1;
end;
/
output: null
Example:
call example:
begin
create_table('employee_details',
'id number primary key, name varchar2(10)');
end;
/
No, however the subprograms which are member of the package can
be overloaded.
Example:
END emp_actions;
/
The SQL group functions, such as AVG, MIN, MAX, COUNT and SUM
cannot be directly used within PL/SQL.
However, they can be embedded within SQL statements and then can
be utilized in the procedural statements.
Example:
SQL> declare
2 maxdeptid number;
3 begin
4 select max(department_id) into maxdeptid
5 from departments;
6 dbms_output.put_line('The maximum department_id is:'
||maxdeptid);
7 end;
8 /
The maximum department_id is:270
PL/SQL procedure successfully completed.
Example:
declare
Var_bonus number;
begin
SELECT DECODE(emprating, 'D', 1500,
'C', 4000,
'B', 6000,
'A', 8000)
INTO var_bonus FROM empcopy WHERE employee_id = 115;
dbms_output.put_line(var_bonus);
end;
/
Example:
declare
v_salary number;
v_bonus number;
v_total_salary number;
begin
select salary, bonus into v_salary, v_bonus
from employees
where employee_id=115;
v_total_salary := NVL(v_salary,0) + NVL(v_bonus,0);
dbms_output.put_line(v_total_salary);
end;
/
Example:
5 rows updated.
What are the restrictions on functions that are called within SQL
statements?
1) IF ....... THEN
......
......
END IF;
2) IF ....... THEN
......
......
ELSE
......
......
END IF;
3) IF ....... THEN
......
......
ELSIF ...... THEN
......
......
END IF;
4) IF ....... THEN
......
......
ELSIF ...... THEN
......
ELSE
......
......
END IF;
Public constructs:
All constructs placed in the specification of the package are
called public constructs.
Public constructs can be accessed from outside of the package
that is from any oracle server environment that is external to
the package.
Private constructs:
Any subprogram not in the package specification but coded in the
package body is called a private constructs.
Private constructs are placed in the package body and can be
referenced only by other constructs within the same package body.
Example:
1 declare
2 cursor c1 is select * from departments;
3 r1 c1%rowtype;
4 begin
5 fetch c1 into r1;
6 end;
SQL> /
declare
*
ERROR at line 1:
ORA-01001: invalid cursor
ORA-06512: at line 5
Example:(EXIT statement)
begin
for i in 1..10 loop
if i>5 then
exit;
end if;
dbms_output.put_line(i);
end loop;
end;
/
begin
for i in 1..10 loop
exit when i>5;
dbms_output.put_line(i);
end loop;
end;
/
Example:
Example:
declare
r1 departments%rowtype;
begin
select * into r1 from departments where department_id=10;
dbms_output.put_line(r1.department_id||''||r1.department_name
||' '||r1.manager_id||' '||r1.location_id);
end;
SQL> /
10 Administration 200 1700
Example:
begin
update departments
set manager_id=125
where location_id=1800;
if sql%found then
dbms_output.put_line(' table departments is updated
successfully');
else
dbms_output.put_line(' table departments is not udpated');
end if;
end;
SQL> /
table departments is updated successfully
Oracle provides DDL triggers to audit all schema changes and can
report the exact change, when it was made, and by which user.
Using the Data Definition Language (DDL) triggers, the Oracle DBA
can automatically track all changes to the database,
including changes to tables. The data from this trigger is
especially useful for change control for the Oracle DBA.
Syntax:
ON DATABASE | SCHEMA
USER function returns the user_id from the current Oracle session.
Procedure Trigger
Defined with CREATE PROCEDURE Defined with CREATE TRIGGER
Procedure accept parameters Trigger does not accept
parameter
Invoked explicitly Invoked implicitly
COMMIT, ROLLBACK and SAVEPOINT COMMIT, ROLLBACK and SAVEPOINT
are allowed are not allowed
Example:
declare
deptname varchar2(40);
begin
select department_name into deptname
from departments
where department_id=(select department_id from employees
where employee_id=100);
dbms_output.put_line('employee 100 works in '||deptname||'
department');
end;
SQL> /
employee 100 works in Executive department
Example:
SELECT text
FROM user_source
WHERE name='RAISE_SALARY' and type='FUNCTION';
What are the different parameter modes, which can be used in the
function specification?
All parameter modes IN, OUT and IN OUT can be used in the
function specification.
Parameters modes
You can remove only package body and retain the package
specification using following syntax:
Variables of scalar data type can hold only one value, whereas a
variable of composite data type can hold multiple values of
scalar data type.
declare
type rec is record(empid number(3), fname varchar2(30),
lname varchar2(30));
r1 rec;
begin
select employee_id, first_name, last_name into r1
from employees
where department_id=10;
dbms_output.put_line(r1.empid||' '||r1.fname||' '||r1.lname);
end;
/
200 Jennifer Whalen
Example:
begin
if SQL%FOUND is null then
dbms_output.put_line('null');
end if;
insert into departments values(300,'sports',null,null);
if SQL%FOUND then
dbms_output.put_line('new department inserted successfully');
end if;
end;
/
output:
null
new department inserted successfully
desc <procedurename|functionname>;
This error occurs when you tried to execute a SQL statement that
referenced a PLSQL function that is in an invalid state. This
happens when the function is compiled with errors
Action: Check the SQL statement and the PL/SQL function for
syntax errors or incorrectly assigned, or missing, privileges
for a referenced object.
Yes, you can remove only package body and retain the package
specification using following syntax:
SQLCODE - Returns the numeric value for error code (You can
assign it to a NUMBER variable).
SQLERRM - Returns the character data containing the message
associated with the error number.
Example:
declare
insert_excep EXCEPTION; -- declare the name of exception
PRAGMA EXCEPTION_INIT(insert_excep,-01400); -- associate
begin
insert into departments(department_id, department_name)
values(280,null);
exception
when insert_excep then -– handling exception
dbms_output.put_line('Insert operation failed');
dbms_output.put_line(SQLERRM);
end;
/
Insert operation failed
DECLARE(optional)
Variables, cursors, user-defined exceptions
BEGIN(mandatory)
SQL statements
PL/SQL statements
EXCEPTION(optional)
Actions to perform when error occur
END;(mandatory)
Yes, procedures and functions are named PL/SQL blocks. They are
also known as subprograms.
These subprograms are compiled and stored in the database.
Because they are named and stored you can invoke them whenever
you want.
What is an exception?
Example:
create procedure add_dept1(dname varchar2:='Unknown',
locid number default 1800)
is
begin
insert into deptcopy values(seq1.nextval,dname,null,locid);
end add_dept1;
/
execute add_dept1;
execute add_dept1('Testing',1700);
execute add_dept1(locid=>1900);
Example:
What is RAISE_APPLICATION_ERROR?
Example:
declare
cursor c1 is select * from departments;
r1 c1%rowtype;
begin
open c1;
loop
fetch c1 into r1;
exit when c1%notfound;
open c1;
end loop;
close c1;
end;
SQL> /
declare
*
ERROR at line 1:
ORA-06511: PL/SQL: cursor already open
ORA-06512: at line 2
ORA-06512: at line 9
Example:
Sequence created.
Procedure created.
No, ELSIF and ELSE are optional in an IF statement. You can have
any number of ELSIF keywords but only one ELSE keyword in your IF
statement.
Example:
SQL> begin
2 for i in 10..2 loop
3 dbms_output.put_line(i);
4 end loop;
5 dbms_output.put_line('loop is skipped');
6 end;
7 /
loop is skipped
Procedure Function
Procedure may or may not return Function is mainly used in the
a value or may return more than case where it must return a
one value using the OUT value.
parameter.
Can not contain RETURN clause Must contain RETURN clause in
in the header the header
We cannot call procedure in We can call functions in select
select statement statement in select statement
A statement trigger:
Executes once for the triggering event
Is the default type of trigger
Fires once even if no rows are affected at all
A row trigger:
Executes once for each row affected by the triggering event.
Is not executed if the trigging event does not affect any
rows.
Is indicated by specifying the FOR EACH ROW clause.
begin
end;
The select query has been written without the into clause, which
is mandatory for a select statement in a pl/sql block. As a
result, the select query cannot get execute without assigning its
output to variable.
The SELECT INTO clause should be used when the result of the
query is known to return a single row. When more than one row is
be returned by the SQL query, then FETCH clause should be used.
If the FETCH clause is not used and the query returns more than
one row, then the runtime error is generated in the code.
statements....
END LOOP;
statements....
END LOOP;
Can the PL/SQL block process more than one exception at a time?
No, the PL/SQL block cannot handle more than one exception. It
can process only one exception handler before terminating.