Sie sind auf Seite 1von 20

PL/SQL

PL/SQL

By Satish Gonare

PL/SQL

1.

Pl/sql block:
PL/SQL Block is a standard PL/SQL code segment. Block consists of three
parts. The following is the block structure.
Declare
Begin
Exception
End;
/

Declarative Section for variables, constants & exceptions. This section is


Optional.

Executable Section which is mandatory. This section must contain at least one
executable statement.

Exception Handlers which is optional.


Pl/sql supports only dml i.e. Insert, update, delete & select...into.
Sql functions can be referenced within a sql statement i.e. Numeric
sqrt,round,power),character(length,upper),date(add_months,months_between)
& group (avg,max,count).
2.

Packages:

Packages bundle related pl/sql types, items, and subprograms into one
container.

A package will have a specification and body stored separately in the database.

The package specification declares the types, variables, constants, exceptions,


cursors and subprograms available for use and it may also include pragmas i.e.
Instructions to the complier.

The body fully defines the cursors and so implements the specification.

The package itself cant be called, parameterized or nested.

Once written and complied, the contents can be shared by many applications.

When you call a packaged pl/sql construct for the first time, the whole
package is loaded into memory. Thus later calls to constructs in the same
package require no disk input/output.

Oracle sever stores the specification and body of a package separately in the
database. This enables us to change the definition of a program construct in
the package body without causing the oracle server to invalidate other schema
objects that call or reference the program construct.

By Satish Gonare

PL/SQL

Global and local pl/sql constructs:

Global or public: Global Or Public package constructs are those that are
declared in the package specification and defined in the package body.
Declared within the package specification and may be defined within the
package body.

Local or private: local or private package constructs are those that are
defined solely within the package body. Declared and defined within the
package body.

Advantages of packages:
a. Modularity: we can combine logically related programming structures in a
named Module.
b. Hiding information:
Only the declarations in the package specification are visible and accessible to
applications. The
package body hides the definitions of private constructs so that
only the package is affected and
not the applications or any calling programs if
the definition changes. This enables us to change the
implementation
without
having to recompile calling programs.
c. Added functionality:
Packaged global variables and cursors persist for the duration of a session
thus; they can be shared
by all subprograms that execute in the environment.
d. Performance improvement:
When we call a subprogram of a package, the first time then entire package is
loaded into memory. This way, later calls to related subprograms in the package
require no further disk i/o. packaged
subprograms
also
stop
cascading
dependencies and so avoid unnecessary compilation.
e. Over loading:

Enables us to define different subprograms with the same name.

We can distinguish the subprograms both by name and by parameters.

Sometimes the processing in two subprograms is the same, but the Parameter
passed to them varies. In that case it is logically to give them the same name.

Stand alone subprograms cannot be overloaded.

Most built-in functions are overloaded.


Ex: Function to_char (s1 date) return varchar2;
Function to_char (s2 number) return varchar2;

3.

Which package do you use to pin stored procedures in to a shared pool.

DBMS_SHARED_POOL

By Satish Gonare

PL/SQL

4.

How do you debug a pl/sql program?


1. Using dbms_output package:
This package enables developers to follow closely the execution of a
function or procedure by sending messages and values to the output buffer.
2. Autonomous procedure call (writing the output to a log table);
3. Exceptions

5.

Differences between Implicit and Explicit cursors:


Implicit cursor:

a. Are opened implicitly by oracle whenever a DML or select statement is


executed.
b. Opened, fetched, closes internally.
c. un named cursors
d. Attributes: sql%isopen, sql%found, sql%notfound, sql%rowcount.
Explicit cursor

6.

Are declared and opened explicitly by developers to manipulate


multiple rows returned by queries one by one.

Manually we have to declare, open, fetch, close it.

Name given to a context area.

Attributes:cur_name%isopen, cur_name%found, cur_name%notfound,


cur_name%rowcount.

What is a cursor? What are sql cursors attributes?


