Vous êtes sur la page 1sur 20

1.What are ref cursors ?

They are similar to a normal curson but they can have different
select stmts at run time.

2.What is the difference between a reference cursor and


normal cursor ?

Ans 1
REF cursors are different than your typical, standard cursors. With
standard cursors, you know the cursor's query ahead of time. With
REF cursors, you do not have to know the query ahead of time.
With REF Cursors, you can build the cursor on the fly

Ans2
Normal Cursor is a Static Cursor.
Refernce Cursor is used to create dynamic cursor.
There are two types of Ref Cursors:
1. Weak cursor and 2.Strong cursor
Type ref_name is Ref cursor [return type]
[return type] means %Rowtype
if Return type is mentioned then it is Strong cursor else weak
cursor
The Reference cursor does not support For update clause

3.Can e truncate some of the rows from the table instead of


truncating the full table.

No , it is not possible. Truncate is a DDL Command and we can not


where clause in Truncate.

4.can i write plsql block inside expection.


Yes you can write PL/SQL block inside exception section. Suppose you want to
insert the exception detail into your error log table, that time you can write insert into
statement in exception part. To handle the exception which may be raised in your
exception part, you can write the PL/SQL code in exception part.

5.What happens when a package is initialized ?

when a package is initialised that is called for the first time the
entire package is loaded into SGA and any variable declared in the
package is initialises.

6.What are the restrictions on Functions ?

Ans1

1).Only in parameters are allowed.No inout & out parameters are


allowed.
(2).A DML statement containing a function cannot perform another
DML operation on the same table.

Ans2

We can use IN and IN OUT parameters with function, in Oracle 9i


ver. 9.2. See below,
create or replace function f1 (num in out number) return number
as
begin
dbms_output.put_line ('Inside f1 num='||num);
num:=100;
return 10;
end;
/
declare
n1 number := 1;
begin
dbms_output.put_line('return val='||f1(n1));
dbms_output.put_line('out param='||n1);
end;
/
7.What type of binding is PL/SQL?

The assigning of values to PL/SQL variables in SQL statements is


called binding. PL/SQL binding operations fall into three categories:
• in-bind
• out-bind
• define
A DML statement can transfer all the elements of a collection in a
single operation, a process known as bulk binding. If the collection
has 20 elements, bulk binding lets you perform the equivalent of
20 SELECT, INSERT, UPDATE, or DELETE statements using a single
operation. This technique improves performance by minimizing the
number of context switches between the PL/SQL and SQL engines.
With bulk binds, entire collections, not just individual elements, are
passed back and forth.

8.Why Functions are used in oracle ? Can Functions Return


more than 1 values? Why Procedures are used in oracle ?
What are the Disadvantages of packages? What are the
Global Variables in Packages?

Ans1.

1) A function will return a value, adding to that , it is possible to use


a function in SQL statements, whereas procedures cannot be
used.2)No, functions cannot retrun more than one value, instead
one can get this done in other way round by using OUT parameters
for a function.3)To perform validations and transactions on
database tables.4)Memory usage is more.5)The variable defined
inside Package Specification are treated as Global variables, which
can be referred externally.

Ans2

Both functions and procedures are using to do a special task or


action, In functions it is must to return a single value, where as in
procedures its not compulsory, In functions with the help of OUT
parameter we can pass multiple outputs.
The functions can be used in DML statements , but it should
satiesfy the function purity level. but procedures cant use in DML
statements
9.Can we use commit or rollback command in the exception
part of PL/SQL block?
Ans1.
Yes, we can use the TCL commands(commit/rollback) in the
exception block of a stored procedure/function. The code in this
part of the program gets executed like those in the body without
any restriction. You can include any business functionality
whenever a condition in main block(body of a proc/func) fails and
requires a follow-thru process to terminate the execution
gracefully!

Ans2
Yes we can use commit/rollback in exception part. Example:
DECALRE
…..
BEGIN
…….
EXCEPTION
WHEN NO_DATA_FOUND THEN

INSERT INTO err_log(


err_code, code_desc)
VALUES(‘1403’, ‘No data found’)

COMMIT;
RAISE;
END

