sql training procedures & functions. confidential & proprietary copyright © 2009 cardinal...

32
SQL Training Procedures & Functions

Upload: blake-malone

Post on 26-Dec-2015

218 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

SQL Training

Procedures & Functions

Page 2: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

DB Procedures & Functions

Procedures and Functions are pre-defined pieces of code that perform database work (like a view).

Procedures are typically used to perform and action - insert, update and delete statements. They do not return data.

Functions run select statements and return a single* value.

Functions can be run from a select statement. Procedures cannot.

* Functions can be modified to return multiple values but then cannot be used in a select statement which is how they provide their value.

Page 3: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

Procedures

Page 4: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

PL/SQL –DB Procedure Sample

Create or Replace Procedure DistributorUpdate (in_DistributorID IN Integer,in_Distributorname IN VarChar2,in_DistributorFirstName IN VarChar2, in_DistributorLastName IN VarChar2, in_Phone IN VarChar2,in_Fax IN VarChar2, in_City IN VarChar2, in_PostalCode IN VarChar2, in_DiscountRate IN Number) as begin Update distributor

Set distributorname = in_DistributorName, distributorfirstname = in_DistributorFirstName, distributorlastname = in_DistributorLastName, phone = in_Phone, fax = in_Fax, city = in_City, postalcode = in_PostalCode, discountrate = in_DiscountRate

where distributorid = in_DistributorID;Commit; end; /

Page 5: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

Using a DB ProcedureCreate or Replace Procedure UpdateAddress (in_distributorid In Integer, in_street IN VarChar2, in_city IN VarChar2, in_statecode IN VarChar2, in_zip IN VarChar2, in_phone IN VarChar2, in_fax IN VarChar2) asbegin Update Distributor Set address1 = in_street, city = in_city, provinceabbreviation = in_statecode, postalcode = in_zip, phone = in_phone, fax = in_fax Where distributorid = in_distributorid; Commit;end;

Create or Replace Procedure UpdateAddress (in_distributorid In Integer, in_street IN VarChar2, in_city IN VarChar2, in_statecode IN VarChar2, in_zip IN VarChar2, in_phone IN VarChar2, in_fax IN VarChar2) asbegin Update Distributor Set address1 = in_street, city = in_city, provinceabbreviation = in_statecode, postalcode = in_zip, phone = in_phone, fax = in_fax Where distributorid = in_distributorid; Commit;end;

Page 6: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

PL/SQL – A Definition

Declaration Section

Executable Section

Exception Section

PL/SQL is an Oracle language that extends the capabilities of SQL.

A PL/SQL procedure contains three sections: Declaration, Executable, and Exception. In PL/SQL you can develop logic that can be executed in:  SQL*Plus A Stored Procedure A Stored Function Database Trigger Package End;

Variables and Cursors

Procedural & SQL statements

Error handling statements

Create or Replace Procedure name AS

Page 7: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

PL/SQL Procedures – A DefinitionThe syntax for the Create Procedure command is as follows:

Create Procedure Syntax:

create [or replace] procedure procedurename[ (argument IN | OUT | IN OUT datatype) ] … as

variable datatype; …begin

……

exception ……

end;

Page 8: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

In addition to the normal Oracle SQL datatypes, PL/SQL allows you to declare variables with these datatypes. The maximum length of a variable name is 30 characters. 

Boolean Can be assigned the constants True or False.Binary_Integer Integers in the range –2,147,483,647 to 2,147,483,647.Natural Integers from 0 to 2,147,483,647.Positive Integers from 1 to 2,147,483,647.%Type A Table.Column’s datatype. 

You must declare all variables and constants that are referenced in the PL/SQL statements. SQL Cursors must also be declared. 

Example:

Create or Replace Procedure AddDistributor (in_distributorid IN Integer, in_name IN String) as

my_address Distributor.Address1%Type;begin

My_Address := 'Unknown';Insert into Distributor(DistributorID, DistributorName, Address1)

values (in_distributorid, in_name, my_address);end;/

PL/SQL – Datatypes

Page 9: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

