Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

PL/SQL

From Wikipedia, the free encyclopedia
Procedural extension for SQL and the Oracle relational database
This article has multiple issues. Please helpimprove it or discuss these issues on thetalk page.(Learn how and when to remove these messages)
This article includes a list ofgeneral references, butit lacks sufficient correspondinginline citations. Please help toimprove this article byintroducing more precise citations.(February 2008) (Learn how and when to remove this message)
icon
This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed.
Find sources: "PL/SQL" – news ·newspapers ·books ·scholar ·JSTOR
(January 2008) (Learn how and when to remove this message)
(Learn how and when to remove this message)

PL/SQL (Procedural Language for SQL) isOracle Corporation'sproceduralextension forSQL and theOracle relational database. PL/SQL is available in Oracle Database (since version 6 - stored PL/SQL procedures/functions/packages/triggers since version 7),TimesTen in-memory database (since version 11.2.1), andIBM Db2 (since version 9.7).[1] Oracle Corporation usually extends PL/SQL functionality with each successive release of the Oracle Database.

PL/SQL includes procedural language elements such asconditions andloops, and can handleexceptions (run-time errors). It allows the declaration of constants andvariables, procedures, functions, packages, types and variables of those types, and triggers.Arrays are supported involving the use of PL/SQL collections. Implementations from version 8 of Oracle Database onwards have included features associated with object-orientation. One can create PL/SQL units such as procedures, functions, packages, types, and triggers, which are stored in the database for reuse by applications that use any of the Oracle Database programmatic interfaces.

The first public version of the PL/SQL definition[2] was in 1995. It implements the ISOSQL/PSM standard.[3]

PL/SQL program unit

[edit]

The main feature of SQL (non-procedural) is also its drawback: control statements (decision-making oriterative control) cannot be used if only SQL is to be used. PL/SQL provides the functionality of other procedural programming languages, such as decision making, iteration etc. A PL/SQL program unit is one of the following: PL/SQL anonymous block,procedure,function,package specification, package body, trigger, type specification, type body, library. Program units are the PL/SQL source code that is developed, compiled, and ultimately executed on the database.[4]

PL/SQL anonymous block

[edit]

The basic unit of a PL/SQL source program is the block, which groups together related declarations and statements. A PL/SQL block is defined by the keywords DECLARE, BEGIN, EXCEPTION, and END. These keywords divide the block into a declarative part, an executable part, and an exception-handling part. The declaration section is optional and may be used to define and initialize constants and variables. If a variable is not initialized then it defaults toNULL value. The optional exception-handling part is used to handle run-time errors. Only the executable part is required. A block can have a label.[5]

For example:

<<label>>-- this is optionalDECLARE-- this section is optionalnumber1NUMBER(2);number2number1%TYPE:=17;-- value defaulttext1VARCHAR2(12):='      Hello world       ';text2DATE:=SYSDATE;-- current date and timeBEGIN-- this section is mandatory, must contain at least one executable statementSELECTstreet_numberINTOnumber1FROMaddressWHEREname='INU';EXCEPTION-- this section is optionalWHENOTHERSTHENDBMS_OUTPUT.PUT_LINE('Error Code is '||TO_CHAR(sqlcode));DBMS_OUTPUT.PUT_LINE('Error Message is '||sqlerrm);END;

The symbol:= functions as anassignment operator to store a value in a variable.

Blocks can be nested – i.e., because a block is an executable statement, it can appear in another block wherever an executable statement is allowed. A block can be submitted to an interactive tool (such as SQL*Plus) or embedded within an Oracle Precompiler orOCI program. The interactive tool or program runs the block once. The block is not stored in the database, and for that reason, it is called an anonymous block (even if it has a label).

Function

[edit]

The purpose of a PL/SQL function is generally used to compute and return a single value. This returned value may be a single scalar value (such as a number, date or character string) or a single collection (such as a nested table or array).User-defined functions supplement the built-in functions provided by Oracle Corporation.[6]

The PL/SQL function has the form:

