42
#14 PL/SQL,Embedded SQL 402 ©Fall, 2001, LRX HUST,Wuhan,China PL/SQL, Embedded SQL Lecture #14 Autumn, 2001

PL/SQL, Embedded SQL - 智能与分布计算实验室idc.hust.edu.cn/~rxli/teaching/database/slides/14 PLSQLEmbeddedS… · ©Fall, 2001, LRX #14 PL/SQL,Embedded SQL HUST,Wuhan,China

  • Upload
    vutram

  • View
    237

  • Download
    1

Embed Size (px)

Citation preview

#14 PL/SQL,Embedded SQL 402©Fall, 2001, LRX HUST,Wuhan,China

PL/SQL, Embedded SQL

Lecture #14Autumn, 2001

#14 PL/SQL,Embedded SQL 403©Fall, 2001, LRX HUST,Wuhan,China

PL/SQL

l Found only in the Oracle SQL processor (sqlplus).

l A compromise between completely procedural programming and SQL's very high-level, but limited statements.

#14 PL/SQL,Embedded SQL 404©Fall, 2001, LRX HUST,Wuhan,China

PL/SQL (II)

l Allows local variables, loops, procedures, examination of relations one tuple at a time.

#14 PL/SQL,Embedded SQL 405©Fall, 2001, LRX HUST,Wuhan,China

PL/SQL (III)

l Rough form:DECLARE

declarationsBEGIN

executable statementsEND;.run;

#14 PL/SQL,Embedded SQL 406©Fall, 2001, LRX HUST,Wuhan,China

PL/SQL (IV)

l DECLARE portion is optional.l Dot and run are needed to end the

statement and execute it.

#14 PL/SQL,Embedded SQL 407©Fall, 2001, LRX HUST,Wuhan,China

Simplest Form: Sequence of Modifications

l Likes(drinker, beer)BEGIN

INSERT INTO LikesVALUES('Sally', 'Bud');DELETE FROM LikesWHERE drinker = 'Fred'

AND beer = 'Miller';END;.run;

#14 PL/SQL,Embedded SQL 408©Fall, 2001, LRX HUST,Wuhan,China

Procedures

l Stored database objects that use a PL/SQL statement in their body.

#14 PL/SQL,Embedded SQL 409©Fall, 2001, LRX HUST,Wuhan,China

Procedures (II)

l Form of declaration:CREATE OR REPLACE PROCEDURE <name>(<arglist>) AS

declarationsBEGIN

<PL/SQL statements>END;.run;

#14 PL/SQL,Embedded SQL 410©Fall, 2001, LRX HUST,Wuhan,China

Procedures (III)

l Argument list has name-mode-type triples.»Mode: IN, OUT, or INOUT, as in ODL.»Types: standard SQL + generic types like

NUMBER = any integer or real type.

#14 PL/SQL,Embedded SQL 411©Fall, 2001, LRX HUST,Wuhan,China

Procedures (IV)

»Since types in procedures must match their types in the DB schema, you should generally use an expression of the form

relation.attribute%TYPEto capture the type correctly.

#14 PL/SQL,Embedded SQL 412©Fall, 2001, LRX HUST,Wuhan,China

Example

l A procedure to take a beer and price and add it to Joe's menu. Sells(bar, beer, price)

#14 PL/SQL,Embedded SQL 413©Fall, 2001, LRX HUST,Wuhan,China

Example (II)

l CREATE PROCEDURE joeMenu( b IN Sells.beer%TYPE, p IN Sells.price%TYPE

) AS BEGIN

INSERT INTO Sells VALUES('Joe''s Bar', b, p);

END; . run;

#14 PL/SQL,Embedded SQL 414©Fall, 2001, LRX HUST,Wuhan,China

Example (III)

l Note "run" only stores the procedure; it doesn't execute the procedure.

#14 PL/SQL,Embedded SQL 415©Fall, 2001, LRX HUST,Wuhan,China

Invoking Procedures

l A procedure call may appear in the body of a PL/SQL statement.

l Example: BEGIN

joeMenu('Bud', 2.50); joeMenu('MooseDrool', 5.00);

END; . run;

