Sie sind auf Seite 1von 23

302:RDBMS [MES-BCA] PLSQL BLOCK

Trial version of Okdo Ppt to Word Converter. http://www.okdosoft.com


PL/SQL
PL/SQL [Procedural
[PROCEDURALLanguage/ Structured
LANGYAGE/ Query Language]
STRUCTURED QUERY
LANGUAGE]

Though SQL, is the natural language of the DBA, it suffers from various
inherent disadvantages, when used as a conventional programming language.

** No Procedural capabilities
** Decrease the speed of data processing
** No facility of programmed handling of errors.

Although SQL is a very powerful tool, it’s set of disadvantages are eliminated
in PL/SQL.

SQL

** SQL stands for Structured Query Languages.

** SQL is a Non-procedural / Declarative language and is always


executed on the database server.

• PL/SQL is Oracle’s Procedural Language extension to SQL i. e. PL/SQL


is a superset of SQL.
• Unlike SQL, PL/SQL is a procedural, not declarative.
• PL/SQL also enables you to embed SQL statements within its
procedural code.

PL/SQL

SQL Plus

• SQL Plus is an interactive program that allows you to type in and


execute SQL statements.

• It also enables you to type in and execute PL/SQL code and is one of
the most common front ends used to develop and create stored
PL/SQL procedures and functions.

• You can use the SQL*Plus (pronounced "sequel plus") program in


conjunction with the SQL database language and its procedural
language extension, PL/SQL. The SQL database language allows you to
store and retrieve data in ORACLE. PL/SQL allows you to link several
SQL commands through procedural logic.

Relationship of
1
302:RDBMS [MES-BCA] PLSQL BLOCK

SQL*Plus, PL/SQL and Oracle.

Getting Started with PL/SQL

PL/SQL is referred to as a block Structured language.

What is PL/SQL Block ?

A PL/SQL block is a syntactical unit that might contain program code,


variable declarations, error handlers, procedures, functions and even other
PL/SQL blocks.

Sections of a PL/SQL Block

The minimum sections of a PL/SQL block are :


<1> The Declare Section
<2> The Begin Section
<3> The Exception Section
<4> The End Section

General diagram of a PL/SQL Block

2
302:RDBMS [MES-BCA] PLSQL BLOCK

PL/SQL Datatypes

Major Categories of Datatypes :


• SCALAR
• COMPOSITE
• REF
• LOBs
Note :
You may use %TYPE and %ROWTYPE attribute
for integration with the type define in your data table.
Also SUBTYPE can be used to declare variable of
USER DEFINED TYPE.

PL/SQL Datatypes

• SCALAR TYPE :
• Seven Families OF Scalar Type :
• NUMERIC FAMILY
• CHARACTER FAMILY
• DATE FAMILY
• BOOLEAN FAMILY
• RAW FAMILY
• ROWID FAMILY
• TRUSTED FAMILY

PL/SQL Datatypes

• NUMERIC FAMILY :
• BINARY_INTEGER
3
302:RDBMS [MES-BCA] PLSQL BLOCK

• DEC
• DOUBLE PRECISSION
• INTEGER
• NATURAL
• NUMBER
• POSITIVE
• REAL
• SMALLINT etc.

4
302:RDBMS [MES-BCA] PLSQL BLOCK

IF Statement :

The IF Statement lets you execute a sequence of statements conditionally.


It is used to test the condition and then taking an appropriate action.

Syntax : [1] Simple IF Statement

IF <Condition> THEN
<Action>
END IF;
Conditional control in PL/SQL

Syntax :
[1] Simple IF Statement
IF <Condition> THEN
<Action>
END IF;

[2] IF-THEN-ELSE

IF <Condition> THEN
<Action>
ELSE
<Action>
END IF;
[3] IF-THEN-ELSIF
IF <Condition> THEN
<Action>
ELSIF <Condition> THEN
<Action>
ELSE
<Action>
END IF;

Conditional control in PL/SQL


Iterative control indicate ability to repeat or skip sections of a code block.

PL/SQL supports mainly three staemetns,viz.

<1> The LOOP Statement


<2> The WHILE LOOP Statement
<3> The FOR LOOP statement
Iterative Control

5
302:RDBMS [MES-BCA] PLSQL BLOCK

The simplest form of LOOP statement is the basic (or infinite) loop. which
encloses a sequence of statements between the keywords LOOP and END
LOOP.

Syntax :

LOOP
Sequence_of_Statements;
END LOOP;
LOOP Statement

