Lab 4.1 Exercise Answers


This section gives you some suggested answers to the questions in Lab 4.1, with discussion related to how those answers resulted. The most important thing to realize is whether your answer works. You should figure out the implications of the answers here and what the effects are from any different answers you may come up with.

4.1.1 Answers

Run the PL/SQL block from the pre-exercise example.

a)

What is displayed on the SQL*Plus screen? Explain the results.

A1:

Answer: You will see the following result:

 The average cost of a course in the CTA program is $1,198.33 PL/SQL procedure successfully completed. 

In the declaration section of the PL/SQL block, the variable v_average_cost is declared as a varchar2. In the executable section of the block, this variable is given the value of the average cost from the course table by means of the SELECT INTO syntax. The SQL function TO_CHAR is issued to format the number. The DBMS_OUTPUT is then used to show the result to the screen.

b)

Take the same PL/SQL block and place the line with the DBMS_OUTPUT before the SELECT INTO statement. What is displayed on the SQL*Plus screen? Explain what the value of the variable is at each point in the PL/SQL block.

A2:

Answer: You will see the following result:

 The average cost of a course in the CTA program is PL/SQL procedure successfully completed. 

The variable v_average_cost will be set to NULL when it is first declared. Because the DBMS_OUTPUT is placed before the variable is given a value, the output for the variable will be NULL. After the SELECT INTO, the variable will be given the same value as in the original block described in question a, but it will not be displayed because there is not another DBMS_OUTPUT line in the PL/SQL block.

4.1.2 Answers

a)

Write a PL/SQL block that will insert a new student in the student table. Use your own information for the data.

A1:

Answer: The following is one example of how this could be handled:

 -- ch04_4a.sql DECLARE v_max_id number; BEGIN SELECT MAX(student_id) INTO v_max_id FROM student; INSERT into student (student_id, last_name, zip, created_by, created_date, modified_by, modified_date, registration_date ) VALUES (v_max_id + 1, 'Rosenzweig', 11238, 'BROSENZ ', '01-JAN-99', 'BROSENZ', '01-JAN-99', '01-JAN-99' ); END; 

In order to generate a unique ID, the maximum student_id is selected into a variable and then it is incremented by one. It is important to remember in this example that there is foreign key on the zip item in the student table, which means that the zipcode you choose to enter must be in the ZIPCODE table.

4.1.3 Answers

a)

Write a PL/SQL block that will insert a new student in the student table. Use your own information for the data. Create two variables that are used in the select statement. Get the USER and SYSDATE for the variables. Finally, use the existing student_id_seq sequence to generate a unique id for the new student.

A1:

Answer: The following is one example of how this could be handled:

 -- ch04_5a.sql DECLARE v_user student.created_by%TYPE; v_date student.created_date%TYPE; BEGIN SELECT USER, sysdate INTO v_user, v_date FROM dual; INSERT INTO student (student_id, last_name, zip, created_by, created_date, modified_by, modified_date, registration_date ) VALUES (student_id_seq.nextval, 'Smith', 11238, v_user, v_date, v_user, v_date, v_date ); END; 

In the declaration section of the PL/SQL block, two variables are declared. They are both set to be datatypes within the student table using the %TYPE method of declaration. This ensures the datatypes match the columns of the tables into which they will be inserted. The two variables v_user and v_date are given values from the system by means of SELECT INTO. The value of the student_id is generated by using the next value of the student_id_seq sequence.



Oracle PL[s]SQL by Example
Oracle PL[s]SQL by Example
ISBN: 3642256902
EAN: N/A
Year: 2003
Pages: 289

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net