In order to process a sql statements oracle will allocate an area of memory
known as the context area. The context area contains information necessary to
complete the processing, including the number of rows processed by the select
statement, a pointer to the parsed representation of the statement, and in the
case of a query, the active set, which is the set of rows returned by the query.
A cursor is a handle or pointer to the context area.
Sql cursor attributes are:
1. SQL%ROWCOUNT: number of rows affected by the most recent sql
statement(an integer value).
2. SQL%FOUND: Boolean attribute that evaluates to true if the most recent
sql statement affects one or more rows.
3. SQL%NOTFOUND: Boolean attribute that evaluates to true if the most
recent sql statement does not affects any rows.

By Satish Gonare

PL/SQL

4. SQL%ISOPEN: always evaluates to false because pl/sql closes implicit


cursors immediately after they are executed.
7.

What is an autonomous transaction?


An autonomous transaction is a transaction that is started within the context
of another transaction, known as the parent transaction, but is independent of
it. The autonomous transaction can be committed or rolled back regardless
of the state of the parent transaction.
Autonomous transactions are use full for implementing actions that need to be
performed independently, regardless whether the calling transaction
commits or rolls back, it will not affect

the

autonomous

transaction

logging and retry counters.


Use pragma autonomous_transaction to make a transaction as autonomous.
When an autonomous transaction block calls another autonomous block or
itself, the called block does not share any transaction context with the calling
block. How ever, when an autonomous block invokes a non-autonomous
block, the called block inherits the transaction context of the calling
autonomous block.
Be careful to always terminate an autonomous transaction with a commit or
roll back. This will

otherwise produce a runtime error.

Pragma autonomous transaction:


The only way to execute an autonomous transaction is from within a pl/sql
block. The block is marked as autonomous by using a pragma in the
declarative section. It must appear in the declarative section of the block, and
only one pragma is allowed in the block.
Types of autonomous blocks:
Not all blocks can be marked as autonomous only the following are legal.

Top level anonymous blocks.

Local, stand alone, and packaged subprograms.

Methods of an object type.

Database triggers.
8.

REF CURSOR EXAMPLE


declare
type t1 is ref cursor;

By Satish Gonare

PL/SQL

v1 t1;
begin
open v1 for select * from inv;
open v1 for select * from inv2;
end;
9.

Explain the usage of WHERE CURRENT OF clause in cursors?


WHERE CURRENT OF clause in an UPDATE, DELETE statement refers to
the latest row fetched from a cursor.

10. What is Raise_application_error?


Raise_application_error is a procedure of package DBMS_STANDARD
which allows to issue
an user_defined error messages .
Syntax:
RAISE_APPLICATION_ERROR(ERROR_NUMBER,ERROR_MESSAGE,[
KEEP_ERRORS]);
WHERE error_number is a value between -20000 and -20999.
11. If we RENAME the TRIGGERING TABLE then the text in the trigger
also changed to new table name but it changes in USER_TRIGGERS not
in USER_OBJECTS.
12. CALL is a SQL statement. It is not valid inside a PL/SQL block, but it is
valid when executed using dynamic SQL, in this case , the EXECUTE
IMMEDIATE statement is used.
13. Difference between procedure and functions.
Procedure
1. used to perform some action like
insert, update, etc

Function
1. Usually used to compute and return a
Value to the calling environment.

2. may or may not return value

2. Must return value.

3. IN, OUT, INOUT can be used

3. IN, OUT, INOUT can be used

4. Cant be used in sql statements.

4. Can be used in sql statements as any


Other built in function.

5.cant be used as a part of an


expression
Similarities:

By Satish Gonare

5. Can be used as a part of expression.

PL/SQL

Both can return values.

14. Parameterized cursor:


