9. Extending the Relational Model: - SQL 99 –

9. Extending the Relational Model: - SQL 99 – 9. Extending the Relational Model: - SQL 99 –

inf.fu.berlin.de
from inf.fu.berlin.de More from this publisher
01.03.2014 Views

PL/pgSQL in a nutshell Example © HS-2008 CREATE OR REPLACE FUNCTION rand (hi integer,low int4) RETURNS integer AS $BODY$ -- no DECLARE BEGIN block RETURN low + ceil((hi-low) * random()); END; $BODY$ LANGUAGE 'plpgsql' VOLATILE; $-quote, useful for string literals During testing no DROP necessary Here go the variable declarations Standard functions: random() returns uniformly distributed values 0

PL/SQL declarations a) Standard declarations DECLARE price NUMBER; myBeer VARCHAR(20); b) Use types of tables DECLARE myBeer Beers.name%TYPE; a) Use row type DECLARE table column beerTuple Beers%ROWTYPE; PL/SQL control flow DECLARE a NUMBER; b NUMBER; BEGIN SELECT e,f INTO a,b FROM T1 WHERE e>1; IF b=1 THEN INSERT INTO T1 VALUES(b,a); ELSE INSERT INTO T1VALUES(b+10,a+10); END IF; END; © HS-2008 09-SQLExtensions-37 © HS-2008 09-SQLExtensions-38 PL/SQL Control flow DECLARE i NUMBER := 1; BEGIN LOOP INSERT INTO T1 VALUES(i,i); i := i+1; EXIT WHEN i>100; END LOOP; END; Similar : WHILE () LOOP ... END LOOP FOR IN .. LOOP ...END LOOP see Manual PL/SQL Insertion create table testNormal (empno number(10), ename varchar2(30), sal number(10)); Begin For i in 1..1000000 Loop Insert into test_normal values(i, dbms_random.string('U',80), dbms_random.value(1000,7000)); If mod(i, 10000) = 0 then Commit; End if; End loop; End; © HS-2008 09-SQLExtensions-39 © HS-2008 09-SQLExtensions-40 PL/SQL procedures CREATE PROCEDURE addtuple2 ( x T2.a%TYPE, y T2.b%TYPE) AS i NUMBER = dbms_random.value(1000,7000) /* here go declarations BEGIN INSERT INTO T2(k NUMBER,a, b) VALUES(i,x, y); END addtuple2; No DECLARE section call by value (IN), call by result (OUT), call by value-result (INOUT) © HS-2008 09-SQLExtensions-41 Realistic PL/SQL (Oracle) example -- very simple purchase transaction DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory WHERE product = 'TENNIS RACKET' -- FOR UPDATE OF quantity; IF qty_on_hand > 0 THEN -- check quantity UPDATE inventory SET quantity = quantity - 1 WHERE product = 'TENNIS RACKET'; INSERT INTO purchase_record VALUES ('Tennis racket purchased', SYSDATE); ELSE INSERT INTO purchase_record VALUES ('Out of tennis rackets', SYSDATE); END IF; COMMIT; END; / © HS-2008 09-SQLExtensions-42 7

PL/pg<strong>SQL</strong><br />

in a nutshell<br />

Example<br />

© HS-2008<br />

CREATE OR REPLACE FUNCTION rand (hi integer,low int4)<br />

RETURNS integer AS<br />

$BODY$<br />

-- no DECLARE<br />

BEGIN<br />

block<br />

RETURN low + ceil((hi-low) * random());<br />

END;<br />

$BODY$<br />

LANGUAGE 'plpgsql' VOLATILE;<br />

$-quote, useful for<br />

string literals<br />

During testing no DROP necessary<br />

Here go <strong>the</strong> variable<br />

declarations<br />

Standard functions:<br />

random() returns<br />

uniformly distributed<br />

values 0

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!