Presentation is loading. Please wait.

Presentation is loading. Please wait.

PL/SQL Continued. So far… Anonymous blocks Procedures Have you tried… –Downloading and running the dispcust.sql sample from your web page? –Writing your.

Similar presentations


Presentation on theme: "PL/SQL Continued. So far… Anonymous blocks Procedures Have you tried… –Downloading and running the dispcust.sql sample from your web page? –Writing your."— Presentation transcript:

1 PL/SQL Continued

2 So far… Anonymous blocks Procedures Have you tried… –Downloading and running the dispcust.sql sample from your web page? –Writing your own anonymous block with embedded procedure?

3 Today… Store procedures and functions for independent use against the schema. Combine procedures and functions to give packages. Look at how errors can be: –Handled –Trapped and passed back to the calling environment.

4 Storing procedures Look ahead at the next slide. –It shows the procedure that we used in the sample anonymous block. –It is a local procedure, that ceases to exist when the program stops running. –Often we need to reuse functionality. –The procedure can be defined as part of the schema. See the slide after next.

5 The procedure in dispcust procedure get_cust_details ( cust_noinbuilder.customer.customer_id%type, cust_nameoutbuilder.customer.customer_name%type, cust_addroutbuilder.customer.customer_address%type, statusoutboolean) is begin status := true; select builder.customer.customer_name, builder.customer.customer_address into cust_name, cust_addr from builder.customer where builder.customer.customer_id = cust_no; exception when no_data_found then dbms_output.put_line('ERROR'); status := false; end;

6 … and independently… Create or replace procedure get_cust_details ( cust_noinbuilder.customer.customer_id%type, cust_nameoutbuilder.customer.customer_name%type, cust_addroutbuilder.customer.customer_address%type, statusoutboolean) as begin status := true; select builder.customer.customer_name, builder.customer.customer_address into cust_name, cust_addr from builder.customer where builder.customer.customer_id = cust_no; exception when no_data_found then dbms_output.put_line('ERROR'); status := false; end;

7 Functions for independent use create or replace Function total_emp_sales( emp_no in staff.staff_no%type) Return number as sales number; Begin select sum(unit_price * quantityrequired) into sales from staff join corder on staff.staff_no = corder.staffpaid join corderline on corder.corderno = corderline.corderno join stock on corderline.stock_code = stock.stock_code where staff.staff_no = emp_no; return (sales); End;

8 To call this function: sql>select staff_no, total_emp_sales(staff_no) from staff;

9 Packages Often it makes sense to keep functions and procedures that relate to the same functionality or group of tables together. This grouping can be known as a package. –It is somewhat similar to a package of classes. An example would be the set of steps required to process orders.

10 Processing orders… Set up a customer order –Does the customer exist in the system already? Should we add the customer? –What will the order number be? Add a detail line to the customer order –Have we enough stock? What’ll we do if we don’t? –Did the stock fall below the reorder level? What’ll we do if it did? Issue the stock for the order –What if there’s an error?

11 Before we begin: Decide on: –A logging strategy –An error handling strategy for Expected errors Unexpected errors –Commit and rollback strategy –Maintenance of sequences strategy

12 Answers… Set up a customer order –Does the customer exist in the system already? Should we add the customer? –What will the order number be? Answers: –Provide a facility for the user to look up the customer by name and address. –Provide a facility to add a customer. –Use a SEQUENCE to create a unique order number.

13 …Answers… Add a detail line to the customer order –Have we enough stock? What’ll we do if we don’t? –Did the stock fall below the reorder level? What’ll we do if it did? Answers –Cancel the detail line, log the problem and return an error if we don’t have enough stock –Log the fact that the reorder quantity has been reached.

14 Logging errors Some of the logging will be used when the supplier orders are being set up, so we can formalise this procedure: Set up logging tables@

15 The ‘add order’ procedure Take in the customer number, the employee number and the order date. Take the next sequential order number. Add a new order, leaving the ‘staff issued’ field as null. Inform the operator of any errors and also log them in a corder_error table, which has 3 columns: –Transaction date –Order number –Message.

16 Add corderline procedure Take in the order number, the stock code and the quantityrequired. If stock_level – quantityrequired > reorder_level then –Update stock level to stock_level - quantityrequired –Add new corderline with corderno, stockcode, quantityrequired Elseif stock_level – quantityrequired quantityrequired –Log the reorder requirement –Update stock_level to stock_level - quantityrequired –Add new corderline with corderno, stockcode, quantityrequired else –Log the error –Control Failure out of the procedure End Introduce a corderline_error table which has 4 columns: –Transaction date –Corderno –Stock code –Message

17 Sequences Sequences: –Fields that are set up to autoincrement every time they are used: create sequence custseq increment by 1 start with 100 maxvalue 20000 cycle; –To increment the sequence: Select custseq.nextval from dual; Or Customer_id = custseq.nextval;

18 Commit and Rollback Commit confirms work done on the database Rollback undoes work done since last commit, exit (PL/SQL), table drop, create or alter. It is wise to commit work at logical breakpoints – at the end of a sequence of transactions. –In this case, the commits should happen when all of the order lines had been put in against an order and the customer had paid. When the order was updated as issued.

19 Combine procedures and functions to give packages. See accompanying handout and sidebar link.

20 To create the package… SQL> @u:\oracle\build\procorder.sql; Package created. No errors. Package body created. No errors.

21 To run a procedure from the package: SQL> exec custorders.add_corderline(108,'B111',20) PL/SQL procedure successfully completed. Note: there were not enough ‘B111’s to fill this orderline – see next slide.

22 SQL> select * from corderline; QUANTITYREQUIRED CORDERNO STOCK ---------------- ---------- ----- 20 101 A101 20 106 A111 47 103 B111 1 103 C101 3 103 C121 1 105 D101 2 107 D131 2 103 E101 5 108 E101 40 103 D101 14 108 B111 QUANTITYREQUIRED CORDERNO STOCK ---------------- ---------- ----- 200 105 E101 12 rows selected.

23 What about the error table? SQL> select * from corderline_error; ERRORDATE ONUM SCODE ERRMSG --------- ---------- ----- ---------------------------------------- 04-NOV-05 108 B111 Do not have enough to sell SQL>


Download ppt "PL/SQL Continued. So far… Anonymous blocks Procedures Have you tried… –Downloading and running the dispcust.sql sample from your web page? –Writing your."

Similar presentations


Ads by Google