0% found this document useful (0 votes)
34 views

Ubd Lab7

The document discusses creating procedures in Oracle SQL that can update employee salaries based on their department, accept parameters, and return output parameters. It includes examples of creating a procedure to update salaries, a procedure that accepts a country ID as a parameter to return country information, and a procedure that finds the highest paid employee in a department using input and output parameters.

Uploaded by

Laurentiu Danuta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Ubd Lab7

The document discusses creating procedures in Oracle SQL that can update employee salaries based on their department, accept parameters, and return output parameters. It includes examples of creating a procedure to update salaries, a procedure that accepts a country ID as a parameter to return country information, and a procedure that finds the highest paid employee in a department using input and output parameters.

Uploaded by

Laurentiu Danuta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Tema nr.

7
Observație!
Scrieți rezolvarea direct în acest document!

Creating Procedures
1. Create, save and execute a procedure which updates the salary of employees in employees_dup
according to the following rules:

.1 if the employee is in department 80, their new salary must = 1000


.2 if the employee is in department 50, their new salary must = 2000
.3 if the employee is in any other department, their new salary must = 3000.

You will need to include three UPDATE statements, one for each of the above rules. Execute your
procedure from an anonymous block and verify that the updates have been performed correctly.

CREATE OR REPLACE PROCEDURE sal_change IS


BEGIN
UPDATE employees_dup
SET salary = 1000
WHERE department_id = 80;
UPDATE employees_dup
SET salary = 2000
WHERE department_id = 50;
UPDATE employees_dup
SET salary = 3000
WHERE department_id NOT IN (80, 50);
END sal_change;
BEGIN
Sal_change;
END;
SELECT employee_id, department_id, salary FROM employees_dup;

Using Parameters in Procedures

1. Using the wf_countries table:

A. Create a procedure that accepts a country_id as a parameter and displays the name of the country
and its capitol city. Name your procedure get_country_info. Save your procedure definition for later
use.

CREATE OR REPLACE PROCEDURE get_country_info


(p_country_id IN wf_countries.country_id%TYPE)
IS
v_country_name wf_countries.country_name%TYPE;
v_capitol wf_countries.capitol%TYPE;
BEGIN
SELECT country_name, capitol
INTO v_country_name, v_capitol
FROM wf_countries
WHERE country_id = p_country_id;
DBMS_OUTPUT.PUT_LINE('The country name is '||v_country_name||
' and the capital city is '||v_capitol);
END;

B. Execute your procedure from an anonymous block, using country_id 90.

BEGIN
get_country_info(90);
END;
The country name is Republic of Turkey and the capital city is Ankara.

C. Re-execute the procedure from the anonymous block, this time using country_id 95. What
happens?

Eroarea se afiseaza pentru ca country_id=95 nu exista.

D. Retrieve your procedure code from Saved SQL and modify it to trap the NO_DATA_FOUND
exception in an exception handler. Re-execute the procedure using country_id 95 again. Now what
happens?

CREATE OR REPLACE PROCEDURE get_country_info


(p_country_id IN wf_countries.country_id%TYPE)
IS
v_country_name wf_countries.country_name%TYPE;
v_capitol wf_countries.capitol%TYPE;
BEGIN
SELECT country_name, capitol
INTO v_country_name, v_capitol
FROM wf_countries
WHERE country_id = p_country_id;
DBMS_OUTPUT.PUT_LINE('The country name is '||v_country_name||
‘ and the capital city is '||v_capitol);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE(‘This country does not exist’);
END;
Exceptia NO_DATA_FOUND a fost blocata de codul de procedura

Passing Parameters

1. Create and test a procedure which accepts a department id as an IN parameter with a default
value of 50, and returns (using OUT parameters) the last name and salary of the employee in that
department who has the highest salary. The SELECT statement in the procedure should use a
subquery. Test your procedure twice from an anonymous block, the first time using department
id 80, the second time using the default value.

CREATE OR REPLACE PROCEDURE highest_sal


(p_dept_id IN employees.department_id%TYPE DEFAULT 50,
p_last_name OUT employees.last_name%TYPE,
p_salary OUT employees.salary%TYPE) IS
BEGIN
SELECT last_name, salary INTO p_last_name, p_salary
FROM employees e
WHERE department_id = p_dept_id
AND salary =
(SELECT MAX(salary)FROM employees
WHERE department_id = e.department_id);
END;

/*department_id=80*/
DECLARE
v_last_name employees.last_name%TYPE;
v_salary employees.salary%TYPE;
BEGIN
highest_sal(80, v_last_name, v_salary);
DBMS_OUTPUT.PUT_LINE('Last name: ' || v_last_name || ' earns: ' || v_salary);
END;

Last name:Abel earns: 11000;

/*default value*/
DECLARE
v_last_name employees.last_name%TYPE;
v_salary employees.salary%TYPE;
BEGIN
highest_sal(p_last_name => v_last_name, p_salary => v_salary);
DBMS_OUTPUT.PUT_LINE('Last name: ' || v_last_name || ' earns: ' || v_salary);
END;

Last name:Mourgos earns: 5800;

You might also like