1
10.can procedures have parameters
Yes PROCEDURE definitely possess parameters.It access the
parameters through the parameter components IN, OUT , INOUT

11.Talk about "Exception Handling" in PL/SQL?

Ans1.
There are 2 types of Exception Handlers:ORACLE defined(Named
Exception Handler and Numbered Exception Handler) and User
Defined Exceptions.
Named Exceptions : examples- LOGIN_DENIED,TOO_MANY_ROWS
etc.
Numbered Exceptions: There are about 20 K numbered
exceptions, one can overwrite these numbered exception such that
instead of showing a number for the raised exception, user
specifed EXCEPTION_NAME will be shown.
User Defined: These are the exceptions defined and raised by user
code. This includes Business Rule validations like (Amount < 0) etc.

Ans2.
the exception are written to handle the exceptions thrown by
programs.
we have user defined and system exception.
user defined exception are the exception name given by user
(explicitly decalred and used) and they are raised to handle the
specific behaviour of program.
system exceptions are raised due to invalid data(you dont have to
deaclre these). few examples are when no_data_found, when
others etc.

12.What is difference between a PROCEDURE & FUNCTION ?

Ans1.
A function always return a values while procedure can return one or more
values through Parameters.
A function can call directly by sql statement like select "func_name" from
dual while procedure cannot

Ans2.
We can use DDL in Procedure using Execute Immediate statement while tht
is not possible in functions.

13.What is Overloading of procedures ?

The Same procedure name is repeated with parameters of different


datatypes and parameters in different positions, varying number of
parameters is called overloading of procedures.
e.g. DBMS_OUTPUT put_line

14.Packages -
The main advantages of packages are -
1- Since packages has specification and body separate so,
whenever any ddl is run and if any proc/func(inside pack) is
dependent on that, only body gets invalidated and not the spec. So
any other proc/func dependent on package does not gets
invalidated.
2- Whenever any func/proc from package is called, whole package
is loaded into memory and hence all objects of pack is availaible in
memory which means faster execution if any is called. And since we
put all related proc/func in one package this feature is useful as we
may need to run most of the objects.
3- we can declare global variables in the package

15.What is PL/SQL ?

PL/SQL is Oracle's Procedural Language extension to SQL. PL/SQL's


language syntax, structure and datatypes are similar to that of
ADA. The language includes object oriented programming
techniques such as encapsulation, function overloading,
information hiding (all but inheritance), and so, brings state-of-the-
art programming to the Oracle database server and a variety of
Oracle tools.

16.What is the basic structure of PL/SQL ?


PL/SQL uses block structure as its basic structure. Anonymous
blocks or nested blocks can be used in PL/SQL

A PL/SQL block has three parts:


a declarative part,
an executable part,
and an exception-handling part.

First comes the declarative part, in which items can


be declared. Once declared, items can be manipulated in the executable part.
Exceptions raised during execution can be dealt with in the exception-handling
part.

17.What are the components of a PL/SQL block ?


PL/SQL Block contains :
Declare : optional
Variable declaration
Begin : Manadatory
Procedural statements.
Exception : Optional
any errors to be trapped
End : Mandatory
So only begin and end keywords are required, as needed, to process
a pl/sql block.

18.What are the datatypes available in PL/SQL ?

Some scalar data types such as NUMBER, VARCHAR2, DATE, CHAR,


LONG, BOOLEAN.
Some composite data types such as RECORD & TABLE.

19.What are % TYPE and % ROWTYPE ? What are the


advantages of using these over datatypes
% TYPE provides the data type of a variable or a database column
to that variable.

% ROWTYPE provides the record type that represents a entire row


of a table or view or columns selected in the cursor.

The advantages are : I. Need not know about variable's data type
ii. If the database definition of a column in a table changes, the
data type of a variable changes accordingly.

20.What is difference between % ROWTYPE and TYPE


RECORD ?

% ROWTYPE is to be used whenever query returns a entire row of a


