Вы находитесь на странице: 1из 21

What is PL/SQL?

PL/SQL is an Oracle Procedural Language extension to SQL.


It is used for data processing.
It uses four keywords
(i) Declare
(ii) Begin
(iii) Exception
(iv) End

What are the Components in a PL/SQL block?


There are three types of components in PL/SQL block
(i) Declaration part
(ii) Execution part
(iii) Exception part

How to display server message in PL/SQL block?


By using built-in database package
DBMS_OUTPUT  It is a package.
PUT_LINE( )  Procedure of DBMS_OUTPUT package.

How to view server message output at SQL*PLUS?


set SERVEROUTPUT on;

How many parameters you can pass through put_line?


Only one parameter.

PL/SQL engine is a compiler (or) Interpreter?


Both

Forwarding referencing of variable in PL/SQL block is possible or not?


It is not possible.

What are the types of PL/SQL block?


(i) Anonymous block
(ii) Named block
What is Anonymous Block?
The PL/SQL block which has no name such PL/SQL block cannot be invoked/called.
Anonymous PL/SQL block used in
(i) Oracle Products (Forms, Reports and Graphics)
(ii) In named PL/SQL block.
PL/SQL processed by its own engine i.e PL/SQL engine.

What is the default value for numeric in a PL/SQL variable?


NULL

What is difference between Persistent and Transient?


If the Object created in a Table is called Persistent.
If the Object created in an Execution of PL/SQL block then it is called Transient.

What is Sub-Program?
A sub-program is a PL/SQL block which will be invoked by taking parameters.
There are two types of Sub-Programs
(i) Procedure
(ii) Function

What are the Anchored data types?


%TYPE -- v_EmpName emp.ename%TYPE;

%ROWTYPE -- v_emp EMP%ROWTYPE; v_emp.empno := 10; v_emp.ename := 'XXXXXXX';

What is the use of %TYPE and %ROWTYPE?


%TYPE : It is used to declare data type and size dynamically for variable in a PL/SQL block
based on single column of a Table.
%ROWTYPE : It is used to define a single variable for all columns of a Table. It is used for Cursors.

What is Execute Immediate?


Execute Immediate is the replacement of DBMS_SQL package from Oracle 8i.
It is used to implement DDL commands in PL/SQL block.
Ex: Begin
Execute Immediate('create table sample(empno number(4))');
End;
What is Cursor?
It is used to process more than one row in PL/SQL block.

What are types of Cursors?


There are two types of Cursors
(i) Implicit Cursor
(ii) Explicit Cursor

Difference between Implicit & Explicit cursors?

Implicit Explicit
1. It is defined by Oracle and used by 1. It is defined by User and used by User.
Oracle and User.
2. In this there is no need to open or 2. In this we have to open and close the
close the Cursor. Cursor

What are the Cursor Keywords?


(i) OPEN
(ii) FETCH
(iii) EXIT
(iv) CLOSE

What are the Cursor Attributes?


(i) %ISOPEN
(ii) %FOUND
(iii) %NOTFOUND
(iv) %ROWCOUNT

Is loop is mandatory for the Cursor?


No, but it will process only the first record.

What is Cursor FOR..LOOP?


It is a short cut process for Explicit Cursor.
In FOR LOOP there is no need to Open and Close the Cursor.

What is the draw back in ‘Cursor For Loop’?


Cursor attributes are not allowed in ‘Cursor For Loop’.

What is the difference between Simple Loop and For Loop?


A Simple Loop requires declaration of variables used in it and requires an Exit condition.
A For Loop does not require declaration of variables and an Exit condition.

When Cursor selects the records at Declaration [or] Open [or] Fetch?
At Open state.

At what situation we will find the SQL%ISOPEN = True?


We never find, because it is done by Oracle implicitly. It is always False.

Whether a Cursor is a Pointer or Reference?


Cursor is a Reference

What are the advanced concepts of Cursor?


Cursor with Parameter
For update
Where current of <cursor name>
Rowid
Inline view

Ex:
1. Cursor with Inline view

FOR C IN (SELECT 1 A FROM DUAL) LOOP


DBMS_OUTPUT.put_line(c.A);
END LOOP;
2. Cursor with Rowid

CURSOR empCursor IS SELECT salary, ROWID FROM emp;

FOR empCursor_rec IN empCursor LOOP


