0% found this document useful (0 votes)
45 views7 pages

Tutor I A Ramirez

The document discusses stored procedures and functions in Oracle SQL. It includes examples of creating procedures to add, update, delete records from a jobs table. It also includes procedures to get employee data and calculate annual compensation. Functions are created to return job titles, annual compensation amounts, and validate department IDs.

Uploaded by

Kevin Garcia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views7 pages

Tutor I A Ramirez

The document discusses stored procedures and functions in Oracle SQL. It includes examples of creating procedures to add, update, delete records from a jobs table. It also includes procedures to get employee data and calculate annual compensation. Functions are created to return job titles, annual compensation amounts, and validate department IDs.

Uploaded by

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

Nombre: Christian Alejandro Ramirez Castro

Materia: Administración de bases de datos

ADMINISTRACION DE BASE DE DATOS - GESTION EN EL AULA

PROCEDIMIENTOS ALMACENADOS
1.
a)
create or replace procedure add_job (v_job_id in jobs.job_id%type,
v_job_title in jobs.job_title%type)
is
begin

insert into jobs(job_id, job_title)


values (v_job_id, v_job_title);

end;

b)
begin
add_job('it_dba', 'database administrator');

end;

select * from jobs;


A) J

begin
add_job('st_man', 'stock manager');
end;

2.
A)

CREATE OR REPLACE PROCEDURE UPD_JOB (V_JOB_ID IN JOBS.JOB_ID%TYPE,


v_job_title in jobs.job_title%type)
is
v_cr number(2);
error_cr exception;
begin
select count(*) into v_cr from jobs where job_id=v_job_id;
if (v_cr=0) then
raise error_cr;
else
update jobs set job_title=v_job_title where job_id=v_job_id;
end if;
exception
when error_cr then
raise_application_error(-20001,'no se pudo actualizar');
end;

B)

declare
v_job_id jobs.job_id%type := 'it_web';
v_job_title jobs.job_title%type := 'master web';
begin
upd_job(v_job_id, v_job_title);
end;

select * from jobs where job_id='it_web';

3.

A)
create or replace procedure del_job(v_job_id in jobs.job_id%type)
is
elim exception;
var_cr number(2);
begin
select count(*) into var_cr from jobs where job_id=v_job_id;
if(var_cr=0) then
raise elim;
else
delete from jobs where job_id=v_job_id;
end if;
exception
when elim then
raise_application_error(-20000 ,'no se pudo eliminar el trabajo');
end;
B)
declare
v_job_id jobs.job_id%type := 'it_dba';
begin
del_job(v_job_id);
end;

select * from jobs where job_id='it_dba';

c)
declare
v_job_id jobs.job_id%type := 'it_web';
begin
del_job(v_job_id);
end;

4.

create or replace procedure get_employee(v_emp_id in


employees.employee_id%type)
is
v_salario employees.salary%type;
v_job_id employees.job_id%type;
begin
select salary, job_id into v_salario, v_job_id from employees where
employee_id=v_emp_id;
dbms_output.put_line('salario: '||v_salario);
dbms_output.put_line('identificador trabajo: '||v_job_id);
end;

a)

declare
v_emp_id employees.employee_id%type :=198;
begin
get_employee(v_emp_id);
end;

B)
create or replace procedure get_employee(v_emp_id in
employees.employee_id%type,
v_salario out employees.salary%type, v_job_id out
employees.job_id%type)
is
begin
select salary, job_id into v_salario, v_job_id from employees where
employee_id=v_emp_id;
end;
declare
v_emp_id employees.employee_id%type :=120;
v_salario employees.salary%type;
v_job_id employees.job_id%type;
begin
get_employee(v_emp_id, v_salario, v_job_id);
dbms_output.put_line('salario: '||v_salario);
dbms_output.put_line('identifador trabajo: '||v_job_id);
end;

C)

declare
v_emp_id employees.employee_id%type :=300;
v_salario employees.salary%type;
v_job_id employees.job_id%type;
begin
get_employee(v_emp_id, v_salario, v_job_id);
dbms_output.put_line('salario: '||v_salario);
dbms_output.put_line('identifador trabajo: '||v_job_id);
end;

Funciones
1.
a)
create or replace function get_job(v_job_id in jobs.job_id%type)
return varchar2
is
v_title jobs.job_title%type;
begin
select job_title into v_title from jobs where v_job_id=job_id;

return v_title;
end;

b)
declare
b_title varchar2(35);
begin
b_title:=get_job('sa_rep');
dbms_output.put_line('cargo: '||b_title);

end;

2.
a)
create or replace function get_annual_comp(v_salary in
employees.salary%type, v_commission in employees.commission_pct%type)
return number
is
begin
return
(nvl(v_salary,0)*12)+(nvl(v_commission,0)*nvl(v_salary,0)*12);
end;

b)
select employee_id, last_name, get_annual_comp(salary,commission_pct)
as comision_anual
from employees
where department_id=30;
3.
a)
create or replace function valid_deptid(v_dep_id in
departments.department_id%type)
return boolean
is
v_validate integer;
begin
select 1
into v_validate
from departments
where department_id = v_dep_id;
return true;
exception
when no_data_found then
return false;

end;

You might also like