table or view.
TYPE rec RECORD is to be used whenever query returns columns of
different
table or views and variables.
E.g. TYPE r_emp is RECORD (eno emp.empno% type,ename emp
ename %type);
e_rec emp% ROWTYPE
cursor c1 is select empno,deptno from emp;
e_rec c1 %ROWTYPE.

21.What is PL/SQL table ?

Ans1.
Objects of type TABLE are called "PL/SQL tables", which are
modeled as (but not the same as) database tables, PL/SQL tables
use a primary PL/SQL tables can have one column and a primary
key.

Ans2.
A PL/SQL table is a one-dimensional, unbounded, sparse collection
of homogenous elements, indexed by integers
One-dimensional

A PL/SQL table can have only one column. It is, in this way, similar
to a one-dimensional array.

Unbounded or Unconstrained

There is no predefined limit to the number of rows in a PL/SQL


table. The PL/SQL table grows dynamically as you add more rows to
the table. The PL/SQL table is, in this way, very different from an
array.
Related to this definition, no rows for PL/SQL tables are allocated
for this structure when it is defined.
Sparse

In a PL/SQL table, a row exists in the table only when a value is


assigned to that row. Rows do not have to be defined sequentially.
Instead you can assign a value to any row in the table. So row 15
could have a value of `Fox' and row 15446 a value of `Red', with no
other rows defined in between.
Homogeneous elements

Because a PL/SQL table can have only a single column, all rows in a
PL/SQL table contain values of the same datatype. It is, therefore,
homogeneous.
With PL/SQL Release 2.3, you can have PL/SQL tables of records.
The resulting table is still, however, homogeneous. Each row simply
contains the same set of columns.
Indexed by integers

PL/SQL tables currently support a single indexing mode: by


BINARY_INTEGER. This number acts as the "primary key" of the
PL/SQL table. The range of a BINARY_INTEGER is from -231-1 to
231-1, so you have an awful lot of rows with which to work
Keep the following restrictions in mind when you work with PL/SQL
tables:
• There is no concept of transaction integrity with PL/SQL
tables. You cannot commit information to a PL/SQL table or
roll back changes from the table.
• You cannot SELECT from PL/SQL tables. There is no way to
perform set-at-a-time processing to retrieve data from a
PL/SQL table. This is a programmatic construct in a
programmatic language. Instead you can use PL/SQL loops to
move through the contents of a PL/SQL table, one row at a
time.
• You cannot issue DML statements (INSERTs, UPDATEs, and
DELETEs) against PL/SQL tables (though PL/SQL Release 2.3
does offer a DELETE operator).

22.What is a cursor ? Why Cursor is required ?

Cursor is a named private SQL area from where information can be


accessed. Cursors are required to process rows individually for
queries returning multiple rows.

Cursor is a named private SQL area also called as context area


from where information can be accessed. Cursors are required to
process rows individually for queries returning multiple rows. there
are three types of cursors
1. Static Cursor
* Implicit Cursor
* Explicit Cursor
2. Dynamic Cursor
3. Reference Cursor

23.Explain the two type of Cursors ?

There are two types of cursors, Implicit Cursor and Explicit Cursor.
PL/SQL uses Implicit Cursors for queries.
User defined cursors are called Explicit Cursors. They can be
declared and used.
Implicit cursor: implicit cursor is a type of cursor which is
automatically maintained by the Oracle server itself.implicit
cursor returns only one row.
Explicit Cursor: Explicit Cursor is defined by the Proframmer,and it
has for phases:declare,open,fetch and close.explicit Cursor
returns more than one row

24.What are the PL/SQL Statements used in cursor


processing ?

DECLARE CURSOR cursor name, OPEN cursor name,FETCH cursor


name INTO
or Record types, CLOSE cursor name.

25.What are the cursor attributes used in PL/SQL ?

Ans1.
%ISOPEN - to check whether cursor is open or not
% ROWCOUNT - number of rows fetched/updated/deleted.
% FOUND - to check whether cursor has fetched any row. True if
rows are fetched.
% NOT FOUND - to check whether cursor has fetched any row. True
if no rows are featched.
These attributes are proceeded with SQL for Implicit Cursors and
with Cursor name for Explicit Cursors.