The WHILE-LOOP

The WHILE-LOOP statement associate a condition with a sequence of


statements enclosed by the keywords LOOP and END LOOP, as follows :

Syntax :

WHILE <Condition>
LOOP
<Action>
END LOOP;
WHILE-LOOP

FOR LOOP

It is used to perform a fixed number of iterations.

Syntax :

FOR variable IN [REVERSE] Start..end


LOOP
<Action>
END LOOP;

FOR-LOOP

Trial version of Okdo Ppt to Word Converter. http://www.okdosoft.com


A series of one or more SQL statements that are logically related, or a series of
operations performed on Oracle table data is termed as a Transaction.

Oracle treats this logical unit as a single entity.

A transaction begins with the first executable SQL statement after a commit,
rollback or connection made to the Oracle engine.

ORACLE TRANSACTIONS
6
302:RDBMS [MES-BCA] PLSQL BLOCK

Specifically, a transaction is a group of events that occurs between any of the


following events :

** Connecting to Oracle
** Disconnecting from Oracle
** Committing to the database table
** Rollback

Closing Transaction
A transaction can be closed by using either a commit or a rollback statement.
By using these statements, table data can be changed or all the changes made
to the table data undone.

ORACLE TRANSACTIONS

Using COMMIT :

A COMMIT ends the current transaction and makes permanent any


changes made during the transaction. All transactional locks acquired on
tables are released.

Syntax : SQL> COMMIT;

ORACLE TRANSACTIONS

Using ROLLBACK :

A ROLLBACK does exactly the opposite of COMMIT. It ends the transaction


but undoes any changes made during the transaction. All transactional locks
acquired on tables are released.

Syntax : SQL> ROLLBACK [WORK]


[TO [SAVEPOINT] savepoint];

ORACLE TRANSACTIONS

Creating SAVEPOINT
SAVEPOINT marks and saves the current point in the processing of a
transaction. When a SAVEPOINT is used with a ROLLBACK statement, parts
of a transaction can be undone.
An active savepoint is one that is specified since the last COMMIT or
ROLLBACK.
Syntax :
SQL> SAVEPOINT savepoint ;

ORACLE TRANSACTIONS
7
302:RDBMS [MES-BCA] PLSQL BLOCK

Use of ROLLBACK without the SAVEPOINT


A ROLLBACK operation performed without the SAVEPOINT clause amounts
to the following :

* Ends the transaction


* Undoes all the changes in the current transaction
* Erases all save points in that transaction
* Releases the transactional locks.

Use of ROLLBACK with the SAVEPOINT


A ROLLBACK operation performed with the TO SAVEPOINT clause amounts
to the following :

• A predetermined portion of the transaction is rolled back.


• Retains the save point rolled back to, but loses those created
after the named savepoint
• Releases all transactional locks that were acquired since the savepoint
was taken.

Processing a PL/SQL Blocks


A PL/SQL blocks can be run in one of two modes :

[1] Batch processing wherein records are gathered in a


table and at regular intervals manipulated.
[2] Real Time processing wherein records are
manipulated as they are created.

Batch processing is a PL/SQL block run at the SQL prompt at regular


intervals to process table data.

A technique that Oracle provides for manipulating table data in batch


processing mode is the use of Cursors.

WHAT IS A CURSOR?
The Oracle Engine uses a work are for its internal processing in order
to execute an SQL statement. This work area is private to SQL’s operations
and is called a Cursor.

TYPES OF CURSOR
Cursors are classified depending on the circumstances under which they are
opened.

[1] IMPLICIT CURSORS :

If the Oracle Engine for its its internal processing has


opened a cursor they are known as IMPLICIT
CURSORS.

[2] EXPLICIT CURSORS :

8
302:RDBMS [MES-BCA] PLSQL BLOCK

A user can also open a cursor for processing data as


required. Such user-defined cursors are known as
EXPLICIT CURSORS.

General Cursor Attributes


Both Implicit and Explicit cursors have four attributes. They are described
below :

[1] %ISOPEN : Return TRUE, if cursor is open,


FASLE otherwise.
[2] %FOUND : Return TRUE, if record was fetched
successfully, FALSE otherwise

[3] %NOTFOUND : Return TRUE, if record was not


fetched successfully, FALSE otherwise

[4] %ROWCOUNT : Returns number of records processed


from the cursor