#14 PL/SQL,Embedded SQL 416©Fall, 2001, LRX HUST,Wuhan,China

Assignment

l Assign expressions to declared variables with :=.

#14 PL/SQL,Embedded SQL 417©Fall, 2001, LRX HUST,Wuhan,China

Branches

l IF <condition> THEN <statement(s)>

ELSE <statement(s)>

END IF;l But in nests, use ELSIF in place of ELSE

IF.

#14 PL/SQL,Embedded SQL 418©Fall, 2001, LRX HUST,Wuhan,China

Loops

l LOOP . . . EXIT WHEN <condition> . . . END LOOP;

#14 PL/SQL,Embedded SQL 419©Fall, 2001, LRX HUST,Wuhan,China

Cursors

l Declare by: CURSOR <name> IS select-from-where statement

#14 PL/SQL,Embedded SQL 420©Fall, 2001, LRX HUST,Wuhan,China

Cursors (II)

l Cursor gets each tuple from the relation produced by the select-from-where, in turn, using a fetch statement in a loop.»Fetch statement:

FETCH <cursor name> INTO variable list;

#14 PL/SQL,Embedded SQL 421©Fall, 2001, LRX HUST,Wuhan,China

Cursors (III)

l Break the loop by a statement of the form: EXIT WHEN <cursor name>%NOTFOUND;»True when there are no more tuples to get.

l Open and close the cursor with OPEN and CLOSE.

#14 PL/SQL,Embedded SQL 422©Fall, 2001, LRX HUST,Wuhan,China

Example

l A procedure that examines the menu for Joe's Bar and raises by $1.00 all prices that are less than $3.00.Sells(bar, beer, price)

#14 PL/SQL,Embedded SQL 423©Fall, 2001, LRX HUST,Wuhan,China

Example (II)

l This simple price-change algorithm can be implemented by a single UPDATE statement, but more complicated price changes could not.

#14 PL/SQL,Embedded SQL 424©Fall, 2001, LRX HUST,Wuhan,China

Example (III)

l CREATE PROCEDURE joeGouge() AS theBeer Sells.beer%TYPE; thePrice Sells.price%TYPE; CURSOR c IS

SELECT beer, price FROM Sells WHERE bar = 'Joe''s bar';

#14 PL/SQL,Embedded SQL 425©Fall, 2001, LRX HUST,Wuhan,China

Example (III)

l BEGIN OPEN c; LOOP

FETCH c INTO theBeer, thePrice; EXIT WHEN c%NOTFOUND; IF thePrice < 3.00 THEN

UDPATE Sells SET price = thePrice + 1.00 WHERE bar = 'Joe''s Bar'

AND beer = theBeer; END IF;

END LOOP;

#14 PL/SQL,Embedded SQL 426©Fall, 2001, LRX HUST,Wuhan,China

Example (IV)

l

CLOSE c; END; . run

#14 PL/SQL,Embedded SQL 427©Fall, 2001, LRX HUST,Wuhan,China

Single-Row Select

l Select-from-where in PL/SQL must have an INTO clause listing variables into which a tuple can be placed.

l It is an error if the select-from-where returns more than one tuple; you should have used a cursor.

#14 PL/SQL,Embedded SQL 428©Fall, 2001, LRX HUST,Wuhan,China

Example

l Find the price Joe charges for Bud (and drop it on the floor). Sells(bar, beer, price) DECLARE

p Sells.price%TYPE;

#14 PL/SQL,Embedded SQL 429©Fall, 2001, LRX HUST,Wuhan,China

Example (II)

l BEGIN SELECT price INTO p FROM Sells WHERE bar = 'Joe''s Bar' AND

beer = 'Bud'; END; . run

#14 PL/SQL,Embedded SQL 430©Fall, 2001, LRX HUST,Wuhan,China

SQL2 Embedded SQL

l Add to a conventional programming language (C in our examples) certain statements that represent SQL operations.

l Each embedded SQL statement introduced with EXEC SQL.

l Preprocessor converts C + SQL to pure C.» SQL statements become procedure calls.

#14 PL/SQL,Embedded SQL 431©Fall, 2001, LRX HUST,Wuhan,China

Shared Variables

l A special place for C declarations of variables that are accessible to both SQL and C.

