Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 PL/SQL The Oracle Programming Language

2 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.

3 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

4 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.

5 Scalar Data types Numeric –binary_integer integer from -2 31 -1 to 2 31 – 1 –natural integer from 0 to 2 31 –positive integer from 1 to 2 31 –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.

6 Variables [not null] [:= ]; constant := 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;

7 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.

8 Program control statements Programs in a 3GL are made up of: –Comments and blank lines null; /*This is a block of comment Code, 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

9 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;

10 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

11 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

12 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) Or SQL> set serveroutput on size 1000000 Where size is the buffer size. Default size is 2000 If you don’t specify the buffer size, the buffer may overflow.

13 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.

14 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)

15 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;

16 If-then-elseif This is the closest that PL/SQL comes to a case statement: If(sorderdate > delivereddate) then dbms_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;

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

18 loop The basic loop statement repeatedly executes until it finds an exit condition: Loop i:= i + 1; if i > 10 then exit; end if; sum := sum + 1; End loop;

19 for loop Syntax: for in [reverse].. loop ; End loop; Example: for i in 1..10 loop dbms_output.put_line(‘i = ‘,||i); sum := sum + i; end loop;

20 While loop Syntax: While loop end loop; Example: i := 1; sum := 0; While (i < 1000) loop sum := sum + 1; i := 2 * i; end loop;

21 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:

22 Anonymous block structure Declare -- Declaration Section --Data, subprogram declarations begin -- Executable Section null; -- Program statements Exception --Exception section -- Exception handlers when others then null; -- default handler end;

23 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.

24 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.

25 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*Plus SQL>start c:getcust

26 Code (1 of 3) DECLARE cnum builder.customer.customer_id%type; cname builder.customer.customer_name%type; caddr builder.customer.customer_address%type; status boolean; procedure get_cust_details ( cust_noin builder.customer.customer_id%type, cust_nameout builder.customer.customer_name%type, cust_addrout builder.customer.customer_address%type, statusoutboolean) is

27 Code(2 of 3) begin select builder.customer.customer_name, builder.customer.customer_address into cust_name, cust_addr from customer where builder.customer.customer_id = cnum; status := true; exception when no_data_found then status := false; end;

28 Code (3 of 3) begin cnum:=1; get_cust_details(cnum,cname, caddr, status); if (status) then dbms_output.put_line(cnum || ' ' || cname || ' ' || caddr); else dbms_output.put_line('Customer ' || cnum || ' not found'); end if; end;


Download ppt "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."

Similar presentations


Ads by Google