Implicit Cursor
The Oracle engine implicitly opens a cursor on the server to process each SQL
statement.
Implicit cursor attributes can be used to access information about the status
of last insert, update, delete or single-row select statement.s. This can be done
by preceding the implicit cursor attribute with the cursor name (i. e. SQL).

The values of the cursor attributes always refer to the most recently executed
SQL statement, whatever the statement appears.

[1] %ISOPEN

The Oracle engine automatically opens and closes the SQL cursor after
executing its associated select, insert, update or delete SQL statement has
been processed in case of implicit cursors.

Syntax : SQL%ISOPEN

[2] %FOUND

Evaluates to TRUE, if an insert, update or delete affected one or more


rows, or a single-row, select returned one or more rows. Otherwise, it
evaluates to FALSE.

Syntax : SQL%FOUND

[3] %NOTFOUND

It Is the logical opposite of %FOUND. It evaluates to TRUE, if an


insert, update, or delete affected no rows, or a single-row select returns no
rows. Otherwise, it evaluates to FALSE.

Syntax : SQL%NOTFOUND
9
302:RDBMS [MES-BCA] PLSQL BLOCK

[4] %ROWCOUNT

Returns the number of rows affected by an insert, update or delete or


select into statement.

Syntax : SQL%ROWCOUNT

Explicit Cursor Attributes


When an individual records in a table have to be processed inside a
PL/SQL block a cursor is used.

This cursor will be declared and mapped to an SQL query in the


DECLARE Section of the PL/SQL block and used within the Executable
Section.

A cursor thus created and used is known as an Explicit Cursor.


[1] %ISOPEN

Evaluates to TRUE, if an explicit cursor is open; or to FALSE, if it is


closed.

Syntax : cursorname%ISOPEN
[2] %FOUND

Evaluates to TRUE, if the last fetch succeeded because a row was


available ; or to FALSE, if the last fetch failed because no more rows were
available.

Syntax : cursorname %FOUND


[3] %NOTFOUND

It Is the logical opposite of %FOUND. It evaluates to TRUE, if the last


fetch has failed because no more rows were available; or to FALSE, if the last
fetch returned a row.

Syntax : cursorname %NOTFOUND


[4] %ROWCOUNT

Returns the number of rows fetched from the active set. It is set to zero
when the cursor is opened.

Syntax : cursorname %ROWCOUNT

How to Declare CURSOR ?


Using CURSOR statement, you can declare a cursor.

Syntax :

CURSOR cursorname IS
10
302:RDBMS [MES-BCA] PLSQL BLOCK

SQL SELECT Statement;


How to Open CURSOR ?
Using OPEN statement, you can open the cursor .

Syntax :
OPEN cursorname;
How to Fetch a record from the Cursor?
Using FETCH statement, to fetch the record from the cursor.

Syntax :

FETCH cursorname INTO mvar1, var2, .. ,Varn;

How to Close the Cursor ?


Using CLOSE statement, you can close the cursor.
Syntax :

CLOSE cursorname;

Exception Section
When an SQL sentence fails the Oracle engine is the first to recognize
this as an Exception Condition. The Oracle engine immediately tries to
handle the exception condition and resolve it. This is done by raising a built-
in Exception Handler.

An Exception Handler is nothing but a code block in memory that will


attempt to resolve the current exception condition. The Oracle engine can
recognize every exception condition that occurs in memory.
As soon as the Oracle engine invokes an exception handler the exception
handler goes back to the PL/SQL block from which the exception condition
was raised.

An Exception Handler is nothing but a code block in memory that will


attempt to resolve the current exception condition. The Oracle engine can
recognize every exception condition that occurs in memory. To handle very
common and respective exception conditions the Oracle engine uses Named
Exception Handlers

How to define Exception ?


Exception
When {Exception Name} Then
{User defined action to be carried out}

DUP_VAL_ON_INDEX
LOGIN_DENIED
NO_DATA_FOUND
NOT_LOGGED_ON
PROGRAM_ERROR
TIMEOUT_ON_RESOURCE
TOO_MANY_ROWS
VALUE_ERROR
OTHERS
11
302:RDBMS [MES-BCA] PLSQL BLOCK

USER-Named Exception Handlers


Syntax :

DECLARE
exception_name EXCEPTION
BEGIN

RAISE <exception name>

EXCEPTION
WHEN <Exception name> THEN
{User defined to be taken};

END;

Explicit Cursor Management


The steps involved in using an explicit cursor and manipulating data in
its active set are :

* Declare a cursor mapped to a SQL select statement