l Bracketed byEXEC SQL BEGIN/END DECLARE SECTION;

#14 PL/SQL,Embedded SQL 432©Fall, 2001, LRX HUST,Wuhan,China

Shared Variables (II)

l In Oracle Pro/C (not C++) the "brackets" are optional.

l In C, variables used normally; in SQL, they must be preceded by a colon.

#14 PL/SQL,Embedded SQL 433©Fall, 2001, LRX HUST,Wuhan,China

Example

l Find the price for a given beer at a given bar. Sells(bar, beer, price)

#14 PL/SQL,Embedded SQL 434©Fall, 2001, LRX HUST,Wuhan,China

Example (II)

l EXEC SQL BEGIN DECLARE SECTION; char theBar[21], theBeer[21]; float thePrice;

EXEC SQL END DECLARE SECTION; . . . /* assign to theBar and theBeer */ . . . EXEC SQL SELECT price

INTO :thePrice FROM Sells WHERE beer = :theBeer AND

bar = :theBar; . . .

#14 PL/SQL,Embedded SQL 435©Fall, 2001, LRX HUST,Wuhan,China

Cursors

l Similar to PL/SQL cursors, with some syntactic differences.

#14 PL/SQL,Embedded SQL 436©Fall, 2001, LRX HUST,Wuhan,China

Example

l Print Joe's menu. Sells(bar, beer, price)

#14 PL/SQL,Embedded SQL 437©Fall, 2001, LRX HUST,Wuhan,China

Example (II)

l EXEC SQL BEGIN DECLARE SECTION; char theBeer[21]; float thePrice;

EXEC SQL END DECLARE SECTION; EXEC SQL DECLARE c CURSOR FOR

SELECT beer, price FROM Sells WHERE bar = 'Joe''s Bar';

EXEC SQL OPEN CURSOR c; while(1) {

EXEC SQL FETCH c INTO :theBeer, :thePrice;

if(NOT FOUND) break; /* format and print beer and price */ } EXEC SQL CLOSE CURSOR c;

#14 PL/SQL,Embedded SQL 438©Fall, 2001, LRX HUST,Wuhan,China

Oracle Vs. SQL2 Features

l SQL2 expects FROM in fetch-statement.l SQL2 defines an array of characters

SQLSTATE that is set every time the system is called.» Errors are signaled there.

#14 PL/SQL,Embedded SQL 439©Fall, 2001, LRX HUST,Wuhan,China

Oracle Vs. SQL2 Features (II)

» A failure for a cursor to find any more tuples is signaled there.

» However, Oracle provides us with a header file sqlca.h that declares a communication area and defines macros to access it.

» In particular, NOT FOUND is a macro that says "the no-tuple-found signal was set."

#14 PL/SQL,Embedded SQL 440©Fall, 2001, LRX HUST,Wuhan,China

Dynamic SQL

l Motivation:» Embedded SQL is fine for fixed applications,

e.g., a program that is used by a sales clerk to book an airline seat.

» It fails if you try to write a program like sqlplus, because you have compiled the code forsqlplus before you see the SQL statements typed in response to the SQL> prompt.

#14 PL/SQL,Embedded SQL 441©Fall, 2001, LRX HUST,Wuhan,China

Dynamic SQL (II)

» Two special statements of embedded SQL:– PREPARE turns a character string into an SQL

query.– EXECUTE executes that query.

#14 PL/SQL,Embedded SQL 442©Fall, 2001, LRX HUST,Wuhan,China

Example: Sqlplus Sketch

l EXEC SQL BEGIN DECLARE SECTION; char query[MAX_QUERY_LENGTH];

EXEC SQL END DECLARE SECTION; /* issue SQL> prompt */ /* read user's text into array query */ EXEC SQL PREPARE q FROM :query; EXEC SQL EXECUTE q; /* go back to reissue prompt */

#14 PL/SQL,Embedded SQL 443©Fall, 2001, LRX HUST,Wuhan,China

Example: Sqlplus Sketch (II)

l Once prepared, a query can be executed many times.

l Alternatively, PREPARE and EXECUTE can be combined into: EXEC SQL EXECUTE IMMEDIATE :query;