lv_new_salary_num := empCursor_rec.salary;
UPDATE employee
SET salary = lv_new_salary_num
WHERE ROWID = empCursor_rec.ROWID;
END LOOP;
3. Cursor with Where current of <cursor name>
cursor cu_emp is select * from emp fOR UPDATE of job;
FOR ro_emp IN cu_emp LOOP
DBMS_OUTPUT.put_line( 'ename/job:'||ro_emp.ename||'/'||ro_emp.JOB);
IF ro_emp.JOB='ARUN' THEN
UPDATE emp
SET job = 'SALESMAN'
WHERE CURRENT OF cu_emp;
COMMIT;
END IF;
END LOOP;

What is Cursor with Parameter?


If the cursor is defined with a parameter then it is known as parametric Cursor.
It is used to process specific rows based on the parameter condition at run-time.

What is ‘Cursor For Update’?


This clause stops the accessing of other users on a particular columns used by the Cursor until the
commit is used.

What is ‘Cursor Where Current Of’?


Where Current Of clause in an Update, Delete statement refers to the latest row fetched from a Cursor.

What are the pre-defined Packages provided by Oracle for developers?


DBMS_OUTPUT
DBMS_SQL
DBMS_DDL
UTL_FILE

What are SQLCODE and SQLERRM?


SQLCODE : It returns a value of the error number for the last error encountered.
SQLERRM : It returns the actual error message for the last error encountered.

How can we find with in a PL/SQL block if a Cursor is open?


Using %ISOPEN keyword

How to generate debugging output from PL/SQL?


Using DBMS_OUTPUT package

What is Procedure?
(i) It is mainly used for data processing.
(ii) It is a named PL/SQL.
(iii) It is a database object (user_source).
(iv) The procedure in oracle known as ‘stored procedure’.
(v) When we invoke procedure, every time not required to compile.
(vi) Procedures can be re-used many times.
(vii) It accepts parameters.
(viii) It may or may not return value depending on how we write procedure.

What are the types of Stored Procedure?


There are two types of Stored Procedure
(i) Stand alone Procedure
(ii) Packaged Procedure

What are the Advantages of Stored Procedure?


(i) Extensibility
(ii) Reusability & Maintainability
(iii) One time Compilation
What are Actual and Formal parameters?
Formal Parameter : The parameter used for defining the construct (program) is known as formal
parameter.
Actual Parameter : The parameter used to invoke/call the program is known actual parameter.

What are the types of Notations?


(i) Positional Notation
(ii) Named Notation
(iii) Mixed Notation

What are the types of Parameter Modes?


(i) IN
(ii) OUT
(iii) IN OUT

What is Overloading Procedure?


If the same Procedure name is repeated with parameters of different data types and parameters in
different positions varying number of parameters is called Overloading Procedure.
Ex: PUT_LINE
Why Create or Replace and not Drop & recreate Procedures?
By using Create or Replace, Grants are not dropped on a Procedure.

If I have an Execute Privilege on a procedure in other user schema, can I execute this procedure
even I do not have privilege on the tables which are used in the Procedure?
Yes

What is the difference between Cursor declared in a Procedure and Cursor declared in a
Package?
The Cursor declared in a Procedure will be accessed only with in that procedure where as the Cursor
declared in a Package can be accessed across the Package.

What are the advanced concepts of procedure?


(i) Synonym of procedure
(ii) Overloading procedure
(iii) Recursive procedure
(iv) Mutually recursive procedure
(v) Life span of variable

Where we can invoke the procedure?


Procedure can be invoked in
(i) Procedure
(ii) Function
(iii) Package
(iv) Trigger
(v) Anonymous block

What is Function?
(i) It is used for Calculation.
(ii) It is a named PL/SQL block.
(iii) It is a database object (user_source).
(iv) It accepts parameters like IN, OUT, IN OUT.
(v) It always returns a value.
(vi) We can invoke Function in a select statement also with some restriction.
(vii) We cannot invoke function in a select statement if function is defined with OUT (or) IN
OUT parameter (or) DML operations.

How many values return by a function at a time?


Only one.

How to invoke function in a select statement?


select fun_sal(7904) from dual;

Can we have two Functions with the same name in a PL/SQL block?
Yes, it is possible in a Package.

Can 2 Functions have same name and Input parameters, but differ only by return value data
type?
No, it is not possible.

Does Oracle support Recursive Function?


Yes.

Where we can invoke function?


We can invoke function in
Procedure
Function
Package
Trigger
Anonymous block

What is difference between Procedure and Function?

Procedure Function
1. It is used for Data processing. 1. It is used for Calculation purpose.
2. It may or may not return a value 2. In this it will return a value.
based on the program how we define.
3. It cannot be invoked in a Select 3. Function can be invoked in a Select
statement. statement.
What is a Package?
It is mainly used to hide the business logic.
It is a logical container which groups related Objects (procedures, functions, variables) together for a
common business function.
Using Package we can develop OOPs concepts by overloading Procedures and Functions.

