Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Download
Standard view
Full view
of .
Look up keyword
Like this
1Activity
0 of .
Results for:
No results containing your search query
P. 1
PL_SQL

PL_SQL

Ratings: (0)|Views: 5|Likes:
Published by Ipsit Samal

More info:

Published by: Ipsit Samal on Jun 25, 2012
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

03/03/2015

pdf

text

original

 
Resources
Database Systems: The Complete Book by Hector Garcia, Jeff Ullman, and Jennifer Widom.A First Course in Database Systemsby Jeff Ullman and Jennifer Widom.GradianceSQL Tutorial. 
Using Oracle PL/SQL
Basic Structure of PL/SQL
PL/SQL stands for Procedural Language/SQL. PL/SQL extends SQL by adding constructs foundin procedural languages, resulting in a structural language that is more powerful than SQL. The basic unit in PL/SQL is a
block 
. All PL/SQL programs are made up of blocks, which can be nestedwithin each other. Typically, each block performs a logical action in he program. A block has thefollowing structure:
DECLARE/* Declarative section: variables, types, and local subprograms. */BEGIN/* Executable section: procedural and SQL statements go here. *//* This is the only section of the block that is required. */EXCEPTION
 
/* Exception handling section: error handling statements go here. */END;
Only the executable section is required. The other sections are optional. The only SQL statementsallowed in a PL/SQL program are
SELECT
,
INSERT
,
UPDATE
,
DELETE
and several other datamanipulation statements plus some transaction control. However, the
SELECT
statement has aspecial form in which a single tuple is placed in variables; more on this later. Data definitionstatements like
CREATE
,
DROP
, or 
ALTER
are not allowed. The executable section also containsconstructs such as assignments, branches, loops, procedure calls, andtriggers, which are alldescribed below (excepttriggers). PL/SQL is not case sensitive. C style comments (
/* ... */
)may be used.To execute a PL/SQL program, we must follow the program text itself byA line with a single dot ("
.
"), and thenA line with
run;
 As with Oracle SQL programs, we can invoke a PL/SQL program either by typing it in
sqlplus
or  by putting the code in a file and invoking the file in the various ways we learned inGetting StartedWith Oracle.
Variables and Types
Information is transmitted between a PL/SQL program and the database through
variables
. Everyvariable has a specific type associated with it. That type can beOne of the types used by SQL for database columnsA generic type used in PL/SQL such as
NUMBER
 Declared to be the same as the type of some database columnThe most commonly used generic type is
NUMBER
. Variables of type
NUMBER
can hold either aninteger or a real number. The most commonly used character string type is
VARCHAR(
n
)
, where
n
isthe maximum length of the string in bytes. This length is required, and there is no default. For example, we might declare:
DECLAREprice NUMBER;myBeer VARCHAR(20);
 Note that PL/SQL allows
BOOLEAN
variables, even though Oracle does not support
BOOLEAN
as atype for database columns.Types in PL/SQL can be tricky. In many cases, a PL/SQL variable will be used to manipulate datastored in a existing relation. In this case, it is essential that the variable have the same type as therelation column. If there is any type mismatch, variable assignments and comparisons may not
 
work the way you expect. To be safe, instead of hard coding the type of a variable, you should usethe
%TYPE
operator. For example:
DECLAREmyBeer Beers.name%TYPE;
gives PL/SQL variable
myBeer
whatever type was declared for the
name
column in relation
Beers
.A variable may also have a type that is a record with several fields. The simplest way to declaresuch a variable is to use
%ROWTYPE
on a relation name. The result is a record type in which thefields have the same names and types as the attributes of the relation. For instance:
DECLAREbeerTuple Beers%ROWTYPE;
makes variable
beerTuple
be a record with fields
name
and
manufacture
, assuming that therelation has the schema
Beers(name, manufacture)
.The initial value of any variable, regardless of its type, is
NULL
. We can assign values to variables,using the "
:=
" operator. The assignment can occur either immediately after the type of the variableis declared, or anywhere in the executable portion of the program. An example:
DECLAREa NUMBER := 3;BEGINa := a + 1;END;.run;
This program has no effect when run, because there are no changes to the database.
Simple Programs in PL/SQL
The simplest form of program has some declarations followed by an executable section consistingof one or more of the SQL statements with which we are familiar. The major nuance is that theform of the
SELECT
statement is different from its SQL form. After the
SELECT
clause, we
must 
have an
INTO
clause listing variables, one for each attribute in the
SELECT
clause, into which thecomponents of the retrieved tuple must be placed. Notice we said "tuple" rather than "tuples", since the
SELECT
statement in PL/SQL only works if the result of the query contains a single tuple. The situation is essentially the same as that of the"single-row select" discussed in Section 7.1.5 of the text, in connection with embedded SQL. If thequery returns more than one tuple, you need to use a
cursor 
, as described in the next section. Hereis an example:
CREATE TABLE T1(

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->