Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions."— Presentation transcript:

1 SQL Training Procedures & Functions

2 Confidential & Proprietary Copyright © 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.

3 Procedures

4 Confidential & Proprietary Copyright © 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; /

5 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Using a DB Procedure 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;

6 Confidential & Proprietary Copyright © 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

7 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. PL/SQL Procedures – A Definition The 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;

8 Confidential & Proprietary Copyright © 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. BooleanCan be assigned the constants True or False. Binary_IntegerIntegers in the range –2,147,483,647 to 2,147,483,647. NaturalIntegers from 0 to 2,147,483,647. PositiveIntegers from 1 to 2,147,483,647. %TypeA 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_addressDistributor.Address1%Type; begin My_Address := 'Unknown'; Insert into Distributor(DistributorID, DistributorName, Address1) values (in_distributorid, in_name, my_address); end; / PL/SQL – Datatypes

9 Confidential & Proprietary Copyright © 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 Tab 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;

10 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. PL/SQL – If /Then / Else Syntax IF condition THEN statement; … statement; [ELSIF condition THEN statement; … statement;] [ELSIF condition THEN statement; … statement;] [ELSE statement; … statement;] END IF; Example: IF My_price < 5000 THEN My_price := My_price * 1; ELSE My_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.

11 Confidential & Proprietary Copyright © 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_Number1Number; v_Number2Number, v_ResultVarchar2(5); begin; … IF v_Number1 IS NULL or v_Number2 IS NULL then v_Result := ‘Unknown’; ELSIF v_Number1 < v_Number2 then v_Result := ‘Yes”; ELSE v_Result := ‘No’; END IF; END;

12 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. PL/SQL – Loop / End Loop Syntax LOOP statement; … statement; END LOOP; Example: create or replace procedure LoopSample as My_CountPositive := 1; Max_Loops constant positive :=100; begin LOOP My_Count := My_Count + 1; EXIT WHEN My_Count > Max_Loops; END LOOP; end; /

13 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. PL/SQL – While / Loop Syntax WHILE condition LOOP statement; … statement; END LOOP; Example: create or replace procedure WhileLoopSample as MyUser_ID Positive := 1; MyUser_Nm VarChar2(30); begin WHILE 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; /

14 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. PL/SQL – For / Loop Syntax FOR loop-variable IN [REVERSE] lower..upper LOOP statement; … statement; END LOOP; Example: create or replace procedure ForLoopSample as MyUser_Nm VarChar2(30); Max_Loops Positive := 100; begin FOR 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; /

15 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. PL/SQL – GOTO Statement GOTO Label; Example: cur_value positive := 1; max_loops positive := 10; begin loop if cur_value > max_loops then GOTO blast_off; else cur_value := cur_value + 1; end if; end loop; > …. end; /

16 Confidential & Proprietary Copyright © 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-list INTO variable-list FROM table-reference WHERE where-clause GROUP BY group-by-clause ORDER BYorder-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 orders where orderid = in_orderid;

17 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. PL/SQL – Cursor Processing A 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 recordset OPEN the recordset FETCH a row and move through the recordset CLOSE the recordset SQL Statement BASE TABLE CURSOR RecordSet

18 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. PL/SQL – Cursor Processing Example Create or Replace Procedure unconfirmedOrders as my_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

19 Confidential & Proprietary Copyright © 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.

20 Confidential & Proprietary Copyright © 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;

21 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. PL/SQL – SQL Syntax (Delete) DELETE The form of a DELETE statement is as follows: DELETE FROM table-reference WHERE [where-clause] [CURRENT OF cursor-name]; Note: The Where Current of cursor-name option allows you to delete the record that is 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;

22 Functions

23 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. PL/SQL Functions – A Definition A 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.

24 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. PL/SQL Function Example Create or Replace Function ShippingCharge(in_orderid IN Integer) return Integer is ShippingCharge integer; begin SELECT Sum(Weight*OrderQty*CarrierMileageRate*Mileage)+ (FixedCostAmount+DeliveryChargeRate) AS ShippingCost into ShippingCharge FROM 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 ShippingCharge from Orders Where OrderID = 1001;

25 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. PL/SQL – Handling Exceptions Predefined Exceptions: DUP_VAL_ON_INDEXDuplicate value for index column. INVALID_NUMBERSQL statement specifies an invalid number. NO_DATA_FOUNDSelect statement doesn’t return any rows. TOO_MANY_ROWSA Select statement has retrieved more than 1 row. VALUE_ERRORTruncation or conversion error. User-Defined Exceptions: Salary_Too_Low exception; MySalary NUMBER(10,2); begin Select Salary from Person Into MySalary Where person = in_person; If MySalary < 50000 then RAISE Salary_Too_Low; End If; return (‘Salary OK’); exception When Salary_Too_Low then return (‘Salary too low’); end;

26 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Exception Handling Example Create or Replace Function ShippingCharge(in_orderid IN Integer) return Integer is ShippingCharge integer; begin SELECT Sum(Weight*OrderQty*CarrierMileageRate*Mileage)+ (FixedCostAmount+DeliveryChargeRate) AS ShippingCost into ShippingCharge FROM 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; 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.

27 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Showing Errors Create 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) 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; / show errors

28 Confidential & Proprietary Copyright © 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;

29 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Test & Use the Function Create or Replace Function ShippingCharge(in_orderid IN Integer) return Integer is 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;

30 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Test & Use the Function

31 Workshop

32 Confidential & Proprietary Copyright © 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.


Download ppt "SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions."

Similar presentations


Ads by Google