Ans2.
For Implicit Cursors:-
%Found - Returns TRUE,If the last DML Statement affect atleast 1
row otherwise FALSE.
%NotFound - Returns TRUE If last DML Statement doesn't affect
atleast 1 row otherwise FALSE.
%Rowcount - Returns number of rows affected by the last DML
statement.
%Isopen - Returns TRUE if the cursor is open otherwise FALSE. Its
always returns FALSE incase of implicit cursor. Because after
executing the DML statement Oracle Server automatically close
the cursor.
For Explicit Cursors:-
%Found - Returns TRUE if the Last fetch returns a Row otherwise
FALSE.
%NotFound - Returns TRUE if the last fetch doesn't return any row
otherwise FALSE.
%Rowcount - Returns number of rows returned so far from the
active set.
%Isopen - Returns TRUE if the cursor is open otherwise FALSE.

26.What is a cursor for loop ?

Cursor for loop implicitly declares %ROWTYPE as loop index,opens


a cursor, fetches rows of values from active set into fields in the
record and closes
when all the records have been processed.
eg. FOR emp_rec IN C1 LOOP
salary_total := salary_total +emp_rec sal;
END LOOP;

27.What will happen after commit statement ?


Cursor C1 is
Select empno,
ename from emp;
Begin
open C1; loop
Fetch C1 into
eno.ename;
Exit When
C1 %notfound;-----
commit;
end loop;
end;

The cursor having quer as SELECT .... FOR UPDATE gets closed after
COMMIT/ROLLBACK.

The cursor having query as SELECT.... does not get closed even
after COMMIT/ROLLBACK.

28.What is a database trigger ? Name some usages of


database trigger ?
Ans1.

Database trigger is stored PL/SQL program unit associated with a


specific database table. Usages are Audit data modifications, Log
events transparently, Enforce complex business rules Derive
column values automatically, Implement complex security
authorizations. Maintain replicate tables

Ans2.
A database trigger is a stored procedure that is invoked
automatically when a predefined event occurs.
Database triggers enable DBA's (Data Base Administrators) to
create additional relationships between separate databases.
For example, the modification of a record in one database could
trigger the modification of a record in a second database.
29.How many types of database triggers can be specified
on a table ? What are they ?
Insert Update Delete

Before Row o.k. o.k. o.k.

After Row o.k. o.k. o.k.

Before Statement o.k. o.k. o.k.

After Statement o.k. o.k. o.k.

If FOR EACH ROW clause is specified, then the trigger for each Row
affected by the statement.

If WHEN clause is specified, the trigger fires according to the


returned Boolean value.

Ans2.
There are five types of database triggers
1. Row Level Trigger
2. Statement Level Trigger
3. Instead of Trigger
4. Schema Trigger
5. Database Trigger
Out of these five types of triggers Only two are used for the table
purpose, i.e. two row level trigger and statement level triggers are
used for insert, update or/and delete operation on table

30.What is an Exception ? What are types of Exception ?

Ans1.
Exception is the error handling part of PL/SQL block. The types are
Predefined and user defined. Some of Predefined exceptions are.
CURSOR_ALREADY_OPEN
DUP_VAL_ON_INDEX
NO_DATA_FOUND
TOO_MANY_ROWS
INVALID_CURSOR
INVALID_NUMBER
LOGON_DENIED
NOT_LOGGED_ON
PROGRAM-ERROR
STORAGE_ERROR
TIMEOUT_ON_RESOURCE
VALUE_ERROR
ZERO_DIVIDE
OTHERS.

Ans2.
Exception is nothing but Error. In PL/SQL block we handle these
errors in Exception block.There are two types of exceptions: 1>
System define exception and 2> User define exception. System
define exception predefine exceptions like
'ZERO_DEVIDE',NO_DATA_FOUND etc.User define exceptions is
defined by the user and raise in the excution block and then call
into the exception block

31.What is Pragma EXECPTION_INIT ? Explain the usage ?

Ans1.
The PRAGMA EXECPTION_INIT tells the complier to associate an
exception with an oracle error. To get an error message of a specific
oracle error.
E.g. PRAGMA EXCEPTION_INIT (exception name, oracle error
number)