CREATEORREPLACEFUNCTION<function_name>[(input/outputvariabledeclarations)]RETURNreturn_type[AUTHID<CURRENT_USER|DEFINER>]<IS|AS>-- heading partamountnumber;-- declaration blockBEGIN-- executable part<PL/SQLblockwithreturnstatement>RETURN<return_value>;[Exceptionnone]RETURN<return_value>;END;

Pipe-lined table functions return collections[7]and take the form:

CREATEORREPLACEFUNCTION<function_name>[(input/outputvariabledeclarations)]RETURNreturn_type[AUTHID<CURRENT_USER|DEFINER>][<AGGREGATE|PIPELINED>]<IS|USING>[declarationblock]BEGIN<PL/SQLblockwithreturnstatement>PIPEROW<returntype>;RETURN;[Exceptionexceptionblock]PIPEROW<returntype>;RETURN;END;

A function should only use the default IN type of parameter. The only out value from the function should be the value it returns.

Procedure

[edit]

Procedures resemble functions in that they are named program units that can be invoked repeatedly. The primary difference is thatfunctions can be used in a SQL statement whereas procedures cannot. Another difference is that the procedure can return multiple values whereas a function should only return a single value.[8]

The procedure begins with a mandatory heading part to hold the procedure name and optionally the procedure parameter list. Next come the declarative, executable and exception-handling parts, as in the PL/SQL Anonymous Block. A simple procedure might look like this:

CREATEPROCEDUREcreate_email_address(-- Procedure heading part beginsname1VARCHAR2,name2VARCHAR2,companyVARCHAR2,emailOUTVARCHAR2)-- Procedure heading part endsAS-- Declarative part begins (optional)error_messageVARCHAR2(30):='Email address is too long.';BEGIN-- Executable part begins (mandatory)email:=name1||'.'||name2||'@'||company;EXCEPTION-- Exception-handling part begins (optional)WHENVALUE_ERRORTHENDBMS_OUTPUT.PUT_LINE(error_message);ENDcreate_email_address;

The example above shows a standalone procedure - this type of procedure is created and stored in a database schema using the CREATE PROCEDURE statement. A procedure may also be created in a PL/SQL package - this is called a Package Procedure. A procedure created in a PL/SQL anonymous block is called a nested procedure. The standalone or package procedures, stored in the database, are referred to as "stored procedures".

Procedures can have three types of parameters: IN, OUT and IN OUT.

  1. An IN parameter is used as input only. An IN parameter is passed by reference, though it can be changed by the inactive program.
  2. An OUT parameter is initially NULL. The program assigns the parameter value and that value is returned to the calling program.
  3. An IN OUT parameter may or may not have an initial value. That 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 NO-COPY hint - may be passedby reference.

PL/SQL also supports external procedures via the Oracle database's standardext-proc process.[9]

Package

[edit]

Packages are groups of conceptually linked functions, procedures, variables, PL/SQL table and record TYPE statements, constants, cursors, etc. The use of packages promotes re-use of code. Packages are composed of the package specification and an optional package body. The specification is the interface to the application; it declares the types, variables, constants, exceptions, cursors, and subprograms available. The body fully defines cursors and subprograms, and so implements the specification.Two advantages of packages are:[10]

  1. Modular approach, encapsulation/hiding ofbusiness logic, security, performance improvement, re-usability. They supportobject-oriented programming features likefunction overloading and encapsulation.
  2. Using package variables one can declare session level (scoped) variables since variables declared in the package specification have a session scope.

Trigger

[edit]
Main article:Database trigger

Adatabase trigger is like a stored procedure that Oracle Database invokes automatically whenever a specified event occurs. It is a named PL/SQL unit that is stored in the database and can be invoked repeatedly. Unlike a stored procedure, you can enable and disable a trigger, but you cannot explicitly invoke it. While a trigger is enabled, the database automatically invokes it—that is, the trigger fires—whenever its triggering event occurs. While a trigger is disabled, it does not fire.