PL/SQL – Operators & Delimiters+ Addition Operator- Subtraction Operator* Multiplication Operator/ Division Operator= Equality Operator< Less than Operator> Greater than Operator; Statement terminator' Character String Delimiter" Quoted String Delimiter<> Not Equal To Operator!= Same as <><= Less Than Or Equal To Operator>= Greater Than or Equal To Operator:= Assignment Operator|| Concatenation Operator-- Single Line Comment/* */ Multiple Line Comments<< >> Label Delimiters<space> Space<tab> Tab<cr> Carriage return

MyUser_Nm := 'Temp' || MyUser_ID;My_Address := 'Unknown';

My_discount := My_discount * 0.75;

Examples:

If MySalary < 50000 then RAISE Salary_Too_Low;End If;

Page 10: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

PL/SQL – If /Then / Else Syntax

IF condition THENstatement; … statement;

[ELSIF condition THENstatement; … statement;]

[ELSIF condition THENstatement; … statement;]

[ELSE statement; … statement;]

END IF; 

Example:

IF My_price < 5000 THENMy_price := My_price * 1;

ELSEMy_price := My_price * 0.75;

END IF;• 

Special Notes: The ELSIF and ELSE clauses are optional. An IF statement can have multiple ELSIF clauses but only one ELSE clause. ELSIF is valid. ELSEIF is invalid.

Page 11: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

PL/SQL – IS Null Condition

The IS NULL condition will evaluate to TRUE only if the variable it is checking is NULL.If the variable IS NOT NULL, the condition will evaluate to FALSE.

v_Number1 Number;v_Number2 Number,v_Result Varchar2(5);begin;…IF v_Number1 IS NULL or v_Number2 IS NULL then

v_Result := ‘Unknown’;

ELSIF v_Number1 < v_Number2 thenv_Result := ‘Yes”;

ELSEv_Result := ‘No’;

END IF;END;

Page 12: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

PL/SQL – Loop / End Loop Syntax

LOOP statement; … statement;END LOOP; 

Example:create or replace procedure LoopSample asMy_Count Positive := 1;Max_Loops constant positive :=100;beginLOOP

My_Count := My_Count + 1;EXIT WHEN My_Count > Max_Loops;

END LOOP;end;/

Page 13: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

PL/SQL – While / Loop Syntax

WHILE condition LOOPstatement; … statement;

END LOOP; 

Example: 

create or replace procedure WhileLoopSample asMyUser_ID Positive := 1;MyUser_Nm VarChar2(30);beginWHILE MyUser_ID < 101 LOOP MyUser_Nm := ‘Temp’ || MyUser_ID; insert into Users (UserID, UserName, RoleID, Password) values (MyUser_ID, MyUser_Nm,5,'ChangeMe');

MyUser_ID := MyUserID + 1;END LOOP;end; /

Page 14: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

PL/SQL – For / Loop Syntax

FOR loop-variable IN [REVERSE] lower..upper LOOPstatement; … statement;

END LOOP; 

Example: 

create or replace procedure ForLoopSample asMyUser_Nm VarChar2(30);Max_Loops Positive := 100;beginFOR MyUser_ID IN 1..Max_Loops LOOP MyUser_Nm := 'Temp' || MyUser_ID; INSERT INTO Users (UserID, UserName, RoleID, Password)

VALUES (MyUser_ID, MyUser_Nm,5,'ChangeMe');END LOOP;end; /

Page 15: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

PL/SQL – GOTO Statement

GOTO Label;

Example:cur_value positive := 1;max_loops positive := 10;beginloop

if cur_value > max_loops thenGOTO blast_off;

elsecur_value := cur_value + 1;

end if; end loop;<<blast_off>>

….end;/

Page 16: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

PL/SQL – SQL Syntax (Select)

SELECT

A SELECT statement retrieves data from the database into PL/SQL variables. The form of a SELECT statement is as follows:

SELECT select-listINTO variable-listFROM table-referenceWHERE where-clauseGROUP BY group-by-clauseORDER BY order-by-clause;

Note: This form of the SELECT statement should return not more than one row. If more than one row must be return, you must use a CURSOR to retrieve each row individually.

Example:select orderdate into in_orderdate from orderswhere orderid = in_orderid;

Page 17: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

PL/SQL – Cursor ProcessingA Cursor is used to process multiple rows retrieved from the database. Using a cursor, your program can step through the set of returned rows one at a time, processing each row in turn.

Cursor Functions:

DECLARE the recordsetOPEN the recordsetFETCH a row and move through the recordsetCLOSE the recordset