that retrieves data for processing
* Open the cursor
* Fetch data from the cursor one row at a time into
memory variables.
* Process the data help in memory variables as required
using a loop.
* Exit from the loop after processing is complete.
* Close the cursor

12
302:RDBMS [MES-BCA] PLSQL BLOCK

Trial version of Okdo Ppt to Word Converter. http://www.okdosoft.com


Procedures and Functions
 Procedures and functions are called stored subprograms.
 They are saved in database in complied form and can be used with any
local subprogram.
 Procedures does not return any value but function always return some
value.
 Both can accept parameters.

Parameter modes
 IN - It is considered read only and cannot be changed.
 OUT - It can be read or written to, but while reading NULL.
 IN OUT – It can be read or written to.

IN OUT and OUT


 The actual parameters that corresponds to IN OUT and OUT mode
parameters must be a variable. There must be location where the
returned value can be stored.

Constraints on formal parameters


 In procedure declaration, it is illegal to constrain CHAR and
VARCHAR2 parameters with a length and a NUMBER parameters with
a precision and/or scale, as the constraints will be taken from the
actual parameters.
 Although formal parameters cannot be declared with constraints, they
can be constrained by using %TYPE. Constraint will be on the formal
parameters and not on actual parameters.

Exceptions handling inside subprogram


 If the procedure does not has exception handler for the error
generated, control immediately passes to the calling environment. In
this case the values of OUT and IN OUT formal parameters are not
returned to the actual parameters. The actual parameters will have the
same values as they would have had if the procedure had not been
called.5

Passing parameters by reference or by value


 By default, PL/SQL will pass IN parameter by reference and IN OUT
and OUT parameters by values.
 NOCOPY is compiler hint to change the default behavior of OUT and
IN OUT parameters.
 Parameter_name [mode] NOCOPY data type
 If NOCOPY is present PL/SQL will try to pass parameter by reference.

13
302:RDBMS [MES-BCA] PLSQL BLOCK

14
302:RDBMS [MES-BCA] PLSQL BLOCK

Trial version of Okdo Ppt to Word Converter. http://www.okdosoft.com


Package
 A package is a PL/SQL construct that allows related objects to be
stored together.
 A package has two separate parts: 1) Specification 2) Body
 Package Specification and package body are stored separately in data
dictionary.
 A package can only be stored it cannot be local.

Objectives:

• select * from nested;select * from nested;

Package Specification
 Contains information about the contents of the package.
 It does not contain the code for any subprograms.
 The elements within the package are the same as they would be in the
declarative section of an anonymous block.

Package body
 The package body is a separate data dictionary object from the package
header (specification).
 It cannot be successfully compiled unless the package header has
already been successfully compiled.
 The body contains the code for the subprograms declared in package
specification.

 It can also contain additional declaration which are visible to the


package body. They are not visible in the specification.
 The package body is optional. If the package header does not contain
any procedures or functions, the body does not have to be present.

Overloading subprograms
 Procedures and functions can be overload inside package.

 Overloading is very useful when the same operation can be done on


arguments of different types.

Restrictions on overloading
 Subprograms cannot be overloaded if their parameters differ only in
name or mode.
 Two functions cannot be overloaded based only on their return type.
15
302:RDBMS [MES-BCA] PLSQL BLOCK

 The parameters of overloaded functions must differ by type family –


you cannot overload on the same family. Eg: CHAR and VARCHAR2
are of the same family and cannot be overloaded.

16
302:RDBMS [MES-BCA] PLSQL BLOCK

TRIGGERTrial version of Okdo Ppt to Word Converter.


http://www.okdosoft.com
Triggers
 Trigger are named PL/SQL blocks.
 Triggers must be stored as standalone objects in the database and
cannot be local to a block or package.
 Triggers are executed implicitly whenever the triggering event
happens.
 Triggers doesn’t accept arguments.
 The act of executing a trigger is known as firing the trigger.

Triggers
 The triggering event can be a DML operation on a database table or
views.
 Triggers can be also fired on system events such as database startup
and shutdown, and certain kinds of DDL operations.
 Triggers are used for maintaining complex integrity constraints not
possible through declarative constraints enabled at table creation.

Triggers
 They are used for auditing information in a table by recording the
changes made and who made them.
 Automatically signaling other programs that action needs to take place
when changes are made to a table.
 Publishing information about various events in a publish-subscribe
environment.

Kinds of triggers
 There are three kinds of triggers:
1) DML
2) Instead of
17
302:RDBMS [MES-BCA] PLSQL BLOCK

