Oracle Programming Using PL/SQL: Level 1

5 downloads 415 Views 77KB Size Report
Feb 6, 2009 ... Understanding the Benefits of PL/SQL . ... Constructing Anonymous PL/SQL Blocks. ... Exercise 1: Working with PL/SQL .
Oracle Programming Using PL/SQL: Level 1 What You Will Learn...................................................................................................................... 1 Working with PL/SQL.................................................................................................................... 2 Understanding the Benefits of PL/SQL .................................................................................... 2 Storing Your PL/SQL Code...................................................................................................... 3 Constructing Anonymous PL/SQL Blocks............................................................................... 3 Executing Anonymous Blocks.................................................................................................. 5 Exercise 1: Working with PL/SQL ................................................................................................. 7 Declaring Variables ........................................................................................................................ 8 Declaring Scalar Variables in PL/SQL ..................................................................................... 8 Initializing Variables............................................................................................................... 10 Declaring Constants ................................................................................................................ 13 Using Variables in Executable Statements ............................................................................. 13 Using Substitution Variables .................................................................................................. 16 Anchoring Scalar Variables .................................................................................................... 17 Understanding PL/SQL Programming Conventions .................................................................... 18 Commenting Your Code ......................................................................................................... 19 Exercise 2: Writing Simple Programs........................................................................................... 20 Selecting and Manipulating Data.................................................................................................. 21 Selecting Data ......................................................................................................................... 21 Manipulating Data .................................................................................................................. 24 Controlling Transactions......................................................................................................... 28 Exercise 3: Selecting and Manipulating Data............................................................................... 29 Writing Conditional IF Statements.............................................................................................. 31 Writing IF-THEN Statements............................................................................................. 31 Adding an ELSE Clause ...................................................................................................... 32 Nesting IF Statements......................................................................................................... 33 Using the ELSIF Clause ..................................................................................................... 34 Dealing with NULL Values .................................................................................................. 35 Using Oracle Data to Make Decisions.................................................................................... 38 Exercise 4: Writing IF Statements............................................................................................... 41 Using CASE Statements and Expressions.................................................................................. 43 Writing Simple CASE Statements........................................................................................ 43 Writing Searched CASE Statements .................................................................................... 44 Understanding CASE Expressions ....................................................................................... 46 Using CASE Expressions with Oracle Data......................................................................... 48 Oracle Programming Using PL/SQL: Level 1 www.micros.umsl.edu

2/6/09

Exercise 5: Using CASE Statements and Expressions.................................................................. 50 Working with Loops ..................................................................................................................... 51 Writing Basic Loops ............................................................................................................... 51 Using WHILE Loops ............................................................................................................ 56 Using Numeric FOR Loops.................................................................................................. 58 Using Loops to Process Oracle Data ...................................................................................... 60 Choosing the Best Loop.......................................................................................................... 61 Exercise 6: Working with Loops .................................................................................................. 62 Working with Cursors................................................................................................................... 63 Managing Explicit Cursors ..................................................................................................... 63 Using Cursor Attributes .......................................................................................................... 67 Exercise 7: Using Explicit Cursors ............................................................................................... 72 Using Cursors with PL/SQL Records ........................................................................................... 73 Declaring PL/SQL Records .................................................................................................... 73 Fetching Data into PL/SQL Records ...................................................................................... 75 Using Cursor FOR Loops ..................................................................................................... 76 Using Cursors with Parameters............................................................................................... 77 Exercise 8: Using Cursors with PL/SQL Records ........................................................................ 81 Handling Exceptions..................................................................................................................... 82 Classifying PL/SQL Errors ..................................................................................................... 82 Trapping Exceptions ............................................................................................................... 83 Handling Named System Exceptions...................................................................................... 83 Handling Unnamed System Exceptions.................................................................................. 85 Handling User-Defined Exceptions ........................................................................................ 87 Handling Unspecified Exceptions........................................................................................... 90 Using Error-Trapping Functions to Create an Error Log (Optional)...................................... 90 Exercise 9: Handling Exceptions .................................................................................................. 92 Appendix A: Suggested Naming Conventions ............................................................................. 94 Appendix B: PL/SQL Datatypes................................................................................................... 95 Appendix C: Named System Exceptions...................................................................................... 96 Appendix D: Fads Through the Decades Museum ....................................................................... 97 Appendix E: SQL*Plus Commands and SQL Developer............................................................. 99 Appendix F: Solutions to Exercises............................................................................................ 100

Oracle Programming Using PL/SQL: Level 1 www.micros.umsl.edu

Suggest Documents