You are on page 1of 4

-- Practice 3

/* 1. Write a PL/SQL block to accept a year and check whether it is a leap year.
For example, if the year entered is 1990, the output should be “1990 is not a leap
year.”
Hint: The year should be exactly divisible by 4 but not divisible by 100, or it
should be divisible by 400.
Test your solution with the following years: */

DECLARE
year_num NUMBER :=2024;
BEGIN
IF(year_num mod 4 = 0 AND year_num mod 100 != 0) OR year_num mod 400 = 0 THEN
DBMS_OUTPUT.PUT_LINE(year_num || ' is a leap year.');
ELSE
DBMS_OUTPUT.PUT_LINE(year_num || ' is not a leap year.');
END IF;
END;

/*2. Write a PL/SQL block to declare a variable called sal to store the salary of
an employee.
If the salary is less than 3,000, give the employee a raise of 500
and display the message “<Employee Name>’s salary updated” in the window.
If the salary is more than 3,000, print the employee’s salary in the format,
“<Employee Name> earns …...………”
Test the PL/SQL block for the following last names: */
DECLARE
sal NUMBER:=2500;
emp_name VARCHAR2(50):='Someone';
BEGIN
IF sal<3000 THEN
sal:=sal+500;
DBMS_OUTPUT.PUT_LINE(emp_name || '''s salary updated -> ' || sal);
ELSE
DBMS_OUTPUT.PUT_LINE(emp_name || ' earns ' || sal);
END IF;
END;

/*
3. Create a table messages with one column Results of the type Number (3).
Write a PL/SQL block to insert numbers into the messages table.
a. Insert the numbers 1 to 10, excluding 6 and 8.
b. Commit before the end of the block.
c. Execute a SELECT statement to verify that your PL/SQL block worked.
You should see the following output. Use different types of loops.
*/
CREATE TABLE messages (
Results NUMBER(3)
);

BEGIN
FOR i in 1..10 LOOP
IF i NOT IN(6,8) then
INSERT INTO messages(Results) VALUES (i);
END IF;
END LOOP;
COMMIT;
END;

--
SELECT * FROM messages;

/*
4. Create a PL/SQL block to identify a name of the month depending on its
number. For example,
if a user input number 3, the output must be March. Use CASE expression and CASE
statement control structures. */
DECLARE
month_num NUMBER := 5;
name_of_month VARCHAR2(20);
BEGIN
name_of_month:=
CASE month_num
WHEN 1 THEN 'January'
WHEN 2 THEN 'February'
WHEN 3 THEN 'March'
WHEN 4 THEN 'April'
WHEN 5 THEN 'May'
WHEN 6 THEN 'June'
WHEN 7 THEN 'July'
WHEN 8 THEN 'August'
WHEN 9 THEN 'September'
WHEN 10 THEN 'October'
WHEN 11 THEN 'November'
WHEN 12 THEN 'December'
ELSE 'Invalid month number'
END;

DBMS_OUTPUT.PUT_LINE('Month number ' || month_num || ' is ' || name_of_month);


END;

/*
5. Within the PL/SQL block create a record to store information about yourself:
name, year of birth, place of birth. Output the information to the screen. */
DECLARE
TYPE person_type IS RECORD(
name VARCHAR2(50),
year_of_birth NUMBER(8),
place_of_birth VARCHAR2(50)
);
person person_type;
BEGIN
person.name := 'Akmaral Kakharova';
person.year_of_birth := 2002;
person.place_of_birth := 'Almaty, Kazakhstan';

DBMS_OUTPUT.PUT_LINE('Name: ' || person.name);


DBMS_OUTPUT.PUT_LINE('Year of birth: ' || person.year_of_birth);
DBMS_OUTPUT.PUT_LINE('Place of birth: ' || person.place_of_birth);
END;

/*
6. Write a PL/SQL block to print information about a given country.
a. Declare a PL/SQL record based on the structure of the countries table.
b. Define a variable countryid and pass the value through a substitution
variable.
c. In the executable section, get all the information from the countries table
by using countryid.
Display selected information about the country. A sample output is shown below.
d. You may want to execute and test the PL/SQL block for the countries with the
IDs DE, UK, US. */

DECLARE
TYPE country_info IS RECORD (
country_id HR.countries.country_id%TYPE,
country_name HR.countries.country_name%TYPE,
region_id HR.countries.region_id%TYPE
);

v_country_id HR.countries.country_id%TYPE := 'CA';


v_country country_info;

BEGIN
SELECT country_id, country_name, region_id
INTO v_country
FROM HR.countries
WHERE country_id = v_country_id;

DBMS_OUTPUT.PUT_LINE('Country ID: ' || v_country.country_id);


DBMS_OUTPUT.PUT_LINE('Country Name: ' || v_country.country_name);
DBMS_OUTPUT.PUT_LINE('Region ID: ' || v_country.region_id);
END;

select * from HR.countries;

/*
7. Within a PL/SQL block create an INDEX BY table to store
the square of the numbers from 1 to 8. Output the result to the screen. */

DECLARE
TYPE square_table IS TABLE OF NUMBER INDEX BY PLS_INTEGER;
squares square_table;
BEGIN
FOR i IN 1..8 LOOP
squares(i) := i * i;
END LOOP;

FOR i IN 1..8 LOOP


DBMS_OUTPUT.PUT_LINE(i || ' squared is ' || squares(i));
END LOOP;
END;

/*8. Create a PL/SQL block to retrieve the names of some departments


from the departments table and print each department name on the screen,
incorporating an INDEX BY table. Using a loop, retrieve the names of 10 departments
and store the names in the INDEX BY table. Start with department_id 10.
Increase deptno by 10 for every iteration of the loop.
The following table shows the department_id for which you should retrieve
the department_name and store in the INDEX BY table. Using another loop,
retrieve the department names from the INDEX BY table and display them. */

DECLARE
TYPE dept_table IS TABLE OF hr.departments.department_name%TYPE INDEX BY
BINARY_INTEGER;
dept_names dept_table;
dept_id hr.departments.department_id%TYPE := 10;
BEGIN
FOR i IN 1..10 LOOP

SELECT department_name
INTO dept_names(i)
FROM hr.departments
WHERE department_id = dept_id;

dept_id := dept_id + 10;


END LOOP;

FOR i IN 1..dept_names.COUNT LOOP


DBMS_OUTPUT.PUT_LINE(dept_names(i));
END LOOP;
END;

select * from hr.departments

You might also like