Beruflich Dokumente
Kultur Dokumente
PL/SQL
Lecture 1
Variables
Used to store numbers, character strings, dates, and other data values Avoid using keywords, table names and column names as variable names Must be declared with data type before use: variable_name data_type_declaration;
3/16/2006
Examples
a binary_integer; cno number(5) not null :=1111; cname varchar2(30); commission real(5,2) :=12.5; maxcolumn constant integer(2) :=30; Hired_date date; done boolean;
Character
Boolean Date-Time
3/16/2006
3/16/2006
3/16/2006
PL/SQL
3/16/2006
PL/SQL
3/16/2006
PL/SQL
3/16/2006
PL/SQL
10
if-then-else
if-then-elseif
3/16/2006
PL/SQL
11
3/16/2006
3/16/2006
Example
1. if (cnum > 1000) and (cnum < 9000) then i := i+1; dbms_output.put_line(Customer || cnum); end if; 2.
if (cnum > 1000) and (cnum < 9000) then i := i+1; dbms_output.put_line( Valid Customer || cnum); else j := j+1; dbms_output.put_line(Invalid Customer || cnum); end if;
Example (cont.)
3. if (score > 90) then na := na+1; elseif (score > 80) then nb := nb+1; elseif (score > 70) then nc := nc+1; elseif (score > 60) then nd := nd+1; else nf := nf+1; end if;
3/16/2006
PL/SQL
13
3/16/2006
PL/SQL
14
Create the program in a text editor and save it to a file with the extension name as sql. On the Worksheet menu, click run local script.
3/16/2006 PL/SQL 15 3/16/2006 PL/SQL 16
3 kinds of loop control structures: loop, for loop, and while loop
Loop structures syntax
loop
for loop
while loop
3/16/2006
In-class Assignment
Assignment 1
3/16/2006
PL/SQL
18
3/16/2006
2.
for i in reverse 1..5 loop dmbs_output.put_line( i = || i); sum := sum + 2*i; end loop;
3/16/2006 PL/SQL 19 3/16/2006 PL/SQL 20
Program Structure
PL/SQL like many high-level languages, groups its statement into units called blocks. Blocks can be unnamed (anonymous blocks) or named (subprograms) The subprograms can be either functions or procedures.
Anonymous Block
An anonymous block has the following 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;
3/16/2006
PL/SQL
21
3/16/2006
PL/SQL
22
Example
--PL/SQL program to display current date DECLARE today_date DATE; BEGIN today_date := SYSDATE; DBMS_OUTPUT.PUT_LINE('Today''s date is '); DBMS_OUTPUT.PUT_LINE(today_date); END;
In-class Assignment
Assignment 2
3/16/2006
PL/SQL
23
3/16/2006
PL/SQL
24
3/16/2006
Example
declare type customers_table is table of customers%rowtype index by binary_integer; c_table customers_table; cursor c is select cno,cname,street,zip,phone from customers; c_rec c%rowtype; i binary_integer;; begin i := 1; for c_rec in c loop exit when c%notfound; c_table(i) := c_rec; i := i + 1; end loop; for i in 1..c_table.count loop DBMS_OUTPUT.PUT_LINE('c_table(' || i || ').cname = ' || c_table(i).cname);
end loop; end;
3/16/2006 PL/SQL 25 3/16/2006 PL/SQL 26
3/16/2006
PL/SQL
27
3/16/2006
PL/SQL
28
Example
declare cnum customers.cno%type; ccname customers.cname%type; cphone customers.phone%type; ccity zipcodes.city%type; status boolean; procedure get_cust_details(cust_no in customers.cno%type, cust_name out customers.cname%type, cust_phone out customers.phone%type, cust_city out zipcodes.city%type, status out boolean) is begin select cname,phone,city into cust_name,cust_phone,cust_city from customers,zipcodes where customers.zip = zipcodes.zip and customers.cno = cust_no; status := true; exception when no_data_found then status := false; end; //to be continued
Example (cont.)
begin cnum := 1111; get_cust_details(cnum,ccname,cphone,ccity,status); if (status) then DBMS_OUTPUT.PUT_LINE(cnum || ' ' || ccname || ' ' || cphone || ' ' || ccity); else DBMS_OUTPUT.PUT_LINE('Customer ' || cnum || ' not found'); end if; cnum := 5555; get_cust_details(cnum,ccname,cphone,ccity,status); if (status) then DBMS_OUTPUT.PUT_LINE(cnum || ' ' || ccname || ' ' || cphone || ' ' || ccity); else DBMS_OUTPUT.PUT_LINE('Customer ' || cnum || ' not found'); end if; end; /
3/16/2006 PL/SQL 30
3/16/2006
PL/SQL
29
3/16/2006
Example
declare enum customers.cno%type; total number(10,2); status boolean; function total_emp_sales( emp_no in employees.eno%type) return number is sales number; begin select sum(price*qty) into sales from orders,odetails,parts where orders.eno = emp_no and orders.ono = odetails.ono and odetails.pno = parts.pno; return (sales); end; begin enum := 1000; total := total_emp_sales(enum); DBMS_OUTPUT.PUT_LINE('Total sales for employee ' || enum || ' is ' || total); end; /
3/16/2006 PL/SQL 32
Execute a Block
Assume an anonymous block, procedure, or a function is saved in a file file-name.sql In SQL*PLUS, it can be called by the syntax SQL> start file-name In SQL*PLUS Worksheet, on the Worksheet menu, click run local script
3/16/2006
PL/SQL
33
3/16/2006
PL/SQL
34
Stored Procedures
The syntax for stored procedures is create [or replace] procedure <proc-name> [(<parameter-list>)] as <declarations> begin <executable-section> [exception <exception-section>] end;
Stored Functions
The syntax for stored function is create [or replace] function <func-name> [(<parameter-list>)] return <datatype> as <declarations> begin <executable-section> [exception <exception-section>] end;
3/16/2006
PL/SQL
35
3/16/2006
PL/SQL
36
3/16/2006
Example
create or replace function get_city(cnum in customers.cno%type) return zipcodes.city%type as ccity zipcodes.city%type; begin select into from where city ccity customers,zipcodes cno = cnum and customers.zip = zipcodes.zip; return (ccity); end; /
In-class Assignement
Assignment 6
The following is a call made to the stored function get_city in an SQL query. SQL> select cno, cname, get_city(cno) 2 from customers;
3/16/2006
PL/SQL
37
3/16/2006
PL/SQL
38
Packages
A package is a group of related PL/SQL objects (variables, constants, types, and cursors), procedures, and functions that is stored in the DB. A package definition contains of two parts: the package specification and the package body.
Packages (cont.)
The syntax for creating a package specification is create [or delete] package <package-name> as <PL/SQL declarations>; end;
<PL/SQL declarations> is any set of variable, type, constant, procedure, or function declarations The procedure and function declarations consist only of the name, parameter list, and function return type. They dont contain the actual code since this has to be specified in the package body.
3/16/2006
PL/SQL
39
3/16/2006
PL/SQL
40
Packages (cont.)
The syntax for creating a package body is create [or delete] package body <package-name> as <PL/SQL declarations>; [begin <initialization-code> end;] end;
The <PL/SQL declarations> now contain the code for the functions and procedures defined in the specification. At the end of the body, you can specify an initialization code fragment that initializes variables and other features of the package.
3/16/2006 PL/SQL 41
3/16/2006
PL/SQL
42
3/16/2006
3/16/2006
PL/SQL
44
Exercise
Execute the following commnads agains the mail-order database in SQL*Plus.
SQL> execute process_orders.add_order(2000, 111, 1000, null); SQL> execute process_orders.add_order_details (2000, 10509, 50); SQL> execute process_orders.add_order_details (2000, 10701, 200); SQL> execute process_orders.ship_order(2000,29-JAN-98); SQL> execute process_orders.add_order(2001,1234,1000,null); SQL> execute process_orders.add_order_details (2000, 10999, 5);
3/16/2006
PL/SQL
46