SQL Statement

BASE TABLE

BASE TABLE

CURSORRecordSet

Page 18: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

PL/SQL – Cursor Processing Example

Create or Replace Procedure unconfirmedOrders asmy_orderid Orders.OrderID%type;cursor my_cursor is select orderID from orders where orderstatuscode = 'O';begin

-- Delete all records in the WIP table-- Then Open the cursor.-- Insert all cursor rows into the WIP Table

delete from WIP;open my_cursor; loop fetch my_cursor into my_orderid; exit when my_cursor%notfound; insert into WIP(OrderID) values (my_orderid);end loop;commit;close my_cursor;end;/

Use a Procedure with a Cursor to save all work in progress (or unshipped orders). Note: WIP Work in Progress

Page 19: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

PL/SQL – SQL Syntax (Insert)INSERT

The form of a INSERT statement is as follows:

INSERT INTO table-reference ( column-name, column-name…)VALUES (expression, expression...);

Example:Insert Into distributor (distributorid, distributorname, distributorfirstname, distributorlastname) Values (in_distributorid, in_distributorname, in_firstname, in_lastname);

Note: Since all character fields are declared as VarChar2 fields, there is no need to enclose them in quotes.

Page 20: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

PL/SQL – SQL Syntax (Update)

UPDATE

The form of a UPDATE statement is as follows:

Update table-reference SET column-name = expression, …

WHERE where-clause;

Example:

Update Distributor Set address1 = in_street,city = in_city,provinceabbreviation = in_state,postalcode = in_zip

Where distributorID = in_distributorid;

Page 21: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

PL/SQL – SQL Syntax (Delete)

DELETE

The form of a DELETE statement is as follows:

DELETE FROM table-referenceWHERE [where-clause]

[CURRENT OF cursor-name];

Note: The Where Current of cursor-name option allows you to delete the record thatis being pointed to by the Cursor. For this to work, the Cursor must be declared as an updateable Cursor. See Cursor Processing.

Example:

Delete from Orders Where OrderID = in_orderid;

Page 22: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

Functions

Page 23: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

PL/SQL Functions – A DefinitionA Function can return a value to the caller.

The value is returned to the caller through the use of a RETURN keyword within the function.

Functions can be referenced directly in SQL Queries.

Create Function Syntax:

create [or replace] function functionname[ (argument IN | OUT | IN OUT datatype) ] …

return datatype is argument datatype;begin

…return (argument);

exception …

end;

Note: Every Function must have a return clause.

Page 24: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

PL/SQL Function ExampleCreate or Replace Function ShippingCharge(in_orderid IN Integer) return Integer is

ShippingCharge integer;

beginSELECT Sum(Weight*OrderQty*CarrierMileageRate*Mileage)+

(FixedCostAmount+ DeliveryChargeRate) AS ShippingCost into ShippingChargeFROM Delivery, CARRIER, Orders, OrderDetail,

Distributor, Product, Distance, v_weight WHERE Delivery.DeliveryCode = Orders.DeliveryCode AND

CARRIER.CarrierID = Orders.CarrierID AND Orders.OrderID = OrderDetail.OrderID AND Orders.DistributorID = Distributor.DistributorID AND Distributor.WarehouseID = Distance.WarehouseID AND Orders.PRovinceID = Distance.ProvinceID AND OrderDetail.ProductID = Product.ProductID AND Product.ProductCode = v_weight.Model AND Orders.OrderID =

in_orderid

Group by FixedCostAmount,DeliveryChargeRate;return ShippingCharge;

end;Select OrderID, ShippingCharge(Orderid) as ShippingChargefrom Orders Where OrderID = 1001;

Page 25: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

PL/SQL – Handling ExceptionsPredefined Exceptions: 

DUP_VAL_ON_INDEX Duplicate value for index column. INVALID_NUMBER SQL statement specifies an invalid number.  NO_DATA_FOUND Select statement doesn’t return any rows. TOO_MANY_ROWS A Select statement has retrieved more than 1 row. VALUE_ERROR Truncation or conversion error. 

User-Defined Exceptions: 

Salary_Too_Low exception; MySalary NUMBER(10,2); begin

Select Salary from Person Into MySalaryWhere person = in_person;If MySalary < 50000 then

RAISE Salary_Too_Low;End If;

