pl/sql the oracle programming language. purpose sql is a non-procedural language. often we need to...

28
PL/SQL The Oracle Programming Language

Post on 21-Dec-2015

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental

PL/SQL

The Oracle Programming Language

Page 2: PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental

Purpose

• SQL is a non-procedural language.• Often we need to put structure on transactions,

so we use the supplemental structures provided by PL/SQL– For sequence, selection, iteration– Variables, constants and data types– Assignment and other arithmetic statements– Customised error handling

• PL/SQL is a superset of the Data Manipulation Language part of SQL.

Page 3: PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental

What is it?

• PL/SQL allows the user to store compiled code in the database, giving central access to routines associated with the data.

• They can be accessed – over the web, using the Web Application server– As form applications, under Developer/2000– As embedded program logic in client-side applications– Using the SQL*Plus environment

Page 4: PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental

Data Definition Language aspect

• PL/SQL does not provide for the Data Definition Language aspect of SQL.

• There is a package called dbms_sql that allows for data definition statements.

Page 5: PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental

Scalar Data types

• Numeric– binary_integer integer from -231 -1 to 231 – 1– natural integer from 0 to 231 – positive integer from 1 to 231 – Number(p,s) p is precision, s is scale

• Character– char(n) fixed length string of length n– varchar2(n) variable length string of maximum length n

• Other– Boolean boolean data type (true, false)– Date, time date Same as Oracle’s date.

Page 6: PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental

Variables

• <variable-name> <datatype> [not null] [:=<initial-value>];

• <constant-name> constant <datatype> := <value>

• E.g.i binary_integer;Cno number(5) not null:= 1111;Cname varchar2(30);Commission real(5,2):=12.5;Maxcolumns constant integer(2):=30;Hired_date date;Done boolean;

Page 7: PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental

More on variables

• All variables with a ‘not null’ clause must be given an initial value.

• Any variable that is allowed a null and is not explicitly initialised is null.

• Anchored data types– A variable can be declared as of the same type as a

database column:Stockvar builder.stock.stock_code%type

Will declare a new variable stockvar that has the same type as the stock_code column in the stock table in the builder schema.

Page 8: PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental

Program control statements

• Programs in a 3GL are made up of:– Comments and blank lines

null;/*This is a block of commentCode, running over > 1 line*/--This is a comment on a single line--

– Sequence• Assignment, I/O, Arithmetic

– Selection• If-then, If-then-else, If-then-elseif

– Iteration• Loop

Page 9: PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental

Sequence

• The assignment symbol is the same as that used in Pascal – a colon followed by an equals :=

• This can be used for assignment and arithmetic:i := i+1;

Cname := 'Jones';

sales := price * qty;

Page 10: PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental

I/O

• dbms_output is a built-in package that allows the user to display information to the session output device (e.g. screen) as the program executes. The most commonly used procedures are:– put– put_line– new_line– get_line– get_lines– disable– enable

Page 11: PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental

dbms_output

• As PL/SQL is essentially a back-end product, there is normally no need for screen output. However, for debugging purposes, we often need it.

• The dbms_output package works with a buffer into which information can be written:– Put, put_line, new_line

• The information can be subsequently retrieved:– get_line, get_lines

Page 12: PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental

Turning on/off the dbms_package

• The package can be disabled:SQL> dbms_output.disable

• no dbms_output procedures will work except enable. This can also be done by entering:

SQL> set serveroutput off• Or enabled:

SQL> dbms_output.enable(1000000)OrSQL> set serveroutput on size 1000000Where size is the buffer size. Default size is 2000If you don’t specify the buffer size, the buffer may

overflow.

Page 13: PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental

put and put_line

• put puts data into the buffer, without an end of line marker:

dbms_output.put(stock.stock_code);

dbms_output.put(stock.stock_description);

dbms_output.put(stock.unit_price);

• Put_line puts data into the buffer, with an end of line marker.

Page 14: PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental

get_line

Get_line

Gets one line of information from the buffer, always in character string format.

procedure get_line (line out varchar2, status out integer);

The size of the varchar2 can be up to 255.

• See later for use of get_line(s)

