Thursday, October 29, 2009

Lab 2.2 Exercises



[ Team LiB ]





Lab 2.2 Exercises


2.2.1 Use Substitution Variables


In this exercise, you will calculate the square of a number. The value of the number will be provided with the help of a substitution variable. Then the result will be displayed on the screen.


Create the following PL/SQL script:





-- ch02_1a.sql, version 1.0
SET SERVEROUTPUT ON
DECLARE
v_num NUMBER := &sv_num;
v_result NUMBER;
BEGIN
v_result := POWER(v_num, 2);
DBMS_OUTPUT.PUT_LINE ('The value of v_result is: '||
v_result);
END;

Execute the script, and then answer the following questions:


a)

If the value of v_num is equal to 10, what output is printed on the screen?

b)

What is the purpose of using a substitution variable?

c)

Why is it considered a good practice to enclose substitution variables with single quotes for string datatypes?



2.2.2 Use the DBMS_OUTPUT.PUT_LINE Statement


In this exercise, you will determine the day of the week based on today's date. You will then display the results on the screen.


Create the following PL/SQL script:





-- ch02_2a.sql, version 1.0
SET SERVEROUTPUT ON
DECLARE
v_day VARCHAR2(20);
BEGIN
v_day := TO_CHAR(SYSDATE, 'Day');
DBMS_OUTPUT.PUT_LINE ('Today is '||v_day);
END;

Execute the script, and then answer the following questions:


a)

What was printed on the screen?

b)

What will be printed on the screen if the statement SET SERVEROUTPUT OFF is issued? Why?

c)

How would you change the script to display the time of the day as well?






    [ Team LiB ]



    No comments:

    Post a Comment