declare
v_j_id employees.job_id%type;
begin
select job_id into v_j_id from employees where department_id=80;
exception
when too_many_rows then dbms_output.put_line('selectul returneaza prea multe randuri! fa un cursor!');
end;


begin
insert into departments (department_id,department_name,manager_id,location_id)
values(50,'a new department',100,1500);
dbms_output.put_line('the new department was inserted');
exception
when DUP_VAL_ON_INDEX then dbms_output.put_line('duplicat!');
when others then dbms_output.put_line('o alta eroare!');
end;


declare
v_language_id wf_languages.language_id%type;
v_language_name wf_languages.language_name%type;
e_null_not_allowed exception;
pragma exception_ginit(e_null_not_allowed,-01400);
begin
select language_id,language_name into v_language_id,v_language_name
from wf_languages
where lower(language_name) like 'al%';
insert into wf_languages(language_id,language_name) values(80,null);
exception
when no_data_found then dbms_output.put_line('dati un alt nume pentru limba de cautat!');
when too_many_rows then dbms_output.put_line('sunt mai multe limbi care incep cum ati vrut, folositi un cursor');
when dup_val_on_index then dbms_output.put_line('incercati sa inserati ceva ce exista deja!');
when e_null_not_allowed then dbms_output.put_line('nu poti insera null!');
end;


declare
v_dept_id excep_emps.department_id%type;
v_count number;
begin
v_dept_id:=10;
select count(*) into v_count from excep_emps
where department_id=v_dept_id;
dbms_output.put_line('there are '||v_count||' employees');
delete from excep_emps where department_id=v_dept_id;
dbms_output.put_line(sql%rowcount||' employees were deleted');
rollback;
end;

declare
v_dept_id excep_emps.department_id%type;
v_count number;
return_zero exception;
no_rows_deleted exception;
begin
v_dept_id:=40;
select count(*) into v_count from excep_emps
where department_id=v_dept_id;
if v_count=0 then raise return_zero;
end if;
dbms_output.put_line('there are '||v_count||' employees');
delete from excep_emps where department_id=v_dept_id;
if sql%notfound then raise no_rows_deleted;
end if;
dbms_output.put_line(sql%rowcount||' employees were deleted');
exception
when return_zero then dbms_output.put_line('nu am un astfel de departament!');
rollback;
end;

declare
v_dept_id excep_emps.department_id%type;
v_count number;
return_zero exception;
no_rows_deleted exception;
begin
v_dept_id:=10;
select count(*) into v_count from excep_emps
where department_id=v_dept_id;
if v_count=0 then raise return_zero;
end if;
dbms_output.put_line('there are '||v_count||' employees');
delete from excep_emps where department_id=v_dept_id;
if sql%notfound then raise no_rows_deleted;
end if;
dbms_output.put_line(sql%rowcount||' employees were deleted');
exception
when return_zero then dbms_output.put_line('nu am un astfel de departament!');
when no_rows_deleted then dbms_output.put_line('nu s-a sters nimic din dept respectiv');
rollback;
end;

declare
v_dept_id excep_emps.department_id%type;
v_count number;
begin
v_dept_id:=40;
select count(*) into v_count from excep_emps
where department_id=v_dept_id;
if v_count=0 then raise_application_error(-20204,'nu exista angajati!');
end if;
dbms_output.put_line('there are '||v_count||' employees');
delete from excep_emps where department_id=v_dept_id;
dbms_output.put_line(sql%rowcount||' employees were deleted');
rollback;
end;

DECLARE
v_country_name wf_countries.country_name%TYPE;
v_currency_code wf_countries.currency_code%TYPE;
BEGIN
DECLARE
e_no_currency EXCEPTION;
BEGIN
SELECT country_name, currency_code
INTO v_country_name, v_currency_code
FROM wf_countries
WHERE country_id = 5; -- repeat with 672
IF v_currency_code = 'NONE' THEN
RAISE e_no_currency;
END IF;


END;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('This country does not exist');
WHEN e_no_currency THEN
DBMS_OUTPUT.PUT_LINE('This country exists but has no currency');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Another type of error occurred');
END; 

create or replace procedure name_change is
begin
update employees_dup
set first_name='Susan'
where department_id=80;
end;

create or replace procedure pay_raise 
is
begin
update employees_dup
set salary=30000;
end;

create or replace procedure get_country_info (p_c_id number)
is
v_c_n wf_countries.country_name%type;
v_c_c wf_countries.capitol%type;
begin
select country_name,capitol into v_c_n,v_c_c
from wf_countries
where country_id=p_c_id;
dbms_output.put_line(v_c_n||' '||v_c_c);
exception 
end;


create or replace procedure get_country_info (p_c_id number)
is
v_c_n wf_countries.country_name%type;
v_c_c wf_countries.capitol%type;
begin
select country_name,capitol into v_c_n,v_c_c
from wf_countries
where country_id=p_c_id;
dbms_output.put_line(v_c_n||' '||v_c_c);
exception
when no_data_found then dbms_output.put_line('nu se gaseste!');
end;

create or replace procedure find_area_pop(p_c_id number,p_c_n out varchar2,p_c_p out varchar2)
is
begin
select country_name,population into p_c_n, p_c_p from wf_countries
where country_id=p_c_id;
end;

declare
v1 wf_countries.country_name%type;
v2 wf_countries.population%type;
begin
find_area_pop(2,v1,v2);
dbms_output.put_line(v1||' '||v2);
end;


create or replace procedure find_area_pop(p_c_id number,p_c_n out varchar2,p_c_p out number,p_d out number)
is
begin
select country_name,population,population/area into p_c_n, p_c_p,p_d from wf_countries
where country_id=p_c_id;
end;

declare
v1 wf_countries.country_name%type;
v2 wf_countries.population%type;
v3 wf_countries.population%type;
begin
find_area_pop(2,v1,v2,v3);
dbms_output.put_line(v1||' '||v2||' '||v3);
end;

create or replace function devide(a number,b number)
return number
as
v number;
begin
v:=round(a/b,2);
return(v);
end;

select divide(50,3) from dual

create or replace function devide(a number,b number)
return number
as
v number;
begin
v:=round(a/b,2);
return(v);
exception
when zero_divide then return(0);

end;