Ans2.
Pragma exception_init Allow you to handle the Oracle predefined
message by you'r own message. means you can instruct compiler
toassociatethe specific message to oracle predefined message at
compile time.This way you Improve the Readbility of your
program,and handle it accoding to your own way.
It should be declare at the DECLARE section.
example
declare
salary number;
FOUND_NOTHING exception;
Pragma exception_init(FOUND_NOTHING ,100);
begin
select sal in to salaryfrom emp where ename ='ANURAG';
dbms_output.put_line(salary);
exception
WHEN FOUND_NOTHING THEN
dbms_output.put_line(SQLERRM);
end;

32.What are the return values of functions SQLCODE and


SQLERRM ?

Ans1.
SQLCODE returns the latest code of the error that has occurred.
SQLERRM returns the relevant error message of the SQLCODE.

Ans2.
Pl / Sql Provides Error Information via two Built-in functions,
SQLCODE & SQLERRM.
SQLCODE Returns the Current Error Code.
Returns 1.
SQLERRM Returns the Current Error Message Text.
Returns " User Defined Exception "
33.What is a stored procedure ?

A stored procedure is a sequence of statements that perform


specific function.

Stored Procedure
A program running in the database that can take complex actions based on the
inputs you send it. Using a stored procedure is faster than doing the same work on
a client, because the program runs right inside the database server. Stored
procedures are nomally written in PL/SQL or Java

34.What are the modes of parameters that can be passed


to a procedure ?

1.In:
in parameter mode is used to pass values to subprogram when
invoked.
2.Out:
out is used to return values to callers of subprograms
3.In out:
it is used to define in and out

35.What are the two parts of a procedure ?


Procedure Specification and Procedure Body.

36.Give the structure of the procedure ?


PROCEDURE name (parameter list.....)
is
local variable declarations

BEGIN
Executable statements.
Exception.
exception handlers

end;

37.Give the structure of the function ?

FUNCTION name (argument list .....) Return datatype is


local variable declarations
Begin
executable statements
Exception
execution handlers
End;

38.Explain how procedures and functions are called in a


PL/SQL block ?

Function is called as part of an expression.


sal := calculate_sal ('a822');
procedure is called as a PL/SQL statement
calculate_bonus ('A822');

39.What are two parts of package ?

The two parts of package are PACKAGE SPECIFICATION & PACKAGE


BODY.

Package Specification contains declarations that are global to the


packages and local to the schema.
Package Body contains actual procedures and local declaration of
the procedures and cursor declarations.
40.What is difference between a Cursor declared in a
procedure and Cursor declared in a package specification ?

A cursor declared in a package specification is global and can be


accessed by other procedures or procedures in a package.
A cursor declared in a procedure is local to the procedure that can
not be accessed by other procedures.

41.How packaged procedures and functions are called from


the following?
a. Stored procedure or anonymous block
b. an application program such a PRC *C, PRO* COBOL
c. SQL *PLUS

.Ans.
a..
PACKAGE NAME.PROCEDURE NAME (parameters);
variable := PACKAGE NAME.FUNCTION NAME (arguments);
EXEC SQL EXECUTE
b.
BEGIN
PACKAGE NAME.PROCEDURE NAME (parameters)
variable := PACKAGE NAME.FUNCTION NAME (arguments);
END;
END EXEC;

c.
EXECUTE PACKAGE NAME.PROCEDURE if the procedures does
not have any out/in-out parameters. A function can not be called.

42. Name the tables where characteristics of Package,


procedure and functions are stored ?

User_objects, User_Source and User_error

43.How to use comand "COMMIT"and "ROLLBACK" in a


transacsion?
For commit simply run the command : commit;it will save all the
operation you have performed on the database.After commit you
can't go back to previous state.A rollback is used to go back to
previous commit.That is if you made some change on database
using DML and if you want to go back to the previous state from
where you have first started changing then use the command :
rollback;It should be kept in mind that after every DDL command
there is a autocommit by oracle server.A rollback can't change
it.Suppose you create a table and after that if you execute rollback
then nothing will happen.

Vous aimerez peut-être aussi