ch2_pp_answers.pks 4.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
# problem 1

Start SQL*Plus, log onto the database, and set up the PL/SQL output buffer using the SET SERVEROUTPUT ON command (Note: if the SET SERVEROUTPUT ON command is not used, the output will not be displayed on the screen).
*/

/*
## problem 2

Write a PL/SQL program to display your name.
*/

begin
  dbms_output.put_line('MICHAEL LUNDQUIST');
end;
/

/*
## problem 3

Write a PL/SQL program that declares two variables (variable1 and variable2), sets their values to zero, and performs the following calculation: variable2=variable1+1 and displays the value of variable2.
*/

declare
  variable1 NUMBER(2) := 0;
  variable2 NUMBER(2) := 0;
begin
  variable2 := variable1 + 1;
  dbms_output.put_line('the value of var2 is now ' || variable2);
end;
/

/*
## Problem 4

Write a PL/SQL program to display the current date.
*/

begin
  dbms_output.put_line('today is ' || sysdate);
end;
/

/*
## problem 5

Write a PL/SQL program to declare a variable V_NUMBER, assign a value to it (any number), and display its’ value if the number is positive.
*/

declare
V_NUMBER NUMBER(10) := 46290;
begin
  IF V_NUMBER >= 0 THEN
  dbms_output.put_line('V_NUMBER IS ' || V_NUMBER);
  END IF;
end;
/

/*
## problem 6

Write a PL/SQL program to declare a variable V_NUMBER, assign a value to it (any number), and test if it is a positive number or a negative number. Display the result.
*/

declare
  V_NUMBER NUMBER(10) := 46290;
begin
  IF V_NUMBER > 0 OR V_NUMBER < 0 THEN
  dbms_output.put_line('V_NUMBER IS ' || V_NUMBER);
  END IF;
end;
/

/*
## problem 7

Write a PL/SQL program to test whether today is Friday or not. Display the result.
*/

BEGIN 
  IF to_char (SYSDATE, 'D') = '6' THEN
     dbms_output.put_line('TODAY IS FRIDAY');
  ELSE
    dbms_output.put_line('TODAY IS NOT FRIDAY');
  END IF;
END;
/

/*
## problem 8

Write a PL/SQL program to test if your date of birth falls on Friday, Saturday, or Sunday. Display the result if it’s Friday, Saturday, or Sunday, otherwise print "It falls on a week-day".
*/
declare
  lv_birthday_date DATE := '07-jul-1993';
BEGIN 
  IF to_char (lv_birthday_date, 'D') = '6' THEN
     dbms_output.put_line('Birthday on FRIDAY');
  ELSE
    dbms_output.put_line('birthday NOT on FRIDAY');
  END IF;
END;
/

/*
## problem 9

Write a PL/SQL program that declares a variable V_COUNTER, assigns 1 to it, and then adds 1 to the variable 5 times. Display the value of the variable every time it changes. Use the Basic LOOP structure.
*/
declare
  lv_counter_num number(2) := 0;
begin
  loop
    lv_counter_num := lv_counter_num + 1;
    exit when lv_counter_num >= 5;
  end loop;
  dbms_output.put_line('result val ' || lv_counter_num);
end;
/

/*
## problem 10

Write a PL/SQL program that declares a variable V_COUNTER, assigns 1 to it, and then adds 1 to the variable 5 times. Display the value of the variable every time it changes. Use the WHILE … LOOP structure
*/

declare
  V_COUNTER number(2) := 1;
begin
  WHILE V_COUNTER <= 5 LOOP
    V_COUNTER := V_COUNTER + 1;
    dbms_output.put_line('CUR VAL ' || V_COUNTER);
  end loop;
end;
/

/*
## problem 11

Write a PL/SQL program that declares a variable V_COUNTER, assigns 1 to it, and then adds 1 to the variable 5 times. Display the value of the variable every time it changes. Use the FOR LOOP structure.
*/

begin
  FOR V_COUNTER IN 1..6 LOOP
    dbms_output.put_line('CUR VAL ' || V_COUNTER);
  end loop;
end;
/

/*
## problem 12

Write a PL/SQL program that declares a variable, assigns to it your birthday, and checks what day you were born on. Use the CASE expression. Display the result.
*/

declare
  lv_birthday_date DATE := '07-jul-1993';
  LV_DAY_CHAR CHAR(1) := to_char (lv_birthday_date, 'D');
  LV_TEXTDAY_VCHAR VARCHAR(10);
BEGIN
  CASE LV_DAY_CHAR
    WHEN '1' THEN LV_TEXTDAY_VCHAR := 'SUNDAY';
    WHEN '2' THEN LV_TEXTDAY_VCHAR := 'MONDAY';
    WHEN '3' THEN LV_TEXTDAY_VCHAR := 'TUESDAY';
    WHEN '4' THEN LV_TEXTDAY_VCHAR := 'WEDNESDAY';
    WHEN '5' THEN LV_TEXTDAY_VCHAR := 'THURSDAY';
    WHEN '6' THEN LV_TEXTDAY_VCHAR := 'FRIDAY';
    WHEN '7' THEN LV_TEXTDAY_VCHAR := 'SATURDAY';
  END CASE;
  dbms_output.put_line('YOU WERE BORN ON A ' || LV_TEXTDAY_VCHAR);
END;
/