Chapter 4 Test Your Thinking


Oracle® PL/SQL® Interactive Workbook, Second Edition
By Benjamin Rosenzweig, Elena Silvestrova
Table of Contents
Chapter 4.  SQL in PL/SQL

In the chapter discussion, you learned how to use numerous SQL techniques in a PL/SQL block. First, you learned how to use SELECT INTO to generate values for a variable. Then you learned the various DML methods, including the use of a sequence. Finally, you learned how to manage transactions by using savepoints. Complete the following projects by writing the code for each step and running it and then going on to the next step.


Create a table called CHAP4 with two columns; one is ID (a number) and the second is NAME, which is a varchar2(20).


Create a sequence called CHAP4_SEQ that increments by units of 5.


Write a PL/SQL block that performs the following in this order:

  1. Declares 2 variables, one for the v_name and one for v_id. The v_name variable can be used throughout the block for holding the name that will be inserted; realize that the value will change in the course of the block.

  2. The block then inserts into the table the name of the student that is enrolled in the most classes and uses a sequence for the ID; afterward there is SAVEPOINT A.

  3. Then the student with the least enrollments is inserted; afterward there is SAVEPOINT B.

  4. Then the instructor who is teaching the maximum number of courses is inserted in the same way. Afterward there is SAVEPOINT C.

  5. Using a SELECT INTO statement, hold the value of the instructor in the variable v_id.

  6. Undo the instructor insert by use of rollback.

  7. Insert the instructor teaching the least amount of courses but do not use the sequence to generate the ID; instead use the value from the first instructor whom you have since undone.

  8. Now insert the instructor teaching the most number of courses and use the sequence to populate his ID.

Add DBMS_OUTPUT throughout the block to display the values of the variables as they change. (This is good practice for debugging.)

The answers to Test Your Thinking can be found in Appendix D and on the web site.


    Oracle PL. SQL Interactive Workbook
    Oracle PL/SQL Interactive Workbook (2nd Edition)
    ISBN: 0130473200
    EAN: 2147483647
    Year: 2002
    Pages: 146

    Similar book on Amazon © 2008-2017.
    If you may any questions please contact us: