<<

Limitations of SQL

CS422 Principles of Systems Most programming language are Oracle PL/SQL Turing-complete SQL is not

Chengyu Sun California State University, Los Angeles

PSM and PL Oracle PL/SQL

Persistent Stored Modules (PSM) SQL and things you would expect a conventional programming language: n Commonly known as Stored Procedures n Variables and types n Stored in the database as other schema n Control flow statements objects n Procedures and functions Procedural Languages (PL) n Packages n Programming language for writing stored No just for creating stored procedures e.g. procedures can be used like other SQL statements n Based on SQL, Java, C#, Perl, Python, …

Example: Hello World Comments

/** A simple PL/SQL example */ C-style comments: /* comments */ begin -- print out “Hello World!” SQL-style comments: -- comments dbms_output.put_line( 'Hello World!' ); end; /

NOTE: the slash (/) at the end execute the PL/SQL code

1 Output Block Structure

DBMS_OUTPUT is one of the built-in [DECLARE packages in Oracle declaration_statements ] n PUT() BEGIN n PUT_LINE() executable_statements

Display the content of the output buffer [EXCEPTION in SQL*Plus exception_handling_statements ] n SET SERVEROUTPUT ON END;

Example: Sum Variable Types

declare All SQL types a integer := 10; b integer default 2; Some PL/SQL types s integer; n boolean : true, false, or null begin n string : same as varchar2 b := 5; n record : composite type s := a + b; n ref : pointer to a cursor dbms_output.put_line( 'sum is ' || s ); Taking the type of a , e.g. end; price items .price %type ; NOTE: Be careful with SQL keywords

Operators Example: Price Cap

Assignment Logical declare l_price items.price%type; n := n AND , OR , NOT begin Arithmetic Concatenation max(price) into l_price from items; n +,-,*,/, mod n || if l_price <= 9.99 then dbms_output.put_line( ' highest price is ' || l_price); Comparison SQL else n = n LIKE, IS NULL items set price = 9.99 price > 9.99; n >,>=,<,<= n IN, BETWEEN…AND dbms_output.put_line( 'price capped at 9.99.' ); n != , <> , ^= , ~= n All functions end if; end;

2 Naming Conventions SELECT…INTO

We want to avoid using the same names for variables and table columns SELECT select_list INTO variable_list FROM table_list A simple naming convention: [WHERE ] [ order_list ]; n Prefix local variable with l_ n Prefix package global variable with g_ n Prefix parameters with p_ SELECT result must be a single .

Branch Statement CASE Statement

CASE expression CASE IF condition1 THEN WHEN value1 THEN WHEN condition1 THEN statements1 statements statements ELSIF condition2 THEN [WHEN value2 THEN [WHEN constition2 THEN statements2 statements ] statements ] ELSE [ELSE [ELSE statements3 statements ] statements ] END IF; END CASE; END CASE;

NOTE: don’t forget the semicolon (;) after Note the difference between CASE Statement END IF. and CASE Expression .

Example – Factorial Loop Statements

declare n integer ; LOOP WHILE condition LOOP factorial integer := 1; statements statements i integer := 1; EXIT WHEN condition; END LOOP; begin statements n := 5; END LOOP; while i <= n loop factorial := factorial * i; FOR loop_variable IN [REVERSE] i := i+1; lower_bound .. upper_bound LOOP end loop; statements dbms_output.put_line( n || '! = ' || factorial ); END LOOP; end;

3 Cursors Cursors

An iterator of a collection of tuples An iterator of a collection of tuples We can use a cursor to process the We can use a cursor to process the rows returned by a SELECT statement rows returned by a SELECT statement

Cursors Cursors

An iterator of a collection of tuples An iterator of a collection of tuples We can use a cursor to process the We can use a cursor to process the rows returned by a SELECT statement rows returned by a SELECT statement

Cursors Cursors

An iterator of a collection of tuples An iterator of a collection of tuples We can use a cursor to process the We can use a cursor to process the rows returned by a SELECT statement rows returned by a SELECT statement

4 Cursors Cursors

An iterator of a collection of tuples An iterator of a collection of tuples We can use a cursor to process the We can use a cursor to process the rows returned by a SELECT statement rows returned by a SELECT statement

Example: Random Output Using Cursors

declare l_name string(32); Declaration l_price number; cursor c is select name, price from items ; OPEN begin open c; FETCH fetch c into l_name, l_price; while c %found loop CLOSE if dbms_random.random > 0 then dbms_output.put_line( l_name || ' ' || l_price ); end if; fetch c into l_name, l_price; end loop; close c; end;

Attributes Cursor FOR Loop

PL/SQL objects like tables,rows, columns, and cursors have attributes associated with them. FOR record_name IN cursor_name LOOP statements Attributes can be access with the % operator. END LOOP; Some useful attributes: n Column attributes: %TYPE n Table attributes: %ROWTYPE n Cursor attributes: %FOUND, %NOTFOUND

5 Cursors with Parameters Example: Exception

declare … declare cursor c (p_min_price number , p_max_price number ) is l_price items.price%type; select name, price from items begin where price >= p_min_price select price into l_price from items; and price <= p_max_price; dbms_output.put_line( l_price ); begin …. exception open c (1.99, 19.99); when too_many_rows then close c; dbms_output.put_line( 'there are too many prices.' ); … end; open c (99.99, 199.99); close c; … NOTE: the program does not resume after an end; exception is handled.

System Exceptions User Defined Exception

Some predefined system exceptions: DECLARE n TOO_MANY_ROWS exception_name EXCEPTION ; n ZERO_DIVIDE BEGIN n INVALID_NUMBER IF condition THEN n SELF_IS_NULL RAISE exception_name ; END IF; n SUBSCRIPT_OUTSIDE_LIMIT EXCEPTION n LOGIN_DENIED WHEN exception_name THEN n … statements n OTHERS END; w Error code is stored in SQLCODE

About PL/SQL Programming

It’s just programming like you always do Bring out your CS201 textbook and do some exercises with PL/SQL Ask “How to do X” questions in the class forum Avoid re-implementing SQL n For example, to compute max(price), use SELECT MAX(price) instead of a cursor to iterate through all tuples

6