You create a trigger with the CREATE TRIGGER statement. You specify the triggering event in terms of triggering statements, and the item they act on. The trigger is said to be created on or defined on the item—which is either a table, aview, a schema, or the database. You also specify the timing point, which determines whether the trigger fires before or after the triggering statement runs and whether it fires for each row that the triggering statement affects.

If the trigger is created on a table or view, then the triggering event is composed of DML statements, and the trigger is called a DML trigger. If the trigger is created on a schema or the database, then the triggering event is composed of either DDL or database operation statements, and the trigger is called a system trigger.

An INSTEAD OF trigger is either: A DML trigger created on a view or a system trigger defined on a CREATE statement. The database fires the INSTEAD OF trigger instead of running the triggering statement.

Purpose of triggers

[edit]

Triggers can be written for the following purposes:

  • Generating somederived column values automatically
  • Enforcing referential integrity
  • Event logging and storing information on table access
  • Auditing
  • Synchronous replication of tables
  • Imposing security authorizations
  • Preventing invalid transactions

Data types

[edit]

The majordatatypes in PL/SQL include NUMBER, CHAR, VARCHAR2, DATE and TIMESTAMP.

Numeric variables

[edit]
variable_namenumber([P,S]):=0;

To define a numeric variable, the programmer appends the variable typeNUMBER to the name definition.To specify the (optional) precision (P) and the (optional) scale (S), one can further append these in round brackets, separated by a comma. ("Precision" in this context refers to the number of digits the variable can hold, and "scale" refers to the number of digits that can follow the decimal point.)

A selection of other data-types for numeric variables would include:binary_float, binary_double, dec, decimal, double precision, float, integer, int, numeric, real, small-int, binary_integer.

Character variables

[edit]
variable_namevarchar2(20):='Text';-- e.g.:addressvarchar2(20):='lake view road';

To define a character variable, the programmer normally appends the variable type VARCHAR2 to the name definition. There follows in brackets the maximum number of characters the variable can store.

Other datatypes for character variables include: varchar, char, long, raw, long raw, nchar, nchar2, clob, blob, and bfile.

Date variables

[edit]
variable_namedate:=to_date('01-01-2005 14:20:23','DD-MM-YYYY hh24:mi:ss');

Date variables can contain date and time. The time may be left out, but there is no way to define a variable that only contains the time. There is no DATETIME type. And there is a TIME type. But there is no TIMESTAMP type that can contain fine-grained timestamp up to millisecond or nanosecond.TheTO_DATE function can be used to convert strings to date values. The function converts the first quoted string into a date, using as a definition the second quoted string, for example:

to_date('31-12-2004','dd-mm-yyyy')

or

to_date('31-Dec-2004','dd-mon-yyyy','NLS_DATE_LANGUAGE = American')

To convert the dates to strings one uses the functionTO_CHAR (date_string, format_string).

PL/SQL also supports the use of ANSI date and interval literals.[11] The following clause gives an 18-month range:

WHEREdateFieldBETWEENDATE'2004-12-30'-INTERVAL'1-6'YEARTOMONTHANDDATE'2004-12-30'

Exceptions

[edit]

Exceptions—errors during code execution—are of two types: user-defined and predefined.

User-defined exceptions are always raised explicitly by the programmers, using theRAISE orRAISE_APPLICATION_ERROR commands, in any situation where they determine it is impossible for normal execution to continue. TheRAISE command has the syntax:

RAISE<exceptionname>;

Oracle Corporation haspredefined several exceptions likeNO_DATA_FOUND,TOO_MANY_ROWS,etc.Each exception has an SQL error number and SQL error message associated with it. Programmers can access these by using theSQLCODE andSQLERRM functions.

Datatypes for specific columns

[edit]
Variable_nameTable_name.Column_name%type;

This syntax defines a variable of the type of the referenced column on the referenced tables.

Programmers specify user-defined datatypes with the syntax:

typedata_typeisrecord(field_1type_1:=xyz,field_2type_2:=xyz,...,field_ntype_n:=xyz);

For example:

