Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Practice 1 Solutions

1. Evaluate each of the following declarations. Determine which of them are not legal and explain
why.
a. DECLARE
v_id NUMBER(4);
Legal

b. DECLARE
v_x, v_y, v_z VARCHAR2(10);
Illegal because only one identifier per declaration is allowed.

c. DECLARE
v_birthdate DATE NOT NULL;
Illegal because the NOT NULL variable must be initialized.

d. DECLARE
v_in_stock BOOLEAN := 1;
Illegal because 1 is not a Boolean expression.
PL/SQL returns the following error:
PLS-00382: expression is of wrong type

Oracle9i: Program with PL/SQL A-2


Practice 1 Solutions (continued)
2. In each of the following assignments, indicate whether the statement is valid and what the valid data
type of the result will be.
a. v_days_to_go := v_due_date - SYSDATE;
Valid; Number

b. v_sender := USER || ': ' || TO_CHAR(v_dept_no);


Valid; Character string

c. v_sum := $100,000 + $250,000;


Illegal; PL/SQL cannot convert special symbols from VARCHAR2 to NUMBER.

d. v_flag := TRUE;
Valid; Boolean

e. v_n1 := v_n2 > (2 * v_n3);


Valid; Boolean

f. v_value := NULL;
Valid; Any scalar data type

3. Create an anonymous block to output the phrase “My PL/SQL Block Works” to the screen.

VARIABLE g_message VARCHAR2(30)


BEGIN
:g_message := 'My PL/SQL Block Works';
END;
/
PRINT g_message

Alternate Solution:

SET SERVEROUTPUT ON
BEGIN
DBMS_OUTPUT.PUT_LINE('My PL/SQL Block Works');
END;
/

Oracle9i: Program with PL/SQL A-3


Practice 1 Solutions (continued)
If you have time, complete the following exercise:
4. Create a block that declares two variables. Assign the value of these PL/SQL variables to
iSQL*Plus host variables and print the results of the PL/SQL variables to the screen. Execute
your PL/SQL block. Save your PL/SQL block in a file named p1q4.sql, by clicking the Save
Script button. Remember to save the script with a .sql extension.

V_CHAR Character (variable length)


V_NUM Number
Assign values to these variables as follows:
Variable Value
---------- ----------------------------------------
V_CHAR The literal '42 is the answer'
V_NUM The first two characters from V_CHAR

VARIABLE g_char VARCHAR2(30)


VARIABLE g_num NUMBER
DECLARE
v_char VARCHAR2(30);
v_num NUMBER(11,2);
BEGIN
v_char := '42 is the answer';
v_num := TO_NUMBER(SUBSTR(v_char,1,2));
:g_char := v_char;
:g_num := v_num;
END;
/
PRINT g_char
PRINT g_num

Oracle9i: Program with PL/SQL A-4

You might also like