Page 15: PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental

Selection

• If-then statement:if(quantityrequired > stock_level) then

dbms_output.putline(‘Stock level is only ’||Stock_level);

End if;

• If-then-else statement:if(quantityrequired > stock_level) then

dbms_output.putline(‘Stock level is only ’ ||Stock_level);

Else dbms_output.putline(‘Now have’||(stock_level-

quantityrequired)||’ items remaining’); stock_level = stock_level – quantityrequired; End if;

Page 16: PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental

If-then-elseif

• This is the closest that PL/SQL comes to a case statement:

If(sorderdate > delivereddate) thendbms_output.putline(‘The order cannot be delivered before it is ordered’);

Elseif (sorderdate = delivereddate) then dbms_output.putline(‘The order delivery date is

the same as the delivery date!’);Else dbms_output.putline (‘There were ‘||(deliverydate

– sorderdate)||’ days between the order and the delivery’);

End if;

Page 17: PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental

Iteration

• There are three types of iterative statement:– Loop– For loop– While loop

Page 18: PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental

loop

• The basic loop statement repeatedly executes until it finds an exit condition:Loopi:= i + 1;if i > 10 then

exit;end if;sum := sum + 1;

End loop;

Page 19: PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental

for loop

Syntax:for <loop-counter> in [reverse] <lower>..<upper> loop<statements>;

End loop;

Example:for i in 1..10 loopdbms_output.put_line(‘i = ‘,||i);

sum := sum + i;end loop;

Page 20: PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental

While loop

Syntax:While <condition> loop

<statement-list>

end loop;

Example:i := 1;

sum := 0;

While (i < 1000) loop

sum := sum + 1;

i := 2 * i;

end loop;

Page 21: PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental

Anonymous blocks

• These are blocks of statements that can be run in a sequence. They can be anonymous or a subprogram.

• An anonymous block has the following structure:

Page 22: PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental

Anonymous block structure

Declare -- Declaration Section-- Data, subprogram declarations

begin-- Executable Sectionnull; -- Program statements

Exception-- Exception section-- Exception handlerswhen others then

null; -- default handlerend;

Page 23: PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental

Declarations

• The declaration section holds the declarations of– All variables and constants to be used in the scope of the block– Any subroutines that are local to the block– Any exception handler that we want to define– Any cursors we want to define (see later in course)

• The executable section holds– The main ‘program’ logic of the block.

• The exception section holds– Handlers for any exceptions that we can reasonably expect to

occur during processing– A default exception handler to handle any exceptions that we

haven’t foreseen.

Page 24: PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental

Exceptions

• Exceptions are occurrences such as:– When we allocate stock to a customer order, if

the stock falls below reorder level, we want to log the fact that the stock needs to be reordered.

– When we try to allocate stock to a customer order, but there isn’t enough stock, we need to have a high level emergency warning for reordering.

Page 25: PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental

Example

• The following procedure– Takes a customer id– Requests the customer details from the builder table customer– If successful

• Displays the customer details

– Else• Displays an error message.

• Calling it– It is called getcust.sql– It is stored on my C drive.– To call it, I open SQL*PlusSQL>start c:getcust

Page 26: PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental

Code (1 of 3)

DECLAREcnum builder.customer.customer_id%type;cname builder.customer.customer_name%type;caddr builder.customer.customer_address%type;status boolean;

procedure get_cust_details (cust_no in

builder.customer.customer_id%type,cust_name out

builder.customer.customer_name%type,cust_addr out

builder.customer.customer_address%type,status out boolean) is

Page 27: PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental

Code(2 of 3)begin

select builder.customer.customer_name, builder.customer.customer_address into cust_name, cust_addr from customerwhere builder.customer.customer_id =

cnum;status := true;

exceptionwhen no_data_found then

status := false;end;

Page 28: PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental

Code (3 of 3)

begincnum:=1;get_cust_details(cnum,cname, caddr,

status);if (status) then

dbms_output.put_line(cnum || ' ' || cname || ' ' || caddr);

elsedbms_output.put_line('Customer '

|| cnum || ' not found');end if;

end;