declaretypet_addressisrecord(nameaddress.name%type,streetaddress.street%type,street_numberaddress.street_number%type,postcodeaddress.postcode%type);v_addresst_address;beginselectname,street,street_number,postcodeintov_addressfromaddresswhererownum=1;end;

This sample program defines its own datatype, calledt_address, which contains the fieldsname, street, street_number andpostcode.

So according to the example, we are able to copy the data from the database to the fields in the program.

Using this datatype the programmer has defined a variable calledv_address and loaded it with data from the ADDRESS table.

Programmers can address individual attributes in such a structure by means of the dot-notation, thus:

v_address.street := 'High Street';

Conditional statements

[edit]

The following code segment shows the IF-THEN-ELSIF-ELSE construct. The ELSIF and ELSE parts are optional so it is possible to create simpler IF-THEN or, IF-THEN-ELSE constructs.

IFx=1THENsequence_of_statements_1;ELSIFx=2THENsequence_of_statements_2;ELSIFx=3THENsequence_of_statements_3;ELSIFx=4THENsequence_of_statements_4;ELSIFx=5THENsequence_of_statements_5;ELSEsequence_of_statements_N;ENDIF;

The CASE statement simplifies some large IF-THEN-ELSIF-ELSE structures.

CASEWHENx=1THENsequence_of_statements_1;WHENx=2THENsequence_of_statements_2;WHENx=3THENsequence_of_statements_3;WHENx=4THENsequence_of_statements_4;WHENx=5THENsequence_of_statements_5;ELSEsequence_of_statements_N;ENDCASE;

CASE statement can be used with predefined selector:

CASExWHEN1THENsequence_of_statements_1;WHEN2THENsequence_of_statements_2;WHEN3THENsequence_of_statements_3;WHEN4THENsequence_of_statements_4;WHEN5THENsequence_of_statements_5;ELSEsequence_of_statements_N;ENDCASE;

Array handling

[edit]

PL/SQL refers toarrays as "collections". The language offers three types of collections:

  1. Associative arrays (Index-by tables)
  2. Nested tables
  3. Varrays (variable-size arrays)

Programmers must specify an upper limit for varrays, but need not for index-by tables or for nested tables. The language includes several collectionmethods used to manipulate collection elements: for example FIRST, LAST, NEXT, PRIOR, EXTEND, TRIM, DELETE, etc. Index-by tables can be used to simulate associative arrays, as in thisexample of a memo function for Ackermann's function in PL/SQL.

Associative arrays (index-by tables)

[edit]

With index-by tables, the array can be indexed by numbers or strings. It parallels aJavamap, which comprises key-value pairs. There is only one dimension and it is unbounded.

Nested tables

[edit]

Withnested tables the programmer needs to understand what is nested. Here, a new type is created that may be composed of a number of components. That type can then be used to make a column in a table, and nested within that column are those components.

Varrays (variable-size arrays)

[edit]

With Varrays you need to understand that the word "variable" in the phrase "variable-size arrays" doesn't apply to the size of the array in the way you might think that it would. The size the array is declared with is in fact fixed. The number of elements in the array is variable up to the declared size. Arguably then, variable-sized arrays aren't that variable in size.

Cursors

[edit]

Acursor is a pointer to a private SQL area that stores information coming from a SELECT or data manipulation language (DML) statement (INSERT, UPDATE, DELETE, or MERGE). Acursor holds the rows (one or more) returned by a SQL statement. The set of rows thecursor holds is referred to as the active set.[12]

Acursor can be explicit or implicit. In a FOR loop, an explicit cursor shall be used if the query will be reused, otherwise an implicit cursor is preferred. If using a cursor inside a loop, use a FETCH is recommended when needing to bulk collect or when needing dynamic SQL.

Looping

[edit]

As a procedural language by definition, PL/SQL provides severaliteration constructs, including basic LOOP statements,WHILE loops,FOR loops, and Cursor FOR loops. Since Oracle 7.3 the REF CURSOR type was introduced to allow recordsets to be returned from stored procedures and functions. Oracle 9i introduced the predefined SYS_REFCURSOR type, meaning we no longer have to define our own REF CURSOR types.