We can pass parameters for cursor as like procedures and functions.
Syntax: cursor cursor_name[parameter_name datatype] is select statement;
The advantage of parameterized cursor is, a single cursor can be opened and
closed several times in a block, returning different active set in each occasion.
Note: formal parameters should not be mentioned with data type size.
15. What is referencing clause? Where we will use this?
Referencing clause is used in triggers. Referencing clause is used to specify a
different name for
:old and :new.
Syntax: referencing [old] as [name];
Or referencing [new] as [name];
: is not used for referencing. The colon is valid only in the trigger body.
16. What is sqlcode, sqlerrm?
Sqlcode and sqlerrm are functions for trapping exceptions.
Sqlcode: returns the numeric value of the error.
Sqlerrm: returns the text message of the error.
Note that the values of sqlcode and sqlerrm are assigned to local variables first;
then these variables are used in a sql statement. Because these functions are
procedural, they cant be used directly inside a sql statement.
17. What are stand-alone procedures?
Procedures that are not part of a package are known as stand-alone
because they independently defined. A good example of a stand-alone
procedure is one written in a SQL*Forms application.

These

types

of

procedures are not available for reference from other Oracle tools. Another
limitation of stand-alone procedures is that they are compiled at run
time, which slows execution.
18. There is a situation where u has to handle the select statement which
does not retrieve any rows. Which of the following can help u in doing
this?
1) Sql%notfound in the execution section
2) Sql%rowcount in the execution section
3) Sql%found in the execution section

By Satish Gonare

PL/SQL

4) Give an exception when no_data_foundans


19. Procedure a calls procedure B which calls procedure c .there is an
zero_divide error in procedure b , but there is no proper exception
handler in the procedure b. what would u do if u have given a provision
to handle it other than giving an exception handler in Procedure b.
1) Giving an exception handler in procedure a ans
2) Giving an exception handler in procedure c
3) No need, automatically handled
20. Which causes an infinite loop in the simple loop with out giving?
1) End loop
2) Exit ans
3) Loop
4) If condition
21. What is event trigger?
An event trigger, a segment of code which is associated with each event and is
fired when the event occurs.
22. How does a stored procedure reduce network traffic?
When a stored procedure is called, only the procedure call is sent to the server
and not the
statements that the procedure contains.
23. What are Procedure, functions and Packages?
Procedures and functions consist of set of PL/SQL statements that are grouped
together as a unit to solve a specific problem or to perform set of related tasks.
Procedures do not return values while Functions must return One Value.
Packages provide a

method of encapsulating and storing related procedures,

functions, variables and other Package Contents.


24. How many types of Exceptions are there?
There are 2 types of exceptions. They are
a. System Exceptions
Predefined
Ex: When no_data_found, When too_many_rows etc.
Non-predefined.
b. User Defined Exceptions
e.g. My_exception exception
When My_exception then

By Satish Gonare

PL/SQL

25. Can you disable database trigger which are written on table? How?
Yes. ALTER TABLE [DISABLE ALL_TRIGGER]
26. Which type of package construct must be declared both within the
package specifications and package body?
Public procedures and functions.
27. When creating stored procedures and functions, which construct allows
you to transfer values to and from the calling environment?
a. Local variables.
b. Formal arguments.
c. Boolean variables.
d. Substitution variables.
Ans:B
28. Which two tables are fused track object dependencies?
USER_DEPENDENSIES.
IDEPTREE.
29. Which type of argument passes a value from a calling environment?
IN, INOUT
30. You need to create a trigger on the EMP table that monitors every row
that is changed and places this information into the AUDIT_TABLE.
Which type of trigger do you create?
After insert or update or delete on EMP for each row.
31. You need to create a stored procedure that deletes rows from a table.
The name of the table from which the rows are to be deleted is unknown
until run time. Which method do you implement while creating such
procedure?
Use DBMS_SQL packaged routines in the procedure to delete the rows.
32. Under which situation do you create a server-side procedure?
When the procedure needs to be used by many users accessing the same
schema objects on a local database.

By Satish Gonare

PL/SQL

