Commit 27dcf4b2 authored by michael lundquist's avatar michael lundquist
Browse files

Adding ch2 practice problems.

parent a9100c76
# 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).
![problem1](./pics/problem1.png)
## problem 2
Write a PL/SQL program to display your name.
```plsql
begin
dbms_output.put_line('MICHAEL LUNDQUIST');
end;
/
```
![problem2](./pics/PROBLEM2.png)
## 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.
```plsql
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;
/
```
![problem3]("./pics/problem 3.png")
## Problem 4
Write a PL/SQL program to display the current date.
```plsql
begin
dbms_output.put_line('today is ' || sysdate);
end;
/
```
![problem4](./pics/problem4.png)
## 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.
```plsql
declare
V_NUMBER NUMBER(10) := 46290;
begin
IF V_NUMBER >= 0 THEN
dbms_output.put_line('V_NUMBER IS ' || V_NUMBER);
END IF;
end;
/
```
![problem5](./pics/PROBLEM5.png)
## 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.
```plsql
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;
/
```
![problem6](./pics/PROBLEM6.png)
## problem 7
Write a PL/SQL program to test whether today is Friday or not. Display the result.
```plsql
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;
/
```
![problem7](./pics/PROBLEM7.png)
## 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".
```plsql
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;
/
```
![problem8](./pics/problem8.png)
## 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.
```plsql
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;
/
```
[problem9](./pics/problem 9.PNG)
## 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
```plsql
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;
/
```
![problem10]("./pics/PROBLEM10.png")
## 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.
```plsql
begin
FOR V_COUNTER IN 1..6 LOOP
dbms_output.put_line('CUR VAL ' || V_COUNTER);
end loop;
end;
/
```
![problem11](./pics/PROBLEM11.png)
## 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.
```plsql
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;
```
![problem12](./pics/PROBLEM12.png)
\ No newline at end of file
/*
# 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;
/
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment