Presentation is loading. Please wait.

Presentation is loading. Please wait.

Oracle10g Developer: PL/SQL Programming1 Objectives SQL queries within PL/SQL Host or bind variables The %TYPE attribute Include queries and control structures.

Similar presentations


Presentation on theme: "Oracle10g Developer: PL/SQL Programming1 Objectives SQL queries within PL/SQL Host or bind variables The %TYPE attribute Include queries and control structures."— Presentation transcript:

1 Oracle10g Developer: PL/SQL Programming1 Objectives SQL queries within PL/SQL Host or bind variables The %TYPE attribute Include queries and control structures Embed DML statements within PL/SQL Composite data types Creating collections

2 Oracle10g Developer: PL/SQL Programming2 Brewbean’s Challenge Consider actions needed upon check out

3 Oracle10g Developer: PL/SQL Programming3 Include SQL within a Block Data query needs to identify if the customer has a saved basket

4 Oracle10g Developer: PL/SQL Programming4 SQL statements can be embedded into the executable area of a PL/SQL block SELECT statements are embedded to query needed data An INTO clause is added to a SELECT statement to move data retrieved into variables Include SQL within a Block

5 Oracle10g Developer: PL/SQL Programming5 SQL Query – add INTO clause Assignment Statement Include SQL within a Block

6 Oracle10g Developer: PL/SQL Programming6 Executing a Block with Errors Common Errors – Use = rather than := – Not declaring a variable – Misspelling a variable name – Not ending a statement with ; – No data returned from a SELECT statement

7 Oracle10g Developer: PL/SQL Programming7 Not closing a statement with ; Executing a Block with Errors

8 Oracle10g Developer: PL/SQL Programming8 Host or Bind Variables Reference host variables with a preceding colon in PL/SQL BEGIN :g_shopper := 25; END; / WHERE idShopper = :g_shopper AND orderplaced = 0; Create host variable Reference host variable

9 Oracle10g Developer: PL/SQL Programming9 %TYPE Attribute Use in variable declaration to provide data type based on a table column Ideal for declaring variables that will hold data from the database Minimizes maintenance by avoiding program changes to reflect database column changes Called an anchored data type lv_basket_num bb_basket.idBasket%TYPE;

10 Oracle10g Developer: PL/SQL Programming10 Data Retrieval & Decisions

11 Oracle10g Developer: PL/SQL Programming11 IF Statement Example

12 Oracle10g Developer: PL/SQL Programming12 Including DML DML statements can be embedded into PL/SQL blocks to accomplish data changes DML includes INSERT, UPDATE, and DELETE statements

13 Oracle10g Developer: PL/SQL Programming13 Including DML Add a new shopper - INSERT

14 Oracle10g Developer: PL/SQL Programming14 Composite Data Types Stores multiple values of different data types as one unit Record – can hold one row of data Table of records – can hold multiple rows of data

15 Oracle10g Developer: PL/SQL Programming15 Record Data Type DECLARE TYPE type_basket IS RECORD ( basket bb_basket.idBasket%TYPE, created bb_basket.dtcreated%TYPE, qty bb_basket.quantity%TYPE, sub bb_basket.subtotal%TYPE); rec_basket type_basket; lv_days_num NUMBER(3); lv_shopper_num NUMBER(3) := 25; BEGIN SELECT idBasket, dtcreated, quantity, subtotal INTO rec_basket FROM bb_basket WHERE idShopper = lv_shopper_num AND orderplaced = 0; lv_days_num := SYSDATE - rec_basket.created; END;

16 Oracle10g Developer: PL/SQL Programming16 %ROWTYPE Attribute Create record structure based on table structure DECLARE rec_shopper bb_shopper%ROWTYPE; BEGIN SELECT * INTO rec_shopper FROM bb_shopper WHERE idshopper = :g_shopper; DBMS_OUTPUT.PUT_LINE(rec_shopper.lastname); DBMS_OUTPUT.PUT_LINE(rec_shopper.address); DBMS_OUTPUT.PUT_LINE(rec_shopper.email); END;

17 Oracle10g Developer: PL/SQL Programming17 Table of Records

18 Oracle10g Developer: PL/SQL Programming18 Collections Store multiple values of the same data type Similar to arrays in other languages Index-by Tables – handle many rows of one field One- dimensional Can only have one column UnconstrainedRows added dynamically as needed SparseA row only exists when a value is assigned; rows do not have to be assigned sequentially HomogeneousAll elements have same data type IndexedInteger index serves as primary key of the table

19 Oracle10g Developer: PL/SQL Programming19 Index-by Table Attributes Attribute Name Value Data typeDescription COUNTNUMBERNumber of rows in the table DELET E NoneRemoves a row from the table EXISTSBOOLEANTRUE if specified row does exist FIRSTBINARY_INTEG ER Index for the first row in the table LASTBINARY_INTEG ER Index for the last row in the table NEXTBINARY_INTEG ER Index for the next row in the table after the row specified PRIORBINARY_INTEG ER Index for the row in the table before the row specified

20 Oracle10g Developer: PL/SQL Programming20 Index-by Table Example Host variables declaration and initialization

21 Oracle10g Developer: PL/SQL Programming21 Index-by Table Example Put host variable values into the table variable A FOR loop adds all the sample measurements that have been entered into the table variable lv_avg_num calculates the Average measurement Index-by table data type declaration Index-by table variable declaration

22 Oracle10g Developer: PL/SQL Programming22 Summary SQL queries and DML statements can be embedded into a block An INTO clause must be added to a SELECT The %TYPE attribute is used to use a column data type Composite data types can hold multiple values in a single variable

23 Oracle10g Developer: PL/SQL Programming23 Summary A record can hold a row of data A table of records can hold multiple rows of data The %ROWTYPE attribute can be used to declare a data type based on a table’s structure An index-by table is a collection similar to arrays The GOTO statement enables execution to jump to specific portions of code


Download ppt "Oracle10g Developer: PL/SQL Programming1 Objectives SQL queries within PL/SQL Host or bind variables The %TYPE attribute Include queries and control structures."

Similar presentations


Ads by Google