return (‘Salary OK’);  exception

When Salary_Too_Low thenreturn (‘Salary too low’);

end;

Page 26: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

Exception Handling ExampleCreate or Replace Function ShippingCharge(in_orderid IN Integer) return Integer

is ShippingCharge integer;

beginSELECT Sum(Weight*OrderQty*CarrierMileageRate*Mileage)+

(FixedCostAmount+ DeliveryChargeRate) AS ShippingCost into ShippingChargeFROM Delivery, CARRIER, Orders, OrderDetail,

Distributor, Product, Distance, v_weight WHERE Delivery.DeliveryCode = Orders.DeliveryCode AND

CARRIER.CarrierID = Orders.CarrierID AND Orders.OrderID = OrderDetail.OrderID AND Orders.DistributorID = Distributor.DistributorID AND Distributor.WarehouseID = Distance.WarehouseID AND Orders.PRovinceID = Distance.ProvinceID AND OrderDetail.ProductID = Product.ProductID AND Product.ProductCode = v_weight.Model AND Orders.OrderID =

in_orderidGroup by FixedCostAmount,DeliveryChargeRate;return ShippingCharge;

exception

when NO_DATA_FOUND then

return(0);

end;

Note: This code uses a view named v_weight. This view is supplied in the _CreateViews.sql file within the student’s starter database folder.

Page 27: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

Showing ErrorsCreate or Replace Procedure UpdateAddress (in_distributorid In Integer, in_street IN VarChar2, in_city IN VarChar2, in_statecode IN VarChar2, in_zip IN VarCharxxx, in_phone IN VarChar2, in_fax IN VarChar2) asbegin Update Distributor Set address1 = in_street, city = in_city, provinceabbreviation = in_statecode, postalcode = in_zip, phone = in_phone, fax = in_fax Where distributorid = in_distributorid; Commit;end;/show errors

Page 28: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

Test Procedures•Create or Replace Procedure UpdateAddress (in_distributorid In Integer, in_street IN VarChar2, in_city IN VarChar2, in_statecode IN VarChar2, in_zip IN VarChar2, in_phone IN VarChar2, in_fax IN VarChar2) as•begin• Update Distributor Set• address1 = in_street,• city = in_city,• provinceabbreviation = in_statecode,• postalcode = in_zip,• phone = in_phone,• fax = in_fax• Where distributorid = in_distributorid;• Commit;•end;

•Create or Replace Procedure UpdateAddress (in_distributorid In Integer, in_street IN VarChar2, in_city IN VarChar2, in_statecode IN VarChar2, in_zip IN VarChar2, in_phone IN VarChar2, in_fax IN VarChar2) as•begin• Update Distributor Set• address1 = in_street,• city = in_city,• provinceabbreviation = in_statecode,• postalcode = in_zip,• phone = in_phone,• fax = in_fax• Where distributorid = in_distributorid;• Commit;•end;

Page 29: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

Test & Use the FunctionCreate or Replace Function ShippingCharge(in_orderid IN Integer) return Integeris ShippingCharge integer;begin SELECT Sum(20*OrderQty*CarrierMileageRate*Mileage) +(FixedCostAmount+ DeliveryChargeRate) AS ShippingCost into ShippingCharge FROM Delivery, CARRIER, Orders, OrderDetail, Distributor, Product, distance WHERE Delivery.DeliveryCode = Orders.DeliveryCode AND CARRIER.CarrierID = Orders.CarrierID AND Orders.OrderID = OrderDetail.OrderID AND Orders.DistributorID = Distributor.DistributorID AND Distributor.WarehouseID = Distance.WarehouseID AND Orders.PRovinceID = Distance.ProvinceID AND OrderDetail.ProductID = Product.ProductID AND Orders.OrderID = in_orderid GROUP BY FixedCostAmount, DeliveryChargeRate; return ShippingCharge;exception when NO_DATA_FOUND then return(0);end;/show errors;

Page 30: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

Test & Use the Function

Page 31: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

Workshop

Page 32: SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

Exercise: Creating Procedures

1. Create a procedure to add an orderdetail line.• Use any existing orderid• Populate all orderdetail columns

2. Create an orderdetail update procedure• Change the order quantity

3. Create a function to return the line amount (productprice * orderqty) for any order detail line.

4. Create a function to return the total order amount for any order.