0% found this document useful (0 votes)
50 views5 pages

Ubd 7

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

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 TABLE employees_dup AS SELECT * FROM employees

CREATE OR REPLACE PROCEDURE change_salary 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 change_salary;

Apelare procedura:

BEGIN
change_salary;

END;

Folosind un select verificam daca procedura s-a executat corect:

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_city wf_countries.capitol%TYPE;
BEGIN
SELECT country_name, capitol INTO v_country_name, v_capitol_city
FROM wf_countries
WHERE country_id=p_country_id;
DBMS_OUTPUT.PUT_LINE(' ' || v_country_name || ' ' || v_capitol_city);
END;

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

BEGIN
get_country_info(90);
END;

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

BEGIN
get_country_info(95);
END;

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_city wf_countries.capitol%TYPE;

BEGIN

SELECT country_name, capitol INTO v_country_name, v_capitol_city

FROM wf_countries

WHERE country_id=p_country_id;

DBMS_OUTPUT.PUT_LINE(' ' || v_country_name || ' ' || v_capitol_city);

EXCEPTION

WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('There are no records on the requested

department id.');

END;

BEGIN

get_country_info(95);

END;

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_salary(p_department_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_department_id AND salary = (SELECT MAX(salary) FROM

employees WHERE department_id = e.department_id);

END;

Testam pentru department_id = 80:

DECLARE

v_last_name employees.last_name%TYPE;

v_salary employees.salary%TYPE;

BEGIN

highest_salary(80, v_last_name, v_salary);

DBMS_OUTPUT.PUT_LINE('Last name: ' || v_last_name || ' earns: ' || v_salary);

END;

Testam pentru valoarea default:

DECLARE

v_last_name employees.last_name%TYPE;
v_salary employees.salary%TYPE;

BEGIN

highest_salary(p_last_name => v_last_name, p_salary => v_salary);

DBMS_OUTPUT.PUT_LINE('Last name: ' || v_last_name || ' earns: ' || v_salary);

END;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy