Beruflich Dokumente
Kultur Dokumente
Every constant, variable, and parameter has a data type (or type), which specifies a
storage format, constraints, and valid range of values. PL/SQL provides a variety of
predefined data types. For instance, you can choose from integer, floating point,
2)User-Defined Subtypes
components that can be manipulated individually. A reference type holds values, called
pointers, that designate other program items. A LOB type holds values, called lob
locators, that specify the location of large objects (graphic images for example) stored
out-of-line.
Number Types
Number types let you store numeric data (integers, real numbers, and floating-
BINARY_INTEGER
You use the BINARY_INTEGER data type to store signed integers. Its magnitude
range is -2**31.. 2**31. Like PLS_INTEGER values, BINARY_INTEGER values require less
BINARY_INTEGER Subtypes
A base type is the data type from which a subtype is derived. A subtype
NATURAL
NATURALN
POSITIVE
POSITIVEN
SIGNTYPE
NUMBER
NUMBER data type to store fixed-point or floating-point numbers. Its
magnitude range is 1E-130 .. 10E125. If the value of an expression falls outside this range,
NUMBER Subtype:
You can use the following NUMBER subtypes for compatibility with ANSI/ISO and IBM
DEC
DECIMAL
DOUBLE PRECISION
FLOAT
INTEGER
INT
NUMERIC
REAL
Use the subtypes DEC, DECIMAL, and NUMERIC to declare fixed-point numbers with a
Character types let you store alphanumeric data, represent words and text, and
CHAR
You use the CHAR data type to store fixed-length character data. How the data is
represented internally depends on the database character set. The CHAR data type takes
an optional parameter that lets you specify a maximum size up to 32767 bytes.
CHAR Subtype
The CHAR subtype CHARACTER has the same range of values as its base type.
That is, CHARACTER is just another name for CHAR. You can use this subtype for
compatibility with ANSI/ISO and IBM types or when you want an identifier more
LONG data type is like the VARCHAR2 data type, except that the maximum size of a
You use the LONG RAW data type to store binary data or byte strings. LONG
RAW data is like LONG data, except that LONG RAW data is not interpreted by PL/SQL.
RAW
You use the RAW data type to store binary data or byte strings. For example, a
RAW data type takes a required parameter that lets you specify a maximum size up to
32767 bytes.
Internally, every database table has a ROWID pseudo column, which stores
binary values called rowdies. Each rowed represents the storage address of a row. A
physical rowed identifies a row in an ordinary table. A logical rowed identifies a row in an
index-organized table. The ROWID data type can store only physical rowdies. However,
the UROWID (universal rowed) data type can store physical, logical, or foreign (non-
Oracle) rowdies.
VARCHAR2
You use the VARCHAR2 data type to store variable-length character data. How
the data is represented internally depends on the database character set. The VARCHAR2
data type takes a required parameter that specifies a maximum size up to 32767 bytes.
The widely used one-byte ASCII and EBCDIC character sets are adequate to represent
the Roman alphabet, but some Asian languages, such as Japanese, contain thousands of
characters. These languages require two or three bytes to represent each character
NCHAR
You use the NCHAR data type to store fixed-length (blank-padded if necessary) national
character data. How the data is represented internally depends on the national character
set specified when the database was created, which might use a variable-width encoding
NVARCHAR2
You use the NVARCHAR2 data type to store variable-length Unicode character
data. How the data is represented internally depends on the national character set
specified when the database was created, which might use a variable-width encoding
LOB Types:
The LOB (large object) data types BFILE, BLOB, CLOB, and NCLOB let you store blocks of
unstructured data (such as text, graphic images, video clips, and sound waveforms) up to
four gigabytes in size. And, they allow efficient, random, piece-wise access to the data.
BFILE
You use the BFILE data type to store large binary objects in operating
system files outside the database. The locator includes a directory alias, which specifies a
BFILEs are read-only, so you cannot modify them. The size of a BFILE is
system dependent but cannot exceed four gigabytes (2**32 - 1 bytes). The maximum
BLOB
You use the BLOB data type to store large binary objects in the database, in-line or out-
of-line. Every BLOB variable stores a locator, which points to a large binary object. The
BLOBs participate fully in transactions, are recoverable, and can be replicated. Changes
made by package DBMS_LOB can be committed or rolled back. BLOB locators can span
CLOB
You use the CLOB data type to store large blocks of character data in the database, in-
line or out-of-line. Both fixed-width and variable-width character sets are supported.
Every CLOB variable stores a locator, which points to a large block of character data. The
NCLOB
You use the NCLOB data type to store large blocks of NCHAR data in the database, in-
line or out-of-line. Both fixed-width and variable-width character sets are supported.
Every NCLOB variable stores a locator, which points to a large block of NCHAR data. The
Boolean Type
BOOLEAN
You use the BOOLEAN data type to store the logical values TRUE, FALSE, and NULL
(which stands for a missing, unknown, or inapplicable value). Only logic operations are
can be assigned to a BOOLEAN variable. You cannot insert the values TRUE and FALSE
The data types in this section let you store and manipulate dates, times, and intervals
(periods of time). A variable that has a date/time data type holds values called datetimes;
a variable that has an interval data type holds values called intervals. A datetime or
interval consists of fields, which determine its value. The following list shows the valid
MONTH 01 to 12 0 to 11
HOUR 00 to 23 0 to 23
MINUTE 00 to 59 0 to 59
interval fractional
seconds
DATE
You use the DATE data type to store fixed-length datetimes, which include the time of
day in seconds since midnight. The date portion defaults to the first day of the current
month; the time portion defaults to midnight. The date function SYSDATE returns the
TIMESTAMP
The data type TIMESTAMP, which extends the data type DATE, stores the year, month,
TIMESTAMP [(precision)]
The data type TIMESTAMP WITH TIME ZONE, which extends the data type TIMESTAMP,
hours and minutes) between local time and Coordinated Universal Time (UTC)--formerly
You use the data type INTERVAL YEAR TO MONTH to store and manipulate intervals of
You use the data type INTERVAL DAY TO SECOND to store and manipulate intervals of
SECOND [(fractional_seconds_precision)]
User-Defined Subtypes
Each PL/SQL base type specifies a set of values and a set of operations applicable to
items of that type. Subtypes specify the same set of operations as their base type but
only a subset of its values. Thus, a subtype does not introduce a new type; it merely
For example, PL/SQL predefines the subtypes CHARACTER and INTEGER as follows:
The first form does not specify the column names where the data will be inserted, only
their values:
VALUES (value1,value2,value3,...);
The second form specifies both the column names and the values to be inserted:
VALUES (value1,value2,value3,);
Table creation:
Table created.
ENO NUMBER(3)
ENAME CHAR(10)
DEPART CHAR(10)
ADDRESS CHAR(20)
SAL NUMBER(6)
MA NUMBER(4)
HRA NUMBER(4)
TOT NUMBER(7)
1 row created.
00,3000);
1 row created.
2000,4000);
1 row created.
Output:
5 rows selected
SQL> declare
2 c char(10);
4 s e%rowtype;
5 begin
6 open e;
7 loop
8 fetch e into s;
10 if s.workingday>200 then
11 c:='good';
12 else
13 c:='satisfied';
14 end if;
17 end loop;
18 close e;
19 end;
20 /
PL/SQL procedure successfully completed.
Output:
---
5 rows selected
EX.N0:2 FUNCTIONS
Function:
Functions can return a value to the caller, and functions can be directly referenced in
queries.
This value is returned through the use of the return keyword within the function.
Calling a Function:
While creating a function, you give a definition of what the function has to do. T o use a
function, you will have to call that function to perform the defined task. When a program
called function .A called function performs defined task and when its return statement is
executed or when it last end statement is reached, it returns program control back to the
main program .To call a function you simply need to pass the required parameters along
with function name and if function returns a value then you can store returned value
Syntax:
{IS | AS}
BEGIN
END [function_name];
Both the header and the body of the function are created by this command.
The return keyword specifies the data type of the function’s return value. This can be any
valid PL/SQL data type (see Chapter 27). Every function must have a return clause, since
Purpose:
T he optional parameter list contains name, mode and types of the parameters. IN
represents that value will be passed from outside and OUT represents that this
of the procedure.
RETURN clause specifies that data type you are going to return from the function.
The AS keyword is used instead of the IS keyword for creating a standalone function.
Table created.
ACCNO NUMBER(3)
NAME CHAR(20)
GENDER CHAR(2)
MNO NUMBER(10)
ADDR CHAR(15)
DEP NUMBER(8)
BAL NUMBER(8)
RWITH NUMBER(8)
PROGRAM :-
3 begin
4 s:=d1-w1;
5 return s;
6 end;
7 /
Function created.
SQL> declare
2 acno number(3):=&number;
3 n char(20):=&char;
4 g char(2):=&char;
5 m number(10):=&number;
6 add char(15):=&char;
7 d number(8):=&number;
8 w number(8):=&number;
9 s number;
10 begin
11 s:=ba15(d,w);
14 /
old 3: n char(20):=&char;
new 3: n char(20):='aruna';
old 4: g char(2):=&char;
new 4: g char(2):='f';
old 5: m number(10):=&number;
new 5: m number(10):=200123;
old 7: d number(8):=&number;
new 7: d number(8):=15000;
old 8: w number(8):=&number;
new 8: w number(8):=1000;
Output:
-----------
1000
PL/SQL procedure successfully completed.
SQL> /
old 3: n char(20):=&char;
new 3: n char(20):='balu';
old 4: g char(2):=&char;
new 4: g char(2):='m';
old 5: m number(10):=&number;
new 5: m number(10):=678456;
old 7: d number(8):=&number;
new 7: d number(8):=12344;
old 8: w number(8):=&number;
new 8: w number(8):=6788;
----- ---------
1000
2 divya f 213456 32 kr street 234567 222222
12345
PL/SQL uses implicit and explicit cursors. PL/SQL declare a implicit cursor for each
registration.
If you want precise control over the interogations, you can declare a explicit cursor;
You can define a explicit cursor for the interogations which returns more than one
registration.
1. IMPLICIT CURSOR
The implicit cursors are managed automatically by the PL/SQL, so you don't have
written any code for handling them. Can you account for the execution of a implicit
When you need precise control over the outcome of the interrogations, must be
Three commands are used to control a cursor: OPEN, FETCH and CLOSE. First time the
cursor must must be opened through the OPEN command. Then you can run FETCH
repeated until all records have been read or you can use BULK COLLECT clause to read
all recording only once. For disable/close a cursor uses the CLOSE command.
The attributes of the implicit cursors can return information about DML and DDL
ROLLBACK.
ROWCOUNT.
% FOUND
Until SQL data manipulation is executed, the attribute % FOUND is NULL. So, %
FOUND is TRUE if the command type of INSERT, UPDATE, or DELETE affects one or more
records from the database or SELECT INTO returns one or more recordings.
% ISOPEN
% NOTFOUND
not affect any of the database or SELECT INTO does not return any registration.
Otherwise it is FALSE.
% ROWCOUNT
that INSERT, UPDATE, or DELETE, or the number of records affected by the SELECT INTO.
% ROWCOUNT is 0 if the command INSERT, UPDATE, or DELETE does not affect any
CURSOR cursor_name
IS
SELECT_statement;
IS
SELECT_statement;
CURSOR cursor_name
RETURN field%ROWTYPE
IS
SELECT_statement;
SQL> create table clg18(sno number(3), sname char(15), inter number(3), exter
Table created.
1 row created.
SQL> declare
2 total number;
3 total1 number;
5 r clg18%rowtype;
6 begin
7 dbms_output.put_line('output');
8 dbms_output.put_line('student details');
9 dbms_output.put_line('NO'||' '||'NAME'||' '||'INTER'||' '||'EXTER'||' '||'TOT');
10 open s;
11 loop
12 fetch s into r;
14 total:=r.inter+r.exter;
15 total1:=r.tot;
16 total1:=total;
18 end loop;
19 close s;
20 end;
21 /
1 darsini 35 50 85
2 ram 30 40 70
3 ram 32 45 77
4 sugan 40 40 80
5 sathya 37 46 83
EX.NO:4 LOOPS
You can use loops to process multiple records within a single PL/SQL block.
PL/SQL supports.
Simple loops A loop that keeps repeating until an exit or exit when statement is
1) Simple Loop
An EXIT condition must be specified in the loop, otherwise the loop will get into an
infinite number of iterations. When the EXIT condition is satisfied the process exits from
the loop.
Syntax:
LOOP
statements;
EXIT;
END LOOP;
These are the important steps to be followed while using Simple Loop.
without
Example:
DECLARE
VAR1 NUMBER;
VAR2 NUMBER;
BEGIN
VAR1:=100;
VAR2:=1;
LOOP
DBMS_OUTPUT.PUT_LINE (VAR1*VAR2);
IF (VAR2=10) THEN
EXIT;
END IF;
VAR2:=VAR2+1;
END LOOP;
END;
Sample Output:
100
..
1000
2) FOR Loop:
of times. Iteration occurs between the start and end integer values given. The counter
is always incremented by 1. The loop exits when the counter reachs the value of the end
integer.
Syntax:
LOOP statements;
END LOOP;
val1 - Start integer value
Example:
DECLARE
VAR1 NUMBER;
BEGIN
VAR1:=10;
LOOP
DBMS_OUTPUT.PUT_LINE (VAR1*VAR2);
END LOOP;
END;
Sample Output:
10
..
100.
3) While Loop
condition is true. The condition is evaluated at the beginning of each iteration. The
Syntax:
WHILE <condition>
LOOP statements;
END LOOP;
Example:
DECLARE
VAR1 NUMBER;
VAR2 NUMBER;
BEGIN
VAR1:=200;
VAR2:=1;
WHILE (VAR2<=10)
LOOP
DBMS_OUTPUT.PUT_LINE (VAR1*VAR2);
VAR2:=VAR2+1;
END LOOP;
END;
Sample Output:
200
400
600
700
1000
1200
1400
1600
1800
2000.
Cursor:
Cursor is a temporary memory area (context area) where Oracle executes SQL
statements. Oracle associates every SELECT statement with a cursor to hold the query
Types of Cursor
Implicit cursor
statement executed. In this process, the user is unaware of implicit cursor. Oracle
Explicit cursor
Explicit cursor is used for the query that returns more than one row of data. These
cursors are explicitly declared in the DECLARE section of the PL/SQL block. This
declaration allows to sequentially process each row of data as the cursor returns it. In
explicit cursor DECLARE, OPEN, FETCH, and CLOSE operations are done by the
programmer.
Open : The cursor is opened which is declared, and the temporary memory area is
allotted.
Fetch : Cursor which is declared and opened can now retrieve rows from data.
Close : The CLOSE statement disables the cursor, and releases the temporary memory
area.
Cursor Attributes
These attributes return useful information about the execution of a SQL statement.
returns TRUE if last fetch did not return a row, Else FALSE. If last fetch returns row.
TRUE if the cursor is open, fetches the row till the last fetches FALSE if last
EXAMPLE
DECLARE
S T%ROWTYPE
BEGIN
OPEN T
LOOP
FETCH T INTO ST
END LOOP;
CLOSE T;
END;
OUTPUT
1 RAJA 89 98 187
2 RAJU 94 96 191
EX.NO:5 PROCEDURE
Language (SQL) statements with an assigned name that's stored in the database in
compiled form so that it can be shared by a number of programs. The use of stored
SYNTAX :
[ (parameter [,parameter]) ]
IS
[declaration_section]
BEGIN
executable_section
[EXCEPTION
exception_section]
END [procedure_name];
When you create a procedure or function, you may define parameters. There are three
2. OUT - The parameter can not be referenced by the procedure or function, but the
AIM :
CREATING A PROCEDURE :
as
nunit number;
amt number;
begin
nunit :=cur-pre1;
if (nunit <300)
then
if(nunit >=300)
then
end if;
end prnam;
Procedure created.
CALLING PROCEDURE :
declare
cv c%rowtype;
begin
open c;
loop
prnam (cv.cur,cv.pre);
end loop;
close c;
end;
CREATING A PROCEDURE :
as
begin
end;
Procedure created.
OUTPUT :
A trigger is a pl/sql block structure which is fired when a DML statements like Insert,
a specified event occurs .A trigger is a special kind of a store procedure. You can’t
explicitly invoke striggers Trigger is stored into database and invoked repeatedly, when
Component of Trigger
Triggering SQL statement : SQL DML (INSERT, UPDATE and DELETE) statement that
Trigger Action : When the triggering SQL statement is execute, trigger automatically call
Trigger Restriction : We can specify the condition inside trigger to when trigger is fire.
Type of Triggers
1. BEFORE Trigger :
BEFORE trigger execute before the triggering DML statement (INSERT, UPDATE,
DELETE) execute. Triggering SQL statement is may or may not execute, depending on
2. AFTER Trigger :
AFTER trigger execute after the triggering DML statement (INSERT, UPDATE,
3. ROW Trigger :
ROW trigger fire for each and every record which are performing INSERT, UPDATE,
DELETE from the database table. If row deleting is define as trigger event, when
trigger file, deletes the five rows each times from the table.
4. Statement Trigger :
Statement trigger fire only once for each statement. If row deleting is define as
trigger event, when trigger file, deletes the five rows at once from the table.
5. Combination Trigger :
Combination trigger are combination of two trigger type,
1. Before Statement Trigger : Trigger fire only once for each statement before
2. Before Row Trigger : Trigger fire for each and every record before the
3. After Statement Trigger : Trigger fire only once for each statement after the
4. After Row Trigger : Trigger fire for each and every record after the
BEFORE |AFTER
[INSERT,UPDATE,DELETE[COLUMN NAME..]
ON table_name
[declaration_section
variable declarations;
constant declarations;
BEGIN
[executable_section
EXCEPTION
[exception_section
END;
AFTER TRIGGER:
This trigger fires after SQL Server completes the execution of the action successfully that
fired it.
SYNTAX:
DECLARE
-- variable declarations
BEGIN
-- trigger code
EXCEPTION
WHEN ...
-- exception handling
END;
EXAMPLE:
BEGIN
trigger_test_api.g_tab.extend;
CASE
END CASE;
END trigger_test_ar_trg;
Trigger created.
OUTPUT :
SQL>SET SERVEROUTPUT ON
ERROR at line 1:
BEFORE TRIGGER:
BEFORE trigger execute before the triggering DML statement (INSERT, UPDATE,
DELETE) execute. Triggering SQL statement is may or may not execute, depending on
SYNTAX:
DECLARE
variable declarations
BEGIN
trigger code
EXCEPTION
WHEN ...
exception handling
END;.
EXAMPLE:
declare
sal_diff number;
begin
end;
Trigger created.
OUTPUT:
1 row created.
1 row updated.
salary
12000
2 sangee 20 42,south street
12,500
11,500
9,500