LOOP statements

[edit]
<<parent_loop>>LOOPstatements<<child_loop>>loopstatementsexitparent_loopwhen<condition>;-- Terminates both loopsexitwhen<condition>;-- Returns control to parent_loopendloopchild_loop;if<condition>thencontinue;-- continue to next iterationendif;exitwhen<condition>;ENDLOOPparent_loop;

[13]

Loops can be terminated by using theEXITkeyword, or by raising anexception.

FOR loops

[edit]
DECLAREvarNUMBER;BEGIN/* N.B. for loop variables in PL/SQL are new declarations, with scope only inside the loop */FORvarIN0..10LOOPDBMS_OUTPUT.PUT_LINE(var);ENDLOOP;IFvarISNULLTHENDBMS_OUTPUT.PUT_LINE('var is null');ELSEDBMS_OUTPUT.PUT_LINE('var is not null');ENDIF;END;

Output:

 0 1 2 3 4 5 6 7 8 9 10 var is null

Cursor FOR loops

[edit]
FORRecordIndexIN(SELECTperson_codeFROMpeople_table)LOOPDBMS_OUTPUT.PUT_LINE(RecordIndex.person_code);ENDLOOP;

Cursor-for loops automatically open acursor, read in their data and close the cursor again.

As an alternative, the PL/SQL programmer can pre-define the cursor's SELECT-statement in advance to (for example) allow re-use or make the code more understandable (especially useful in the case of long or complex queries).

DECLARECURSORcursor_personISSELECTperson_codeFROMpeople_table;BEGINFORRecordIndexINcursor_personLOOPDBMS_OUTPUT.PUT_LINE(recordIndex.person_code);ENDLOOP;END;

The concept of the person_code within the FOR-loop gets expressed with dot-notation ("."):

RecordIndex.person_code

Dynamic SQL

[edit]

While programmers can readily embedData Manipulation Language (DML) statements directly into PL/SQL code using straightforward SQL statements,Data Definition Language (DDL) requires more complex "Dynamic SQL" statements in the PL/SQL code. However, DML statements underpin the majority of PL/SQL code in typical software applications.

In the case of PL/SQL dynamic SQL, early versions of the Oracle Database required the use of a complicated OracleDBMS_SQL package library. More recent versions have however introduced a simpler "Native Dynamic SQL", along with an associatedEXECUTE IMMEDIATE syntax.

Similar languages

[edit]

PL/SQL works analogously to the embedded procedural languages associated with otherrelational databases. For example,SybaseASE andMicrosoftSQL Server haveTransact-SQL,PostgreSQL hasPL/pgSQL (which emulates PL/SQL to an extent),MariaDB includes a PL/SQL compatibility parser,[14] andIBM Db2 includes SQL Procedural Language,[15] which conforms to theISO SQL’sSQL/PSM standard.

The designers of PL/SQL modeled its syntax on that ofAda. Both Ada and PL/SQL havePascal as a common ancestor, and so PL/SQL also resembles Pascal in most aspects. However, the structure of a PL/SQL package does not resemble the basicObject Pascal program structure as implemented by aBorland Delphi orFree Pascal unit. Programmers can define public and private global data-types, constants, and static variables in a PL/SQL package.[16]

PL/SQL also allows for the definition of classes and instantiating these as objects in PL/SQL code. This resembles usage inobject-oriented programming languages likeObject Pascal,C++ andJava. PL/SQL refers to a class as an "Abstract Data Type" (ADT) or "User Defined Type" (UDT), and defines it as anOracle SQL data-type as opposed to a PL/SQL user-defined type, allowing its use in both theOracle SQL Engine and theOracle PL/SQL engine. The constructor and methods of an Abstract Data Type are written in PL/SQL. The resulting Abstract Data Type can operate as an object class in PL/SQL. Such objects can also persist as column values in Oracle database tables.

PL/SQL is fundamentally distinct fromTransact-SQL, despite superficial similarities. Porting code from one to the other usually involves non-trivial work, not only due to the differences in the feature sets of the two languages,[17] but also due to the very significant differences in the way Oracle and SQL Server deal withconcurrency andlocking.