33. A programmer develops a procedure, ACCOUNT_TRANSACTION,


and has left your company. You are assigned a task to modify this
procedure. You want to find all the program units invoking the
ACCOUNT_TRANSACTION procedure. How can you find this
information?
a. Query the USER_SOURCE data dictionary view.
b. Query the USER_PROCEDURES data dictionary view.
c. Query the USER_DEPENDENCIES data dictionary views.
d. Set the SQL Plus environment variable trade code=true and run the
ACCOUNT_TRANSACTION procedure.
e. Set the SQL Plus environment variable DEPENDENCIES=TRUE and
run the Account_Transaction procedure.
Ans: c
34. How many triggers can be placed on a table in PL/SQL 2.1 (Oracle 7.1)
and above?
Oracle places no limit on the number of triggers that can be created on a table.
However, effects on performance should be considered.
35. Can one read/write files from PL/SQL?
Pl/sql does not have any built in capabilities to interact with the file system out
side the database; it is designed to manipulate relational data within the
database through sql. Through the TL_FILE package, however, a pl/sql
program can read and write to a file on the file system. The directory you
intend writing to has to be in your INIT.ORA file (see UTL_FILE_DIR=...
parameter) either be physically located on the same machine as the sever or
accessible to the database from it

through a directory mapping (such as

NFS). Before Oracle 7.3 the only means of writing a file was to use
DBMS_OUTPUT with the SQL*Plus SPOOL command.
Copy this example to get started:
DECLARE
fileHandler UTL_FILE.FILE_TYPE;
BEGIN
----open the file /tmp/utl_file.txt for writing. If the file dose not exist,
this will create it.
----If the file dose exist, this will over write it.
fileHandler := UTL_FILE.FOPEN('/tmp', 'utl_file.txt', 'w');

By Satish Gonare

PL/SQL

---- write some lines to the file.


UTL_FILE.PUTF(fileHandler, 'Look mam, I''m writing to a file!!!\n');
-----close the file
UTL_FILE.FCLOSE(fileHandler);
EXCEPTION
WHEN utl_file.invalid_path THEN
raise_application_error(-20000, 'ERROR: Invalid path for file or path
not in INIT.ORA.');
END;
UTL_FILE can be used for reading text files. Note that binary files cannot be
accessed using
UTL_FILE only text files are supported.
36. Can one pass an object/table as an argument to a remote procedure?
The only way to reference an object type between databases is via a database
link.
Note that it is not enough to just use "similar" type definitions. Look at this
example:
-- Database A: receives a PL/SQL table from database B
CREATE OR REPLACE PROCEDURE pcalled(TabX
DBMS_SQL.VARCHAR2S) IS
BEGIN
-- do something with TabX from database B
null;
END;
/
-- Database B: sends a PL/SQL table to database A
CREATE OR REPLACE PROCEDURE pcalling IS
TabX DBMS_SQL.VARCHAR2S@DBLINK2;
BEGIN
pcalled@DBLINK2(TabX);
END;
37. How can one search PL/SQL code for a string/ key value?
The following query is handy if you want to know where a certain table, field
or expression is referenced in your PL/SQL source code.
Select type, name, line from
'%&keyword%';

By Satish Gonare

user_source where upper (text) like

PL/SQL

38. How can one keep a history of PL/SQL code changes?


One can build a history of PL/SQL code changes by setting up an
AFTER CREATE schema (or database) level trigger (available from Oracle
8.1.7).
This way one can easily revert to previous code should someone make any
catastrophic changes.
Look at this example:
CREATE TABLE SOURCE_HIST

-- Create history table

AS SELECT SYSDATE CHANGE_DATE, USER_SOURCE.*


FROM USER_SOURCE WHERE 1=2;
CREATE OR REPLACE TRIGGER change_histStore code in hist
table
AFTER CREATE ON SCOTT.SCHEMA -- Change SCOTT to your schema
name
DECLARE
BEGIN
if dictionary_obj_type in ('procedure', 'function','package', 'package
body', 'type') then -- store old code in source_hist table
insert into source_hist select sysdate, user_source * from user_source
where type = dictionary_obj_type and name = dictionary_obj_name;
end if;
Exception
When others then
raise_application_error(-20000, SQLERRM);
END; /
39. What are the various uses of database triggers?
Database triggers can be used to enforce business rules, to maintain derived
values and perform Value-based auditing.
40. Why is an event driven program referred to a passive program?
Because an event driven program is always waiting for something to happen
before processing.
41. What are the four types of events?
1. System Events.
2. Control Events
3. User Events
4. Other Events.

By Satish Gonare

PL/SQL

42. What is mutating table and constraining table?


Mutating Table is a table that is currently being modified by an Insert,
Update or Delete statement. Constraining Table is a table that a triggering
statement might need to read either directly for a SQL statement or
indirectly for declarative Referential Integrity constraints.

43. Why u cant use a procedure in sql statement?


Because procedure itself is a statement and it cant be a part of select statement.
Procedure may not

return a value.

44. Types of cursors?


Implicit and explicit cursors. I
Implicit-1) For all dml statements (insert, update, delete)
2) Select statement which returns only one row.
in implicit cursor it implicitly opens,fetches,closes.( implicit cursor attributes
sql%notfound,sql%

found,sql%rowcount,sql%isopen)