3) System trigger

Trial version of Okdo Ppt to Word Converter. http://www.okdosoft.com

18
302:RDBMS [MES-BCA] PLSQL BLOCK

TRIGGER SYNTAX
CREATE OR REPLACE TRIGGER trigger_name
{ BEFORE | AFTER | INSTEAD OF }
triggering_event
[referencing_clause]
[ WHEN trigger_condition ]
[FOR EACH ROW ]
trigger_body;

Correlation identifiers in Row-Level Triggers


 Inside Row-Level Trigger, you can access the data in the row that is
currently being processed.
 This is accomplished through two correlation identifiers : 1) :old 2)
:new
 Correlation identifiers are special kind of PL/SQL bind variable.
 The PL/SQL compiler will treat them as records of type
triggering_table%ROWTYPE.
triggering table is the table for which the trigger is defined.

ORDER OF DML TRIGGER FIRING


 Execute the before statement level trig.
 For each row affected by the statement
 Execute the before row level trigger
 Execute the statement itself
 Execute the after row level trigger
 Execute the after statement level trigger

Correlation identifiers in Row-Level Triggers


 Each field of the triggering table can be reference as :new.fieldname
 Although they are treated as records, they are not.
 :new and :old are known as pseudorecords.

19
302:RDBMS [MES-BCA] PLSQL BLOCK

Correlation identifiers in Row-Level Triggers


Note – 1) :old is undefined for INSERT statements.
2) :new is undefined for DELETE statements.
3) :old and :new are only valid in triggers
The PL/SQL compiler will not generate an error, but the field
values of both will be null.

 Oracle8i defined an additional correlation identifier - :parent


 If the trigger is defined on a nested table, :old and :new refer to the
rows in the nested table, where as :parent refers to the current row of
the parent table.
eg: Inside trigger you can write –
select stud_seq.nextval into :new.id from dual;

About :new and :old


 :new cannot be changed in an after row-level trigger, because the
statement has already been processed.
 :new is modified only in before row-level trigger.
 :old is never modified, only read from.
 :new and :old cannot be assigned as entire records.
 They cannot be passed as argument to subprograms that take
arguments of triggering_table%ROWTYPE.

Referencing clause
 This can be used to specify a different name for :old and :new
eg: Referencing new as new_student
When Clause
 It is only valid for ROW-LEVEL triggers
 If present, the trigger body will executed only for those records that
meet the condition specified by the when clause.
 :new and :old records can be referenced inside trigger_condition, but
the : is not used there.
 The : is only valid in trigger body.
20
302:RDBMS [MES-BCA] PLSQL BLOCK

Triggering predicates
 INSERTING : INSERTING is TRUE if the triggering statement is an
INSERT: FALSE otherwise.
 UPDATING : UPDATING is TRUE if the triggering statement is an
UPDATE: FALSE otherwise.
 DELETING : DELETING is TRUE if the triggering statement is an
DELETE: FALSE otherwise.

 EXAMPLE
IF INSERTING THEN
-------------
ELSIF UPDATING THEN
-------------
ELSE
-------------
END IF;
Instead-Of triggers
 Instead-Of triggers are fired instead of DML operation on views on
which trigger is defined.
 They are used in two cases:
1) To allow a view that would otherwise not be modifiable to be
modified.
2) To modify the columns of a nested table column in a view.
EXAMPLE: DML TRIGGER
/* WRITE DATABASE TRIGGER THAT CHECK THE FOLLOWING :
1) THE QTY TO BE DISPATCHED IS AVAILABLE IN PRODUCT_MASTER
TABLE.
2) THE QTY BEING DISPATCHED,IS NOT EQUAL TO ZERO AND
3) THE QTY BEING DISPATCHED IS LESS THAN OR EQUAL TO THE
SALES_ORDER */

CREATE OR REPLACE TRIGGER QTY_CHECK


BEFORE INSERT
ON CHALLAN_DETAILS
FOR EACH ROW
DECLARE
/* DECLARING MEMORY VARIABLES THAT HOLD VALUES FROM THE
PRODUCT_MASTER,

21
302:RDBMS [MES-BCA] PLSQL BLOCK

SALES_ORDER,SALES_ORDER_DETAILS,CHALLAN_HEADER,CHALL
AN_DETAILS TABLES.*/

V_NEW_QTY NUMBER(8);
V_QTY_ON_HAND NUMBER(8);
V_QTY_ORDERED NUMBER(8);
V_QTY_DISP NUMBER(8);
V_PRODUCTNO VARCHAR2(6);
V_CHALLAN_NO VARCHAR2(6);