TheStepSqlite product is a PL/SQL compiler for the popular small databaseSQLite which supports a subset of PL/SQL syntax. Oracle'sBerkeley DB 11g R2 release added support forSQL based on the popular SQLite API by including a version of SQLite in Berkeley DB.[18] Consequently, StepSqlite can also be used as a third-party tool to run PL/SQL code on Berkeley DB.[19]

See also

[edit]

References

[edit]
  1. ^Serge Rielau (srielau@ca.ibm.com), SQL Architect, STSM, IBM."DB2 10: Run Oracle applications on DB2 10 for Linux, UNIX, and Windows". Ibm.com. Retrieved2012-07-26.{{cite web}}: CS1 maint: multiple names: authors list (link)
  2. ^Steven Feuerstein (1995), "Oracle PL/SQL Programming", 1st, First Edition.
  3. ^"Oracle Compliance with SQL/PSM".
  4. ^"Oracle Server Features". Oracle. Retrieved2020-09-21.
  5. ^"PL/SQL Anonymous Blocks". Oracle. Retrieved2020-09-21.
  6. ^"Database SQL Reference". Oracle. Retrieved2020-09-21.
  7. ^Nanda, Arup; Feuerstein, Steven (2005).Oracle PL/SQL for DBAs. O'Reilly Series. O'Reilly Media, Inc. pp. 122, 429.ISBN 978-0-596-00587-0. Retrieved2011-01-11.A pipelined table function [...] returns a result set as a collection [...] iteratively. [... A]s each row is ready to be assigned to the collection, it is 'piped out' of the function.
  8. ^"Database SQL Reference". Oracle. Retrieved2020-09-21.
  9. ^Gupta, Saurabh K. (2012). "5: Using Advanced Interface Methods".Advanced Oracle PL/SQL Developer's Guide. Professional experience distilled (2 ed.). Birmingham: Packt Publishing Ltd (published 2016). p. 143.ISBN 9781785282522. Retrieved2017-06-08.Whenever the PL/SQL runtime engine encounters an external procedure call, the Oracle Database starts the extproc process. The database passes on the information received from the call specification to theextproc process, which helps it to locate the external procedure within the library and execute it using the supplied parameters. Theextproc process loads the dynamic linked library, executes the external procedure, and returns the result back to the database.
  10. ^"Coding PL/SQL Procedures and Packages". Oracle. Retrieved2020-09-21.
  11. ^"Literals".Oracle iSQL Reference 10g Release 2 (10.2). Oracle. Retrieved2009-03-20.
  12. ^Feuerstein, Steven."Working with Cursors".oracle.com.
  13. ^"Database PL/SQL User's Guide and Reference".download.oracle.com.
  14. ^"What's New in MariaDB Server 10.3"(PDF). MariaDB. Retrieved2018-08-21.
  15. ^"SQL PL". Publib.boulder.ibm.com. Retrieved2012-07-26.
  16. ^Oracle documentation"Private and public items in PL/SQL"
  17. ^"Migrating from Oracle to SQL Server - T-SQL, PL/SQL differences: Narayana Vyas Kondreddi's home page".vyaskn.tripod.com.
  18. ^Burd, Gregory (March 24, 2010)."@humanications We didn't re-implement the SQLite API, we include a version of SQLite which uses Berkeley DB for storage (replacing btree.c)".
  19. ^"Official Berkeley DB FAQ".Oracle Corporation. RetrievedMarch 30, 2010.Does Berkeley DB support PL/SQL?

Further reading

[edit]

External links

[edit]
The WikibookOracle Programming has a page on the topic of:SQL Cheatsheet
Corporate directors
Acquisitions (list)
Databases
Programming languages
IDEs
Middleware
Operating systems
Computer hardware
Computer appliances
Education and recognition
International
National
Other
Retrieved from "https://en.wikipedia.org/w/index.php?title=PL/SQL&oldid=1336489564"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp