Display The Hire Date For The Employee With Id 110
Display The Hire Date For The Employee With Id 110
set serveroutput on
declare
v_emp employees%rowtype;
begin
select * into v_emp from employees where employee_id = 110;
dbms_output.put_line('Employee '|| v_emp.first_name || ' ' || v_emp.last_name || ' was hired
on ' || v_emp.hire_date);
end;
/
or
set serveroutput on
declare
first_name employees.first_name%type,
last_name employees.last_name%type,
hire_date date
);
v_emp t_emp;
begin
select first_name, last_name , hire_date into v_emp from employees where employee_id = 110;
end;
set serveroutput on
declare
v_salary number;
begin
else
end if;
end;
When case
declare
begin
case
dbms_output.put_line('Has decimals!');
dbms_output.put_line('Absent!');
dbms_output.put_line('Failed!');
dbms_output.put_line('Passed!');
--else
-- dbms_output.put_line('Wrong mark!');
end case;
end;
--SUM OF ODD AND EVEN NUMBERS FROM STRING
set serveroutput on
declare
sum_odd number := 0;
sum_even number := 0;
c char;
begin
dbms_output.put_line(i);
c := substr(v_string, i, 1);
if c mod 2 = 0 then
sum_even := sum_even + c;
else
sum_odd := sum_odd + c;
end if;
end loop;
end;
--Create a SP that update the salary of a given employee (by substitution) for the following cases:
set serveroutput on
set define on
declare
v_salary employees.salary%type;
begin
v_salary := v_salary*1.1;
else
end if;
end;
----------------------------------------------------------------------------------
--1. Delete a row from Product_information table and count the number of deleted ones.
set serveroutput on
declare
nb_r number(2);
begin
nb_r := SQL%ROWCOUNT;
commit;
end;
--------------------------------------------------------------------------------------------
--2.Try to modify the name of the product 113. If the product doesn't exist(the update command
won't modify anything),
--display a message.
begin
if SQL%NOTFOUND then
end if;
end;
declare
row_c c%rowtype;
begin
open c;
loop
end if;
end loop;
close c;
end;
----------------------------------------------------------------------------------
declare
row_c c%rowtype;
begin
open c;
loop
dbms_output.put_line(row_c.region_name);
end loop;
close c;
end;
-- 5. Display the regions that begin with 'A' or 'a' using for loop
********************************************************************************
declare
begin
dbms_output.put_line(region.region_name);
end loop;
end;
-- 6.Display the products that have a total ordered quantity larger than a value received as a
parameter
declare
group by p.product_id
v_val NUMBER(5);
rec_prod c%rowtype;
BEGIN
v_val:=50;
DBMS_OUTPUT.PUT_LINE('The products that have a total ordered quantity larger than '|| v_val);
OPEN c (v_val);
END IF;
LOOP
END LOOP;
CLOSE c;
END;
declare
cursor c is select product_name, list_price from product_information where list_price is not null
order by list_price desc;
product c%rowtype;
begin
open c;
loop
end loop;
close c;
end;
declare
cursor c is select product_id id, sum(quantity) total from order_items group by product_id order by
total desc;
product c%rowtype;
begin
open c;
loop
end loop;
close c;
end;
OR
begin
for rec in
where p.product_id=o.product_id
group by p.product_id)
loop
dbms_output.put_line(rec.total_quantity||' pieces of product '||rec.prod||' were ordered');
end loop;
end;
For Orders table update the order_date by adding a week to each date. Use a FOR UPDATE cursor to
block the rows in the table when the cursor is opened.
DECLARE
BEGIN
UPDATE orders
SET order_date=order_date+7
WHERE order_id=rec.order_id;
END LOOP;
END;
--For Orders table update the order_date by decreasing it by one week. Use FOR UPDATE and
WHERE CURRENT OF clauses.
DECLARE
CURSOR c IS
SELECT *
FROM orders
BEGIN
UPDATE orders
SET order_date=order_date-7
WHERE CURRENT OF c;
END LOOP;
END;
Display all the customers and their orders. Use a cursor to load the customer’s name and another
cursor with parameter to load their orders
declare
cursor c is
begin
dbms_output.put_line(rec2.order_id);
end loop;
end loop;
end;
Display the orders that have the largest value. Stop after the first 5 orders.
declare
cursor c is
product c%rowtype;
begin
open c;
loop
end loop;
close c;
end;
--Display product names for the products that have been orderd at least once
set serveroutput on
declare
cursor c is
r varchar2(50);
begin
open c;
end if;
loop
fetch c into r;
end loop;
close c;
end;
set serveroutput on
declare
cursor c is
r c%rowtype;
begin
for r in c loop
end loop;
end;
OR
set serveroutput on
declare
cursor c is
on p.product_id = o.product_id
r c%rowtype;
begin
open c;
end if;
loop
fetch c into r;
end loop;
close c;
end;
set serveroutput on
declare
on p.product_id = o.product_id
r c%rowtype;
begin
for r in c loop
end loop;
end;
OR
set serveroutput on
declare
on p.product_id = o.product_id
r c%rowtype;
begin
open c;
end if;
loop
fetch c into r;
end loop;
close c;
end;
OR
set serveroutput on
declare
i pls_integer := 0;
begin
for r in (select distinct product_name, (list_price + min_price)/2 mean_price
on p.product_id = o.product_id
loop
i := i+1;
end loop;
end;
Display the department names of the departments which have employees. Under each department
display the names of the employees from that department.
+ the number of employees per department and the total number allover
set serveroutput on
declare
cursor c is
from departments d
join employees e
on e.department_id = d.department_id;
nr1 number;
begin
for r1 in c loop
nr1 := 0;
dbms_output.put_line (r1.department_name);
nr2 := nr2 + 1;
end loop;
dbms_output.put_line(nr1);
end loop;
dbms_output.put_line(nr2);
end;
set serveroutput on
declare
sal_before number;
sal_after number;
begin
for r in c loop
where current of c;
end loop;
rollback;
end;
--PRE-DEFINED EXCEPTION
--Display the product 13. If there is no such product, handle the exception.
DECLARE
name VARCHAR2(20);
BEGIN
SELECT product_name INTO name
FROM product_information
WHERE product_id=13;
dbms_output.put_line(name);
EXCEPTION
END;
--NON-PREDEFINED EXCEPTION
--Insert a row in the Locations table. For the location_id use the value 213 and for the coutry_id use
NULL. An exception having the code ORA-01400 will be automatically raised. Handle the exception
regarding violating the integrity constraint.
DECLARE
INSERT_EXCEPT EXCEPTION;
BEGIN
EXCEPTION
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
--USER-DEFINED EXCEPTION
--Raise an error if the user tries to execute the PL/SQL block before 5 o’clock pm.
DECLARE
e_exc1 EXCEPTION;
BEGIN
RAISE e_exc1;
END IF;
EXCEPTION
WHEN e_exc1 THEN
END;
User-defined exceptions can also be handled as Oracle exceptions, by giving them error codes and
error messages. In this case, the error code can have values between -20999 and -20000.
DECLARE
e_exc1 EXCEPTION;
BEGIN
RAISE_application_error(-20600,'error message');
END IF;
EXCEPTION
dbms_output.put_line(sqlerrm);
END;
begin
for r in c loop
dbms_output.put_line(r.product_name);
end loop;
end;
/
begin
display_categ(15); --hardware
end;
--Create a procedure that modifies the salary of an employee whose id is given as parameter. Also
the percentage of salary growth is given as a parameter.
IS v_salary number;
BEGIN
update employees
set salary=salary*(1+percent/100)
where employee_id=v_employee_id;
END;
begin
modify_salary(176, 10);
end;
--Create a procedure that returns the name and the salary of an employee whose id is given.
IS
BEGIN
END;
--The call:
DECLARE
v_name varchar2(50);
v_salary number;
BEGIN
END;
--Create a procedure that calculates the average salary and returns it. Use a host variable.
is
begin
end;
declare
v_salary number;
begin
avg_salary(v_salary);
dbms_output.put_line(v_salary);
end;
--Write a SP that display for a specific employee his name, hire date and seniority
IS
begin
end;
declare
v_name varchar2(20);
v_seniority number;
begin
display_employee(100,v_name,v_seniority);
end;
--Create a procedure that modifies the salary of all employees if their salary is smaller than the
average salary.
--The procedure receives the average salary and returns it after the updates have been made. It calls
the modify_salary procedure and uses 15 for the percentage.
is
begin
modify_salary(emp.employee_id,15);
end loop;
end;
declare
avg_salary number;
begin
modify_salary2(avg_salary);
end;
: Create a function that returns TRUE/FALSE if the employee whose id is given has the salary
higher/smaller than the average. The function returns NULL if the employee doesn’t exist.
return boolean
is
v_avg_salary number;
v_salary number;
begin
return true;
else
return false;
end if;
EXCEPTION
return NULL;
end;
BEGIN
if check_salary(100) then
dbms_output.put_line('the salary is higher');
else
end if;
end;
calc_income is
v_sum number;
cursor c2(v_dep number) is select sum(salary) suma from employees where department_id = v_dep;
begin
for r in c loop
dbms_output.put_line(r.department_id);
end loop;
end loop;
end;
begin
calc_income;
end;
-- a subprogram that will calculate the total income by department -> nb employees, total income
v_total_salaries number;
begin
exception
end calc_income_dep;
begin
v_total_salaries := 0;
end;
declare
v_nb number;
v_sal_tot number;
begin
p_employee_management.v_total_salaries := 0;
dbms_output.put_line('In the dep with id=90 are ' || v_nb || ' employees that have salary of ' ||
v_sal_tot);
end;
PACKAGE
set serveroutput on
return number;
v_total_salaries number;
end;
v_first_name employees.first_name%type;
begin
return v_first_name;
exception
return null;
end search_employee;
return number is
v_sal number;
begin
select salary into v_sal from employees where first_name = p_first_name and department_id =
p_dep_id;
return v_sal;
exception
return null;
end search_employee;
v_com employees.commission_pct%type;
v_sal number;
begin
select salary, commission_pct into v_sal, v_com from employees where employee_id =
p_employee_id;
return v_sal * (1 + nvl(v_com, 0)); -- nvl -> it will put a value everywhere where is null
exception
return null;
end income_employee;
begin
begin
exception
end calc_income_dep;
begin
v_total_salaries := 0;
end;
declare
v_sal number;
v_nb number;
v_sal_tot number;
v_first_name varchar2(50);
begin
v_first_name := p_employee_management.search_employee(101);
p_employee_management.v_total_salaries := 0;
dbms_output.put_line('In the dep with id=90 are ' || v_nb || ' employees that have salary of ' ||
v_sal_tot);
dbms_output.put_line('In the dep with id=110 are ' || v_nb || ' employees that have salary of ' ||
v_sal_tot);
dbms_output.put_line('In the dep with id=50 are ' || v_nb || ' employees that have salary of ' ||
v_sal_tot);
end;
TRIGGERS
--avoid delete departments
begin
end;
/
--disable trigger
--enagle trigger
--select trigger