Você está na página 1de 7

PL/SQL

Procedural SQL

Introduction
PL/SQL supports variables, conditions, loops and exceptions. Arrays are also supported, though in a somewhat unusual way, involving the use of PL/SQL collections. PL/SQL collections are a slightly advanced topic. mplementations from version ! of "racle #atabase onwards have included features associated with ob$ect%orientation. PL/SQL program units &essentially code containers' can be compiled into the "racle database. Programmers can thus embed PL/SQL units of functionality into the database directly. (hey also can write scripts containing PL/SQL program units that can be read into the database using the "racle SQL)Plus tool. "nce the program units have been stored into the database, they become available for execution at a later time. *hile programmers can readily embed #ata +anipulation Language &#+L' statements directly into their PL/SQL code using straight forward SQL statements, #ata #efinition Language &##L' re,uires more complex -#ynamic SQL- statements to be written in the PL/SQL code. .owever, #+L statements underpin the ma$ority of PL/SQL code in typical software applications. n the case of PL/SQL dynamic SQL, early versions of the "racle #atabase re,uired the use of a complicated "racle DBMS_SQL pac/age library. +ore recent versions have however introduced a simpler -0ative #ynamic SQL-, along with an associated EXECUTE IMMEDIATE syntax. "racle 1orporation customarily extends pac/age functionality with each successive release of the "racle #atabase.

Functions
2unctions in PL/SQL are a collection of SQL and PL/SQL statements that perform a tas/ and should return a value to the calling environment. 3ser defined functions are used to supplement the many hundreds of functions built in by "racle. (here are two different types of functions in PL/SQL. (he traditional function is written in the form4
CREATE OR REPLACE FUNCTION <function_name !"in#ut$out#ut %a&ia'(e )ec(a&ation*+, RETURN &etu&n_t-#e !AUT.ID <CURRENT USER / DEFINER , <IS/AS !)ec(a&ation '(oc0, BE1IN <PL$SQL '(oc0 2IT. RETURN *tatement RETURN <&etu&n_%a(ue 3 !EXCEPTION EXCEPTION '(oc0, RETURN <&etu&n_%a(ue 3 END3

Pipelined (able 2unctions are written in the form4


CREATE OR REPLACE FUNCTION <function_name !"in#ut$out#ut %a&ia'(e )ec(a&ation*+, RETURN &etu&n_t-#e !AUT.ID <CURRENT USER / DEFINER , !<A11RE1ATE / PIPELINED , <IS/USIN1 !)ec(a&ation '(oc0, BE1IN <PL$SQL '(oc0 2IT. RETURN *tatement PIPE RO2 <&etu&n t-#e 3 RETURN3 !EXCEPTION EXCEPTION '(oc0, PIPE RO2 <&etu&n t-#e 3 RETURN3 END3

(here are three types of parameter4 0, "3( and 0 "3(. An 0 parameter is used as input only. An 0 parameter is passed by copy and thus cannot be changed by the called program. An "3( parameter is initially 03LL. (he program assigns the parameter a value and that value is returned to the calling program. An 0 "3( parameter may or may not have an initial value. (hat initial value may or may not be modified by the called program. Any changes made to the parameter are returned to the calling program by default by copying but, with the 0"1"P5 hint may be passed by reference.

Procedures
Procedures are similar to 2unctions, in that they can be executed to perform wor/. (he primary difference is that procedures cannot be used in a SQL statement and although they can have multiple out parameters they do not -67(360- a value. Procedures are traditionally the wor/horse of the coding world and functions are traditionally the smaller, more specific pieces of code. PL/SQL maintains many of the distinctions between functions and procedures found in many general%purpose programming languages, but in addition, functions can be called from SQL, while procedures cannot.

Packages
Pac/ages are groups of conceptually lin/ed 2unctions, Procedures,8ariable,PL/SQL table and record (5P7 statements,1onstants 9 1ursors etc. (he use of pac/ages promotes re% use of code. Pac/ages usually have two parts, a specification and a body, although sometimes the body is unnecessary. (he specification &spec for short' is the interface to your applications: it declares the types, variables, constants, exceptions, cursors, and subprograms available for use. (he body fully defines cursors and subprograms, and so implements the spec. (hey are stored permanently in 3S76;S"3617 system table. (wo advantages of pac/ages include4 <. +odular approach, 7ncapsulation/hiding of business logic, security, performance improvement, re%usability.(hey support ""PS features li/e function over loading,encapsulation. =. 3sing pac/age variables we can declare session level &scoped' variables, since variables declared in the pac/age specification have session scope.

Variables
Numeric variables
%a&ia'(e_name NUMBER"P!4S,+ 56 7ALUE3

(o define a numeric variable, the programmer appends the variable type NUMBER to the name definition. (o specify the &optional' precision&P' and the &optional' scale &S', one can further append these in round brac/ets, separated by a comma. &-Precision- in this context refers to the number of digits which the variable can hold, -scale- refers to the number of digits which can follow the decimal point.' A selection of other datatypes for numeric variables would include4 binary;float, binary;double, dec, decimal, double precision, float, integer, int, numeric, real, smallint, binary;integer

Character variables
%a&ia'(e_name 7ARC.AR8"L+ 56 9Te:t93

(o define a character variable, the programmer normally appends the variable type 8A61.A6= to the name definition. (here follows in brac/ets the maximum number of characters which the variable can store. "ther datatypes for character variables include4 varchar, char, long, raw, long raw, nchar, nchar=, clob, blob, bfile

Date variables
%a&ia'(e_name DATE 56 9;<=>an=8;;?93

"racle provides a number of data types that can store dates &#A(7, #A(7( +7, ( +7S(A+P etc.', however #A(7 is most commonly used. Programmers define date variables by appending the datatype code -#A(7- to a variable name. (he TO_DATE function can be used to convert strings to date values. (he function converts the first ,uoted string into a date, using as a definition the second ,uoted string, for example4
TO_DATE"9@<=<8=8;;A949))=mm=----9+

or
TO_DATE "9@<=Dec=8;;A949))=mon=----94 9NLS_DATE_LAN1UA1E 6 Ame&ican9+

(o convert the dates to strings one uses the function TO_C.AR ")ate_*t&inB4 fo&mat_*t&inB+. PL/SQL also supports the use of A0S date and interval literals.>=? (he following clause gives an <!%month range4
2.ERE )ateFie() BET2EEN DATE 98;;A=<8=@<9 = INTER7AL 9<=C9 DEAR TO MONT. AND DATE 98;;A=<8=@<9

Datatypes for specific columns


8ariable;name (able;name.1olumn;name@type: (his syntax defines a variable of the type of the referenced column on the referenced tables. Programmers specify user%defined datatypes with the syntax4
t-#e )ata_t-#e i* &eco&) "fie()_< t-#e_< 56:-E4 fie()_8 t-#e_8 56:-E4 FFF4 fie()_n t-#e_n 56:-E+3

2or example4
DECLARE TDPE t_a))&e** IS RECORD " name a))&e**FnameGTDPE4 *t&eet a))&e**F*t&eetGTDPE4 *t&eet_num'e& a))&e**F*t&eet_num'e&GTDPE4 #o*tco)e a))&e**F#o*tco)eGTDPE+3 %_a))&e** t_a))&e**3 BE1IN SELECT name4 *t&eet4 *t&eet_num'e&4 #o*tco)e INTO %_a))&e** FROM a))&e** 2.ERE RO2NUM 6 <3 END3

(his sample program defines its own datatype, called t_address, which contains the fields name, street, street_number and postcode. So according to the example, we are able to copy the data from the database to the fields in the program. 3sing this datatype the programmer has defined a variable called v_address and loaded it with data from the A##67SS table. Programmers can address individual attributes in such a structure by means of the dot% notation, thus4 "v_address.street := 'High Street';"

Conditional Statements
(he following code segment shows the 2%(.70%7LS 2 construct. (he 7LS 2 and 7LS7 parts are optional so it is possible to create simpler 2%(.70 or, 2%(.70%7LS7 constructs.
IF : 6 < T.EN *eHuence_of_*tatement*_<83 ELSIF : 6 8 T.EN *eHuence_of_*tatement*_83 ELSIF : 6 @ T.EN *eHuence_of_*tatement*_@3 ELSIF : 6 A T.EN *eHuence_of_*tatement*_A3 ELSIF : 6 ? T.EN *eHuence_of_*tatement*_?3 ELSE *eHuence_of_*tatement*_N3 END IF3

(he 1AS7 statement simplifies some large 2%(.70%7LS7 structures.


CASE 2.EN : 6 < T.EN *eHuence_of_*tatement*_<3 2.EN : 6 8 T.EN *eHuence_of_*tatement*_83 2.EN : 6 @ T.EN *eHuence_of_*tatement*_@3 2.EN : 6 A T.EN *eHuence_of_*tatement*_A3 2.EN : 6 ? T.EN *eHuence_of_*tatement*_?3 ELSE *eHuence_of_*tatement*_N3 END CASE3

1AS7 statement can be used with predefined selector4


CASE : 2.EN < T.EN *eHuence_of_*tatement*_<3 2.EN 8 T.EN *eHuence_of_*tatement*_83 2.EN @ T.EN *eHuence_of_*tatement*_@3 2.EN A T.EN *eHuence_of_*tatement*_A3 2.EN ? T.EN *eHuence_of_*tatement*_?3 ELSE *eHuence_of_*tatement*_N3 END CASE3

Array andlin!
PL/SQL refers to arrays as -collections-. (he language offers three types of collections4 <. Associative arrays & ndex%by tables' =. 0ested tables A. 8arrays &variable%siBe arrays' Programmers must specify an upper limit for varrays, but need not for index%by tables or for nested tables. (he language includes several collection methods used to manipulate collection elements4 for example 2 6S(, LAS(, 07C(, P6 "6, 7C(70#, (6 +, #7L7(7, etc. ndex%by tables can be used to simulate associative arrays, as in this example of a memo function for Ac/ermannDs function in PL/SQL.

Você também pode gostar