What is Package and Package Body?


Package is a declarative part of the Procedures and Functions stored in the Package.
Package Body is the definition part of the Procedure and Function in the Package.

What are the Advantages of Package?


(i) Easy Maintenance
(ii) Better Performance
(iii) Hiding the Business logic

What are the Disadvantages of Package?


(i) Package cannot be parameterized.
(ii) Package cannot be Invoked.
(iii) Package cannot be Nested.

What is Forward declaration in a Package?


PL/SQL allows for a special sub-program declaration called forward declaration. It consists of the sub-
program specification in the package body terminated by a semicolon.
You can use forward declaration to do the following:
(i) Define sub-program in logical or alphabetical order.
(ii) Define mutually recursive sub-programs.
(iii) Group sub-programs in a Package.

Is it possible to have Procedure and Function with the same name?


No, it is not possible out side the Package.

Can we pass parameters in a Package?


Yes, we can pass parameters for Procedures or Functions in a Package.

What is Overloading and what are the restrictions?


Overloading means creating more than one Object (procedure/function) with the same name.
It is only possible in a Package and the parameter should be different.

What are the Constructs of a Procedure, Function and Package?


The Constructs of a Procedure, Function and Package are
(i) Variables and Constants
(ii) Cursors
(iii) Exceptions

What is Database Trigger?


It is a special kind of Stored Procedure that gets executed automatically when an INSERT, UPDATE
or DELETE operation takes place on a Table.
Trigger cannot be invoked on demand.
Triggers are generally used to implement Complex business logic and Auditing.

What are the parts of a Data base Trigger?


(i) Triggering Event
(ii) Triggering Restriction
(iii) Triggering Action

What are the various types of Database Triggers?


Insert/Update/Delete  Event
Before/After  Timing
Row/Statement Level  Level

How many Triggers are possible on a Table?


3 (Insert/Update/Delete) * 2 (Row/Statement) * 2 (Before/After) =12

How to combine three Triggers into one Trigger?


Using Trigger PREDICATE

Can views specified in a Database Trigger statement?


No, it is not possible to define a View in a Database Trigger statement.

What is the Sequence of Triggers in PL/SQL?


Before Statement Level
Before Row Level
After Row Level
After Statement Level

What is the difference between Table Level and Row Level triggers?
Table Level triggers executes once for each table based on Transaction where as Row Level triggers
executes once for each row.

Is it possible to use TCL statements in a Database Trigger?


Yes it is possible to use TCL statements using ‘PRAGMA AUTONOMOUS_TRANSACTION’

What is an Instead of a Trigger?


These triggers are used for Complex Views only to make possible of Insert, Update or Delete on those
Views.
What is Cascading Triggers?
When a statement in a Trigger body causes other Trigger to be fired.
Maximum 32 cascading Triggers are Possible at a time.

What is Pragma?
It is a set of rules to compiler to do specific tasks at compilation time.

What is Autonomous?
It is used to make local Transactions Autonomous so that it will not effect to any other transactions.

In which scenario we use Autonomous_Transaction in reports?


We use Autonomous_Transaction in reports for writing error message in to the database tables.

What is Mutating Error?


If the Row level triggers access the same table on which it is based then it is called Mutating error.

How to avoid Mutating Error?


Using View [or]
After/Statement Level, Before/Row Level
Is it possible to create Trigger on Complex Views?
Yes it is possible only for Row Level.

What is the Advantage of Stored Procedure over a Database Trigger?


We have control over the firing of a Stored Procedure but we didn’t have control on a Database
Trigger.

What is difference between a Trigger and Constraint?


Constraint are always TRUE where as Triggers are not always True.
Constraints has some limitations where as Trigger has no limitations.

What are the Data types in PL/SQL?


There are two types
(i) Scalar
(ii) Composite
What is difference between Scalar and Composite data type?
Scalar Composite
1. It is pre defined data type. 1. It is a user defined data type.
2. It is not having its own internal 2. It is having internal structure.
structure.
3. It holds only one value at a 3. It holds more than one value.
time. 3. It can be used only for PL/SQL
4. It can be used for column of a variables.
Table and Variables in PL/SQL. Ex: PL/SQL table, Record Type,
Ex: Number, Char, Varchar2, Date Ref -Cursor.

What is PL/SQL table?