Explicit-1) ref cursor


2) parameterised
3)for loop cursor
in explicit cursor we have to declare,open ,fetch and close (explicit cursor
attributescursorname%notfound,cursorname%found,cursorname%rowcount,c
ursorname%isopen)
45. Look out following pl/sql block
Declare
a number;
Begin
select ename into a from emp where empno = 1100;
Exception
When others then
dbms_output.put_line('others');
When no_data_found then
dbms_output.put_line('no data found');
End;
Will this compile successfully?

By Satish Gonare

PL/SQL

No. you will get error as OTHERS handler must be last among the
exception handlers of a
block
46. Create a table using a pl/sql block.
BEGIN
EXECUTE IMMEDIATE CREATE TABLE NEW_1(AGE NUMBER);
END;
47. CURSOR WITH UPDATE CLAUSE IS USED TO PERFORM LOCKS ON
DATA.
48. Restrictions for overloading?
You cannot overload two subprograms if their parameters differ only in name
or mode. The following two procedures cannot be overload, for example;
Procedure overloadme(p_s in number);
Procedure overloadme(p_s out number);
You cannot overload two function based only on their return type.
Ex: Function overloadmetoo return date;
Function overloadmetoo return number;
Finally, the parameter of overload functions must differ by type familyyou
cannot overload on the same family.
49. Explain Bulk bind:
Sql statements in pl/sql blocks are sent to the sql engine to be executed. The
sql engine can in turn send data back to the pl/sql engine (as the result of a
query). In many cases, data to be inserted or updated in the database is first
put into a pl/sql collection, and then this collection is iterated over with a for
loop to send the information to the sql engine. This results in a context switch
between pl/sql and sql for each row in the collection.
Oracle 8i and higher allow you to pass all rows in a collection to the sql engine
in one operation, eliminating all but one context switch. This is known as bulk
bind, and is done with the FORALL statement.
50. Types of triggers.
There are three main kind of triggers :
a. DML
b.Instead of and

By Satish Gonare

PL/SQL