BEGIN

V_NEW_QTY := :NEW.QTY_DISP;

/* CHECKING THAT THE QUANTITY TO BE DISPATCHED IS GREATER


THAN ZERO AND
RAISING AN ERROR IN CASE THE QUANTITY DISPATCHED IS ZERO */

IF V_NEW_QTY <= 0 THEN


RAISE_APPLICATION_ERROR(-20001,'QUANTITY
DISPATCHED CAN NOT BE 0');
END IF;

/* STORING THE VALUES OF QUANTITY_ORDERED AND


QTY_DISPATCHED INTO
MEMORY VARIABLES */
SELECT QTYORDERED,QTYDISP INTO
V_QTY_ORDERED,V_QTY_DISP
FROM SALES_ORDER_DETAILS,CHALLAN_HEADER
WHERE PRODUCTNO = :NEW.PRODUCTNO AND
CHALLAN_HEADER.CHALLAN_NO =
:NEW.CHALLAN_NO AND
SALES_ORDER_DETAILS.ORDERNO =
CHALLAN_HEADER.ORDER_NO AND
SALES_ORDER_DETAILS.PRODUCTNO =
:NEW.PRODUCTNO;

/* CHECKING THAT V_NEW_QTY + THE TOTAL QUANTITY


DISPATCHED IS NOT GREATER THAN
THE QUANTITY ORDERED AND IF FOUND SO RAISING AN ERROR
CONDITION */

IF (V_NEW_QTY + V_QTY_DISP) > V_QTY_ORDERED THEN


RAISE_APPLICATION_ERROR(-20001,'QUANTITY
DISPATCHED IS MORE THAN WHAT WAS ORDERED BY THE CLIENT');
END IF;

/* CHECKING THAT THE V_NEW_QTY TO BE DISPATCHED IS NOT


GREATER THAN
THE QTYONHAND IN THE PRODUCT_MASTER TABLE AND IF FOUND
SO RAISING
AN ERROR CONDITION */
22
302:RDBMS [MES-BCA] PLSQL BLOCK

SELECT QTYONHAND INTO V_QTY_ON_HAND


FROM PRODUCT_MASTER
WHERE PRODUCTNO = :NEW.PRODUCTNO;
IF V_NEW_QTY > V_QTY_ON_HAND THEN
RAISE_APPLICATION_ERROR(-20001,'NOT ENOUGH
STOCK IN THE INVENTORY');
END IF;

END;

OUTPUT :-
SQL> INSERT INTO CHALLAN_DETAILS
2 VALUES('CH0018','P0345',0);
INSERT INTO CHALLAN_DETAILS
*
ERROR at line 1:
ORA-20001: QUANTITY DISPATCHED CAN NOT BE 0
ORA-06512: at "SCOTT.QTY_CHECK", line 15
ORA-04088: error during execution of trigger 'SCOTT.QTY_CHECK'

EXAMPLE: INSTEAD OF TRIIGER (ONLY ON VIEWES)


CREATE OR REPLACE TRIGGER EMPTRIG INSTEAD OF INSERT ON
EMPVIEW FOR EACH ROW
DECLARE
NAME EMPVIEW.EMP_NAME%TYPE;
SAL EMPVIEW.SAL%TYPE;
BEGIN
INSERT INTO LOGTABLE VALUES (SYSDATE, USER,
TO_CHAR(SYSDATE, 'HH:MI:SS'), 'INSERTED IN VIEW', 'BARDOLI');
END;

EXAMPLE:
/* Write a DATABASE TRIGGER that allows changes to
CLIENT_MASTER table only
during the business hours( i.e. 8 A.M. to 5 P.M.) From MONDAY to
SATURDAY.
There is no restriction on viewing data from the table.
*/
CREATE OR REPLACE TRIGGER time_check
BEFORE INSERT OR UPDATE OR DELETE ON CLIENT_MASTER
BEGIN
IF ( TO_NUMBER(TO_CHAR(SYSDATE,'HH24') ) < 8 OR
TO_NUMBER(TO_CHAR(SYSDATE,'HH24') ) >= 17 OR
TO_CHAR(SYSDATE,'DAY') = 'SUN' ) THEN
RAISE_APPLICATION_ERROR(-20000,'Changes t o EMPMAST
table
allowed only during business hours');
END IF;
END;

23

Das könnte Ihnen auch gefallen