(i) It is a User-Defined data type.
(ii) It has one column and Index.
(iii) It is used to hold values for use in later Queries or Calculation.
(iv) It is generally used in a Package.
(v) We populate the data into PL/SQL Table using cursor.

What is syntax for PL/SQL table?


Type <type_name> is Table of <data_type> index by Binary_Integer;
What are the PL/SQL attributes?
COUNT  <pl/sql_table_name>.count
EXTEND  <pl/sql_table_name>.extend(1,1)
EXISTS  <pl/sql_table_name>.exists
DELETE  <pl/sql_table_name>.delete
FIRST  <pl/sql_table_name>.first
LAST  <pl/sql_table_name>.last
NEXT  <pl/sql_table_name>(j).next
PRIOR  <pl/sql_table_name>(j).prior

What is Record Type?


(i) It is a user-defined data type.
(ii) It can have more than one column.
(iii) We provide name to each column.
(iv) Column can be initialized.
(v) It can be used for parameter.

What is the syntax for Record Type?


Type <record_name> is record
(empno number(4),
Job varchar2(15),
Sal number(7,2) );
VREC <record_name>;

What is the difference between %ROWTYPE and RECORD type?


%ROWTYPE is used when ever a query returns an entire row of a table or view.
RECORD type is used when ever a query returns columns of different tables or views and Variables.

What is Ref-Cursor?
It is a Dynamic Cursor which is used to change the action of Cursor query at run-time.
It can access more than one memory area at run-time.
It works like Pointer in C-Language.

What are the types of Ref-Cursor?


There are two types of Ref-Cursors
(i) Ref-Cursor with out return type
(ii) Ref-Cursor with return type

What are the keywords for Ref-Cursor?


There are four Keywords in Ref-Cursor
(i) OPEN
(ii) FETCH
(iii) EXIT
(iv) CLOSE

What are the Attributes for Ref-Cursor?


There are four Attributes in Ref-Cursor
(i) %ISOPEN
(ii) %FOUND
(iii) %NOTFOUND
(iv) %ROWCOUNT

What is Exception?
(i) Any error raised in exception area is known as Exception.
(ii) If exception is not handled in exception area, the whole pl/sql block terminated with
failure and all transactions will be rollback.

What are the various types of Exceptions?


There are three types of Exceptions
(i) Pre-Defined Exception
(ii) User defined Exception
(iii) Non-Predefined Exception

How to use pre-defined Exception?


(i) It is defined by Oracle
(ii) Raised by Oracle
(iii) Handled by User