c. System triggers
51. Types of DML triggers
There are a total of 12 possible types; (9i ): 3 statements * 2 timing * 2 leveles
Statement: Insert, Delete, or Update.
Timing: Before or After
Level: Row or Statement.
52. NOTE:
Function:
If we ALTER a table on which a function is based, then the function
becomes INVALID.
If we ALTER a table and DROP a table column, function becomes
INVALID. If we are using that column in function body and we
invoke such function then function will give error as 'Function is
invalid'. Else function gets compiled and executed and it becomes
VALID.
If we DROP a table on which a function is based on, then function
becomes INVALID. If we recreate the same table and if we invoke
the function then function again gets compiled internally and
executed and it becomes VALID.
If we RENAME a table, function becomes INVALID. It becomes
VALID when we rename it to original one and invoke the function.
Same applies to Procedures and Packages.
VIEWS :(SAME above rules applies to view also)
If we alter a table on which a view is created then view becomes
INVALID. If we invoke such a view then view again gets compiled
and executed and then it becomes valid.
Create view demo1 as select * from demo; (if demo has two columns
col1, col2) Here view will be stored as select col1, col2 from demo in
data dictionary. So if we alter a table and add new columns then view
becomes invalid and after compilation it becomes valid and it will
show only two previous columns which it contains.
If we ALTER a table and drop a column which is there in select
statement of view then view becomes INVALID. If we invoke such a

By Satish Gonare

PL/SQL

view it will give 'view SANJAY.DEMO1" has errors'. else view gets
compiled and gets executed and becomes valid.
If we DROP a table then view becomes invalid. If we invoke such a
view it gives 'ORA-04063: view "SANJAY.DEMO1" has errors'. If we
recreate the same table then view becomes valid on invoking it.
If we RENAME a table view becomes INAVLID. It becomes VALID
when we rename it to original one and invoke the function.
TRIGGERS:
If we alter a table, then its respective trigger becomes INVALID. If that
trigger is invoked (as a result of insert, update, delete on table) then
trigger gets revalidated and becomes VALID.
If we alter a table and drop a column, then its respective trigger
becomes INVALID. If that trigger is invoked and if the dropped
column is referenced in trigger(:new, :old) then Trigger gives
error'SANJAY.INV2_STOCK2' is invalid and failed re-validation'.
Else it becomes valid.
If we rename a table then its respective triggers will become invalid.
When trigger is invoked it becomes valid and work as usual and
table_name will be changed in user_triggres data dictionary.(this is not
so in functions, views etc.)
If a table is dropped its trigger will also get dropped.
53. DIFFERENCE BETWEEN 8I & 9I & 10g
1. In 8i we use snapshots. Those are replaced by materialized views in 9i.
2. In 9I partitioning of tables introduced.
3. In 9i Timestamp datatype is introduced in pl/sql.
4. In 10g multiple inserts into different tables using a single statement is used.
5. In 10g regular expressions introduced.
6. Till 7i the max no. of columns in a table are 256, but this extended to 1000
in 8i.
7. Till 7i generally we use set operators these are replaced by joins & sub
queries in 8i.
8. In subprograms a compiler hint known as NOCOPY is added in 8i, but this
is not there in prior versions.
SYNTAXES
CURSORS

By Satish Gonare

PL/SQL

EXPLICIT CURSORS:
CURSOR cursor_name[parameter_list]
[RETURN return_type]
IS query
[FOR UPDATE[OF (column_list)][NOWAIT]];
Where,
Cursor _name can be any valid identifier
PARAMETER_LIST is optional and can be any valid parameter used
for query execution.
RETRUN clause is optional, specifies the type of data to be returned
as defined by return_type.
FOR UPDATE clause is optional, locks the records when cursor is
opened. Records are still available to other session as READ ONLY.
OPEN THE CURSOR
OPEN cursor_name[(parameter_values)];
open command prepares the cursor for use.
FETCH RECORDS FROM THE CURSOR
FETCH cursor_name into VARiable_name(s)| PL/SQL_record;
FETCH retrieves records from the context area into a variable so that it can be
used.
CLOSE THE CURSOR
CLOSE cursor_name;
always close the cursor to intentionally introducing a memory leak into code.
until the cursor is closed, the memory is not released. Whenever cursor
attributes are used then the block is turned to implicitly cursor. Then no
need to close the cursor.
ERROR HANDLING
WHEN exception_name THEN
sequence_of_statement1;
WHEN exception_name THEN
sequence_of_statement2;
WHEN OTHERS THEN
sequence_of_statement3;
END;

