Subido por Erick Vélez Armijo

Tarea 7 Administración DB Erick Vélez

Anuncio
FUNCIONES
1.
CREATE OR REPLACE FUNCTION GET_JOB
(V_ID_TRABAJO JOBS.JOB_ID%TYPE)
RETURN VARCHAR2
IS
V_TITULO_TRABAJO JOBS.JOB_TITLE%TYPE;
BEGIN
SELECT JOB_TITLE
INTO V_TITULO_TRABAJO
FROM JOBS
WHERE JOB_ID = V_ID_TRABAJO;
RETURN (V_TITULO_TRABAJO);
END;
DECLARE
b_title VARCHAR2(35);
BEGIN
b_title := GET_JOB('SA_REP');
DBMS_OUTPUT.PUT_LINE(b_title);
END;
2.
CREATE OR REPLACE FUNCTION GET_ANNUAL_COMP
(V_SALARIO EMPLOYEES.SALARY%TYPE,
V_PCT_COMISION EMPLOYEES.COMMISSION_PCT%TYPE)
RETURN NUMBER
IS
V_ANNUAL_COMP NUMBER;
BEGIN
IF (V_SALARIO IS NULL) THEN
RETURN (0);
ELSIF (V_PCT_COMISION IS NULL) THEN
V_ANNUAL_COMP := (V_SALARIO*12) + (0*V_SALARIO*12);
RETURN (V_ANNUAL_COMP);
ELSIF (V_SALARIO IS NOT NULL AND V_PCT_COMISION IS NOT NULL) THEN
V_ANNUAL_COMP := (V_SALARIO*12) + (V_PCT_COMISION*V_SALARIO*12);
RETURN (V_ANNUAL_COMP);
END IF;
END;
SELECT EMPLOYEE_ID, LAST_NAME, GET_ANNUAL_COMP(SALARY, COMMISSION_PCT)
FROM EMPLOYEES
WHERE DEPARTMENT_ID = 30;
3.
a)
CREATE OR REPLACE FUNCTION VALID_DEPTID
(V_ID_DEPARTAMENTO IN DEPARTMENTS.DEPARTMENT_ID%TYPE)
RETURN BOOLEAN
IS
V_ID_DEPARTAMENTO_V DEPARTMENTS.DEPARTMENT_ID%TYPE := 0;
BEGIN
BEGIN
SELECT COUNT(DEPARTMENT_ID)
INTO V_ID_DEPARTAMENTO_V
FROM DEPARTMENTS
WHERE DEPARTMENT_ID = V_ID_DEPARTAMENTO;
EXCEPTION
WHEN OTHERS THEN
V_ID_DEPARTAMENTO_V := 0;
END;
IF(V_ID_DEPARTAMENTO_V = 0) THEN
RETURN (FALSE);
ELSE
RETURN (TRUE);
END IF;
END VALID_DEPTID;
b)
CREATE OR REPLACE PROCEDURE ADD_EMPLOYEE
(V_ID_EMPLEADO IN EMPLOYEES.EMPLOYEE_ID%TYPE,
V_ NOMBRE IN EMPLOYEES.FIRST_NAME%TYPE,
V_APELLIDO IN EMPLOYEES.LAST_NAME%TYPE,
V_CORREO IN EMPLOYEES.EMAIL%TYPE,
V_CONTRATO IN EMPLOYEES.HIRE_DATE%TYPE,
V_ID_TRABAJO IN EMPLOYEES.JOB_ID%TYPE,
V_SALARIO IN EMPLOYEES.SALARY%TYPE,
V_PCT_COMISION IN EMPLOYEES.COMMISSION_PCT%TYPE,
V_ID_REPRESENTANTE IN EMPLOYEES.MANAGER_ID%TYPE,
V_ID_DEPARTAMENTO IN EMPLOYEES.DEPARTMENT_ID%TYPE)
IS
BEGIN
IF(VALID_DEPTID(V_ID_DEPARTAMENTO) = TRUE ) THEN
INSERT INTO EMPLOYEES (EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL,
HIRE_DATE, JOB_ID, SALARY, COMMISSION_PCT, MANAGER_ID, DEPARTMENT_ID)
VALUES(V_ID_EMPLEADO, V_ NOMBRE V_APELLIDO, V_CORREO, V_CONTRATO,
V_ID_TRABAJO, V_SALARIO, V_PCT_COMISION, V_ID_REPRESENTANTE, V_ID_DEP
ARTAMENTO);
ELSE
DBMS_OUTPUT.PUT_LINE('Departamento Invalido, Intente nuevamente');
END IF;
END ADD_EMPLOYEE;
c)
BEGIN
ADD_EMPLOYEE(EMPLOYEES_SEQ.nextval, 'Jane', 'Harris', 'JHarris@', SYSDATE(),
'SA_REP', 1000, 0, 145, 15);
END;
Departamento Invalido, Intente nuevamente
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.059
d)
BEGIN
ADD_EMPLOYEE(EMPLOYEES_SEQ.nextval, 'Joe', 'Harris', 'JoeHarris@', SYSDATE(),
'SA_REP', 1000, 0, 145, 80);
END;
ORA-02291: integrity constraint (EVELEZ.EMP_MANAGER_FK) violated - parent key not found
ORA-06512: at "EVELEZ.ADD_EMPLOYEE", line 15 ORA-06512: at line 2
4.
CREATE OR REPLACE FUNCTION GET_DAYS_SERVICE
(V_ID_EMPLEADO EMPLOYEES.EMPLOYEE_ID%TYPE)
RETURN NUMBER
IS
V_EMPLEADO_FECHA EMPLOYEES.HIRE_DATE%TYPE;
BEGIN
BEGIN
SELECT HIRE_DATE
INTO V_EMPLEADO_FECHA
FROM EMPLOYEES
WHERE EMPLOYEE_ID = V_ID_EMPLEADO;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('El empleado ingresado no existe');
END;
RETURN (SYSDATE() - V_EMPLEADO_FECHA);
END GET_DAYS_SERVICE;
BEGIN
DBMS_OUTPUT.PUT_LINE(GET_DAYS_SERVICE(999));
END;
El empleado ingresado no existe
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.014
BEGIN
DBMS_OUTPUT.PUT_LINE(GET_DAYS_SERVICE(106));
END;
8537.709814814814814814814814814814814815
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.013
5.
CREATE OR REPLACE FUNCTION GET_JOB_COUNT
(V_ID_EMPLEADO EMPLOYEES.EMPLOYEE_ID%TYPE)
RETURN NUMBER
IS
V_TRABAJOS_CANT NUMBER;
BEGIN
BEGIN
SELECT COUNT(*)
INTO V_TRABAJOS_CANT
FROM (
SELECT JOB_ID FROM EMPLOYEES
WHERE EMPLOYEE_ID = V_ID_EMPLEADO
UNION
SELECT JOB_ID FROM JOB_HISTORY
WHERE EMPLOYEE_ID = V_ID_EMPLEADO
) JBH;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('El empleado ingresado no existe');
END;
RETURN (V_TRABAJOS_CANT);
END GET_JOB_COUNT;
PROCEDIMIENTOS
1.
CREATE OR REPLACE PROCEDURE PR_CAMBIA_CORREO
IS
CURSOR 2B IS
SELECT UPPER(SUBSTR(EMP.FIRST_NAME,1,1)) AS "PN", UPPER(EMP.LAST_NAME) AS "AP
", EMP.EMPLOYEE_ID AS "CE",
CTR.COUNTRY_ID AS "PA",
UPPER(SUBSTR(REG.REGION_NAME,1,3)) AS "NR"
FROM EMPLOYEES EMP, DEPARTMENTS DEP, LOCATIONS LOC, COUNTRIES CTR, REGIONS RE
G
WHERE (EMP.DEPARTMENT_ID = DEP.DEPARTMENT_ID) AND (DEP.LOCATION_ID = LOC.LOCA
TION_ID) AND
(LOC.COUNTRY_ID = CTR.COUNTRY_ID) AND (CTR.REGION_ID = REG.REGION_ID)
;
BEGIN
FOR NEW_EMAIL IN 2B LOOP
UPDATE EMPLOYEES
SET EMAIL = NEW_EMAIL.PN || NEW_EMAIL.AP || '@' || NEW_EMAIL.NR || '.' ||
NEW_EMAIL.PA
WHERE EMPLOYEE_ID = NEW_EMAIL.ID;
COMMIT;
END LOOP;
END;
2.
a)
CREATE OR REPLACE PROCEDURE ADD_JOB
(V_ID_TRABAJO JOBS.JOB_ID%TYPE,
V_TITULO_TRABAJO JOBS.JOB_TITLE%TYPE)
IS
BEGIN
INSERT INTO JOBS (JOB_ID, JOB_TITLE)
VALUES (V_ID_TRABAJO, V_TITULO_TRABAJO);
END ADD_JOB;
b)
BEGIN
ADD_JOB('IT_DBA', 'Database Administrator');
END;
SELECT *
FROM JOBS;
c)
BEGIN
ADD_JOB('ST_MAN', 'Stock Manager');
END;
ORA-00001: unique constraint (EVELEZJOB_ID_PK) violated ORA-06512: at "EVELEZ_JOB", line 6
ORA-06512: at line 2
Esto sucede debido a que el campo que deseamos ingresar está repetido, todos sus valores de la
columna “JOB_ID” deben ser únicos.
3.
a)
CREATE OR REPLACE PROCEDURE UPD_JOB
(V_ID_TRABAJO JOBS.JOB_ID%TYPE,
V_TITULO_TRABAJO JOBS.JOB_TITLE%TYPE)
IS
BEGIN
BEGIN
SELECT JOB_ID
INTO V_EXC
FROM JOBS
WHERE JOB_ID = V_ID_TRABAJO;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('El identificador de trabajo que ingresó, no existe');
END;
UPDATE JOBS
SET JOB_TITLE = V_TITULO_TRABAJO
WHERE JOB_ID = V_ID_TRABAJO;
END UPD_JOB;
b)
BEGIN
UPD_JOB('IT_DBA', 'Data Administrator');
END;
SELECT *
FROM JOBS;
c)
BEGIN
UPD_JOB('IT_WEB', 'Web Master');
END;
El identificador de trabajo que ingresó no está registrado
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.026
4.
a)
CREATE OR REPLACE PROCEDURE DEL_JOB
(V_ID_TRABAJO JOBS.JOB_ID%TYPE)
IS
BEGIN
BEGIN
IF (GET_JOB(V_ID_TRABAJO) != ' ') THEN
DELETE FROM JOBS
WHERE JOB_ID = V_ID_TRABAJO;
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('El identificador de trabajo que ingresó no está
registrado');
END;
END DEL_JOB;
b)
BEGIN
DEL_JOB('IT_DBA');
END;
SELECT *
FROM JOBS;
c)
BEGIN
DEL_JOB('IT_WEB');
END;
El identificador de trabajo que ingresó no está registrado
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.028
5.
a)
CREATE OR REPLACE PROCEDURE GET_EMPLOYEE
(V_ID_EMPLEADO IN EMPLOYEES.EMPLOYEE_ID%TYPE,
V_SALARIO OUT EMPLOYEES.SALARY%TYPE,
V_ID_TRABAJO OUT EMPLOYEES.JOB_ID%TYPE)
IS
BEGIN
BEGIN
SELECT SALARY, ID_TRABAJO
INTO V_SALARIO, V_ID_TRABAJO
FROM EMPLOYEES
WHERE EMPLOYEE_ID = V_ID_EMPLEADO;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('El identificador de trabajo que ingresó no está
registrado);
END;
END GET_EMPLOYEE;
b)
DECLARE
V_SALARIO EMPLOYEES.SALARY%TYPE;
V_ID_TRABAJO EMPLOYEES.JOB_ID%TYPE;
BEGIN
GET_EMPLOYEE(100, V_SALARIO, V_ID_TRABAJO);
DBMS_OUTPUT.PUT_LINE('SALARIO: '||V_SALARIO|| ' ID TRABAJO: '||V_ID_TRABAJO);
END;
SALARIO: 3081.75 ID TRABAJO: AD_PRES
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.017
c)
DECLARE
V_SALARIO EMPLOYEES.SALARY%TYPE;
V_ID_TRABAJO EMPLOYEES.JOB_ID%TYPE;
BEGIN
GET_EMPLOYEE(300, V_SALARY, V_JOB_ID);
DBMS_OUTPUT.PUT_LINE('SALARIO: '||V_SALARIO|| ' ID TRABAJO: '||V_ID_TRABAJO);
END;
El identificador de trabajo que ingresó, no existe
SALARIO: ID TRABAJO:
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.019
Esto sucede debido a que el código del empleado no existe por lo tanto aparece el mensaje que
ingresamos cuando salte una excepción
Descargar