Example:
declare
vsal number(7,2);
begin
select sal into vsal from emp where empno=&pno;
dbms_output.put_line(vsal);
exception
when no_data_found then
dbms_output.put_line("Invalid number...');
end;

Name some built-in Exceptions?


(i) NO_DAT_FOUND
(ii) TOO_MANY_ROWS
(iii) LOGIN_DENIED
(iv) INVALID_CURSOR
(v) CURSOR_ALREADY_OPEN
(vi) DUP_VAL_ON_INDEX
(vii) INVALID_NUMBER
(viii) PROGRAM_EEROR
(ix) VALUE_ERROR
(x) OTHERS

Can we define Exception twice in a single block?


No, it is not possible to define Exceptions twice.

How to use User-defined Exception?


(i) It is defined by User
(ii) Raised by User
(iii) Handled by User

Example:
declare
vsal number(7,2);
exp_sal exception;
vempno number(4);
begin
vempno:=&pno;
select sal into vsal from emp where empno=vempno;
if vsal>2000 then
raise exp_sal;
else
update emp set comm=sal*.20 where empno=vempno;
dbms_output.put_line(vempno||' '||'is updated');
end if;
exception
when no_data_found then
dbms_output.put_line('Invalid data...');
when exp_sal then
dbms_output.put_line('salary:'||vsal||' '||'is more for update...');
end;
How to use Non Pre-defined Exception?
(i) It is defined by User
(ii) Raised by Oracle
(iii) Handled by User

Example:
declare
vempno number(4);
exp_integrity exception;
pragma exception_init(exp_integrity,-01422);
begin
select empno into vempno from emp where deptno=30;
dbms_output.put_line(vempno);
exception
when exp_integrity then
dbms_output.put_line('stmt returns More than one row...');
end;

What is Pragma Exception_Init?


It is a program which instruct to the oracle compiler to attach user-defined exception to oracle error at
compile time.

What is an Exception and how it is differ from Error?


Any error raised in exception area is known as Exception.
Error is a Bug where as Exception is a Warning.

What is OTHERS Exception?


It is used along with one or more Exceptions handlers in a PL/SQL block.
This will handle all the errors which are not already handled in the Block.

What happens when commit is given in executable section when an error occurs?
It Rollback the transactions

What is Raise_Application_Error?
It is Oracle defined Procedure.
It is a procedure which is used to stop the execution of the process and displays the user message.
The error number must be in the range of -20,000 to -20,999

Example:
declare
vsal number(7,2);
vempno number(4);
begin
vempno:=&no;
select sal into vsal from emp where empno=vempno;
if vsal>2000 then
raise_application_error(-20001,'more salary for update');
else
update emp set comm=sal*.20 where empno=vempno;
dbms_output.put_line(vempno||' '||'comm is updated...');
end if;
exception
when no_data_found then
raise_application_error(-20001,'more salary for update');
end;

What is SQL*Loader?
It is used to transfer data from Legacy System data to Oracle database.

What is Legacy System?


It is an Offline data in data file format from Non-Oracle database.

How many files are used in SQL*Loader?


We can use 5 different files in SQL*Loader
(i) Data file
(ii) Control file
(iii) Log file
(iv) Bad file
(v) Discard file

What the Data file contains?


It contains data to be loaded.
The Data file formats are:
(i) Fixed format
(ii) Variable format

What is Fixed format?


In Data file the column position is Fixed.
What is Variable format?
In Data file column value is separated by delimiter (,).

What the Control file contains?


The Control file contains Pointers to Locations of various data files, log files.
It contains the information of
(i) Data file name with path
(ii) Table name for data loading
(iii) Loading mode (Insert/Replace/Append)
(iv) Log file name
(v) Bad file name
(vi) Discard file name

For Insert mode table must be empty or not?


Table must be empty.

What the Log file contains?


It contains the SQL*Loader execution history information like
(i) Date of running the program
(ii) Data file name
(iii) How many rows in data file
(iv) How many rows rejected
(v) How many rows inserted

What the Bad file contains?


It contains information of rejected data at the time of Execution which are rejected by SQL*Loader
(due to an error) and by Oracle (due to RDBMS rule).

What the Discard file contains?


It contains the information of rejected data at the time of Execution which are rejected by User.

What is the SQL*Loader utility?


The SQL*Loader utility is SQLLDR.exe found in bin of Oracle. It is used in two modes
(i) Direct path mode
(ii) Conventional path mode  default
What is direct path mode?
(i) In this data directly inserted into database.
(ii) No validation of RDBMS rule.

What is Conventional path mode?


(i) In this case data inserted into Tablespace.
(ii) All RDBMS rules validated.

What is the Advantages of SQL*Loader?


(i) It can load data to more than one table at a time.
(ii) It can use more than one data file at a time.
(iii) Data file & Control file can be saved in any folder.

What are the Pre-Requisites to run SQL*Loader?


To run SQL*Loader program there are three Pre-Requisites we have to follow
(i) Data file should be ready
(ii) Control file should be ready
(iii) Table should be ready

Suppose there are two columns in a table, if one column is null then how to insert?
Using ‘Trailing Nullcols’

What is UTL_FILE?
(i) It is a database Package.
(ii) It is used for loading data from legacy system to Oracle database.
(iii) It is also used for creating data files of Oracle database.
(iv) The Data file must be stored in a particular
(v) It supports validations

How to check the path of UTL_FILE?


Select value from v$parameter where name = ‘utl_file_dir’;

Which package is used for file Input/Output in Oracle?


UTL_FILE package is used for file Input/Output in Oracle

Name some methods and Procedures of file Input/Output Package?


There are 5 procedures in Utl_File package
(i) FOPEN
(ii) GET_LINE
(iii) PUT_LINE
(iv) NEW_LINE
(v) FCLOSE

What are the exceptions raised in case of UTL_FILE program?


The Utl_File exceptions are:
(i) Invalid_Path
(ii) Invalid_Mode
(iii) Internal_Error
(iv) Invalid_filehandle  for fopen,fclose

What is the difference between SQL*Loader & UTL_FILE?


SQL*Loader UTL_FILE
1. It is a Oracle utility. 1. It is a package

2. It can be used for more than 2. It can be used for one file at a time.
one file at a time.

3. No validation is required 3. Validation is required


4. Data file should not be in a 4. Data file should be in a same folder.
specific folder.
5. Performance is fast. 5. Performance is slow.
6. Generally used for huge data. 6. Generally used for less data.
7. Run at server-side only. 7. Run at Client & Server side.
8. It is only used for loading data 8. It is used for Loading & Creating
file. data file.

Вам также может понравиться