By Satish Gonare

PL/SQL

(when others will trap all exceptions , be there user defined or predefined)
SQLERM returns 1,SQLERM "User-defined Exception"
PRAGMA
EXCEPTION_INIT(exception_name,oracle_error_number);
pragma must be in declarative section,
pragma is complier directive(over ridding the usual process of oracle)
RAISE_APPLICATION_ERROR

(error_number,error_message,

[keep_errors)
This can is built-in function to create your own error messages
error_number- values between -20,000 and -20,999
error_message is the text associated with this error,(512-characters),
keep_errors is a boolean values.
PROCEDURES
CREATE OR REPLACE PROCEDURE procedure_name[parameter_list]
[arguments>(IN , OUT, IN OUT) , (NO COPY), TYPE]
AS or IS
/* Declarative section is here*/
BEGIN
/*Executable section is here*/
EXCEPTION
/*Exception section is here*/
END [procedure_name];
PROCEDURE_NAME is the name of the procedure to be created, argument is
the name of the procedure parameter, type is the type of the associated
parameter procedure_body is a pl/sql block that makes up the code of the
procedure.
FUNCTION
CREATE OR REPLACE FUNCTION function_name[parameter_list]
RETURN(return_type)
[arguments>(IN , OUT, IN OUT) , (NO COPY), TYPE]
AS or IS
/* Declarative section is here*/
BEGIN
By Satish Gonare

PL/SQL

/*Executable section is here*/


EXCEPTION
/*Exception section is here*/
(return_type);
END [procedure_name];
Function is very similar to a procedure. Both take parameters, which can be of
any mode.
Both can be stored in the database or declared within the block. However a
procedure call is pl/sql statement by itself, while a function call is called

as

part of an expression.
PACKAGE:
CREATE OR REPLACE PACAGE package_name
IS or AS
/*package_body*/
package_name is the name of the package.
elements within the package (procedure and function specifications, variables,
and so on) are the

same as they would be in the declarative section of an

anonymous block. the same syntax rules

apply for a package header as for

a declarative section, except for procedure and function

declarations,

PACKAGE INITILAZATION
CREATE OR REPLACE PACKAGE BODY package_name[IS| AS]
-------------BEGIN
Initialize
tion_code;
END package_name;
Where package_name is the name of the package and initialization_code is the
code to be run .
TRIGGERS
CREATE [OR REPLACE] TRIGGER trigger_name
(before| after|instead of) triggering_event
[referencing_clause]
[WHEN trigger_condition]
[FOR EACH ROW]

By Satish Gonare

PL/SQL

trigger_body;
Where trigger_name is the name of the trigger, trigger_event specifies the
event that fires the trigger(possibly including a specific table or view), and
trigger_body is the main code for the trigger.The

refencing_clause

is

used to refer to the data in the row currently being modified by a different
name. The trigger _condition in the WHEN clause, if present, is evaluated
first, and the body of the trigger is executed only when this condition
evaluates to TRUE.
trigger_body is a pl/sql bolck, which must contain at least an executable
section.
where declarative and exception handling sections are optional. if there is a
declarative section,

however, the trigger_body must begin with the

DECLARE keyword.
CREATE [OR REPLACE] TRIGGER [SCHEMA.] trigger_name
(before | after)
(ddl_event_list | database_event_list)
ON (DATABASE | [schema.] SCHEMA)
[when_clause]
trigger_body;
system triggers fires on two different kinds of events :DDL or database.
DDL events include create, alter,or drop statements, while database events
include startup/shutdown of the server, logon/logoff of a user, and a server
error.
where ddl_event_list is one or more DDL events (separated by the or
keyword), and database_event_list is one or more database events (separated
by the OR keyword)

By Satish Gonare

Das könnte Ihnen auch gefallen