FUNCIONES 1. CREATE OR REPLACE FUNCTION GET_JOB (V_JOB_ID JOBS.JOB_ID%TYPE) RETURN VARCHAR2 IS V_JOB_TITLE JOBS.JOB_TITLE%TYPE; BEGIN SELECT JOB_TITLE INTO V_JOB_TITLE FROM JOBS WHERE JOB_ID = V_JOB_ID; RETURN (V_JOB_TITLE); 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_SALARY EMPLOYEES.SALARY%TYPE, V_COMMISSION_PCT EMPLOYEES.COMMISSION_PCT%TYPE) RETURN NUMBER IS V_ANNUAL_COMP NUMBER; BEGIN IF (V_SALARY IS NULL) THEN RETURN (0); ELSIF (V_COMMISSION_PCT IS NULL) THEN V_ANNUAL_COMP := (V_SALARY*12) + (0*V_SALARY*12); RETURN (V_ANNUAL_COMP); ELSIF (V_SALARY IS NOT NULL AND V_COMMISSION_PCT IS NOT NULL) THEN V_ANNUAL_COMP := (V_SALARY*12) + (V_COMMISSION_PCT*V_SALARY*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_DEPARTMENT_ID IN DEPARTMENTS.DEPARTMENT_ID%TYPE) RETURN BOOLEAN IS V_DEPARTMENT_ID_V DEPARTMENTS.DEPARTMENT_ID%TYPE := 0; BEGIN BEGIN SELECT COUNT(DEPARTMENT_ID) INTO V_DEPARTMENT_ID_V FROM DEPARTMENTS WHERE DEPARTMENT_ID = V_DEPARTMENT_ID; EXCEPTION WHEN OTHERS THEN V_DEPARTMENT_ID_V := 0; END; IF(V_DEPARTMENT_ID_V = 0) THEN RETURN (FALSE); ELSE RETURN (TRUE); END IF; END VALID_DEPTID; b) CREATE OR REPLACE PROCEDURE ADD_EMPLOYEE (V_EMPLOYEE_ID IN EMPLOYEES.EMPLOYEE_ID%TYPE, V_FIRST_NAME IN EMPLOYEES.FIRST_NAME%TYPE, V_LAST_NAME IN EMPLOYEES.LAST_NAME%TYPE, V_EMAIL IN EMPLOYEES.EMAIL%TYPE, V_HIRE_DATE IN EMPLOYEES.HIRE_DATE%TYPE, V_JOB_ID IN EMPLOYEES.JOB_ID%TYPE, V_SALARY IN EMPLOYEES.SALARY%TYPE, V_COMMISSION_PCT IN EMPLOYEES.COMMISSION_PCT%TYPE, V_MANAGER_ID IN EMPLOYEES.MANAGER_ID%TYPE, V_DEPARTMENT_ID IN EMPLOYEES.DEPARTMENT_ID%TYPE) IS BEGIN IF(VALID_DEPTID(V_DEPARTMENT_ID) = 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_EMPLOYEE_ID, V_FIRST_NAME, V_LAST_NAME, V_EMAIL, V_HIRE_DATE, V_JOB_ID, V_SALARY, V_COMMISSION_PCT, V_MANAGER_ID, V_DEPARTMENT_ID); 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 (HRAMIREZ.EMP_MANAGER_FK) violated - parent key not found ORA-06512: at "HRAMIREZ.ADD_EMPLOYEE", line 15 ORA-06512: at line 2 4. CREATE OR REPLACE FUNCTION GET_DAYS_SERVICE (V_EMPLOYEE_ID EMPLOYEES.EMPLOYEE_ID%TYPE) RETURN NUMBER IS V_DATE_EMP EMPLOYEES.HIRE_DATE%TYPE; BEGIN BEGIN SELECT HIRE_DATE INTO V_DATE_EMP FROM EMPLOYEES WHERE EMPLOYEE_ID = V_EMPLOYEE_ID; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('El código del empleado ingresado no existe'); END; RETURN (SYSDATE() - V_DATE_EMP); END GET_DAYS_SERVICE; BEGIN DBMS_OUTPUT.PUT_LINE(GET_DAYS_SERVICE(999)); END; El código del empleado ingresado no existe PL/SQL procedure successfully completed. Elapsed: 00:00:00.014 BEGIN DBMS_OUTPUT.PUT_LINE(GET_DAYS_SERVICE(107)); END; 8537.709814814814814814814814814814814815 PL/SQL procedure successfully completed. Elapsed: 00:00:00.013 5. CREATE OR REPLACE FUNCTION GET_JOB_COUNT (V_EMPLOYEE_ID EMPLOYEES.EMPLOYEE_ID%TYPE) RETURN NUMBER IS V_CANT_JOBS NUMBER; BEGIN BEGIN SELECT COUNT(*) INTO V_CANT_JOBS FROM ( SELECT JOB_ID FROM EMPLOYEES WHERE EMPLOYEE_ID = V_EMPLOYEE_ID UNION SELECT JOB_ID FROM JOB_HISTORY WHERE EMPLOYEE_ID = V_EMPLOYEE_ID ) JBH; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('El código del empleado ingresado no existe'); END; RETURN (V_CANT_JOBS); END GET_JOB_COUNT; PROCEDIMIENTOS 1. CREATE OR REPLACE PROCEDURE PR_CAMBIA_CORREO IS CURSOR C1 IS SELECT UPPER(SUBSTR(EMP.FIRST_NAME,1,1)) AS "FN", UPPER(EMP.LAST_NAME) AS "LN A", EMP.EMPLOYEE_ID AS "ID", CTR.COUNTRY_ID AS "CI", UPPER(SUBSTR(REG.REGION_NAME,1,3)) AS "RN" 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 C1 LOOP UPDATE EMPLOYEES SET EMAIL = NEW_EMAIL.FN || NEW_EMAIL.LNA || '@' || NEW_EMAIL.RN || '.' | | NEW_EMAIL.CI WHERE EMPLOYEE_ID = NEW_EMAIL.ID; COMMIT; END LOOP; END; 2. a) CREATE OR REPLACE PROCEDURE ADD_JOB (V_JOB_ID JOBS.JOB_ID%TYPE, V_JOB_TITLE JOBS.JOB_TITLE%TYPE) IS BEGIN INSERT INTO JOBS (JOB_ID, JOB_TITLE) VALUES (V_JOB_ID, V_JOB_TITLE); 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 (EVELEZ.JOB_ID_PK) violated ORA-06512: at "HRAMIREZ.ADD_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_JOB_ID JOBS.JOB_ID%TYPE, V_JOB_TITLE JOBS.JOB_TITLE%TYPE) IS BEGIN BEGIN SELECT JOB_ID INTO V_EXC FROM JOBS WHERE JOB_ID = V_JOB_ID; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('El identificador de trabajo que ingresó, no existe'); END; UPDATE JOBS SET JOB_TITLE = V_JOB_TITLE WHERE JOB_ID = V_JOB_ID; 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 existe PL/SQL procedure successfully completed. Elapsed: 00:00:00.026 4. a) CREATE OR REPLACE PROCEDURE DEL_JOB (V_JOB_ID JOBS.JOB_ID%TYPE) IS BEGIN BEGIN IF (GET_JOB(V_JOB_ID) != ' ') THEN DELETE FROM JOBS WHERE JOB_ID = V_JOB_ID; END IF; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('El identificador de trabajo que ingresó, no existe'); 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 existe PL/SQL procedure successfully completed. Elapsed: 00:00:00.028 5. a) CREATE OR REPLACE PROCEDURE GET_EMPLOYEE (V_EMPLOYEE_ID IN EMPLOYEES.EMPLOYEE_ID%TYPE, V_SALARY OUT EMPLOYEES.SALARY%TYPE, V_JOB_ID OUT EMPLOYEES.JOB_ID%TYPE) IS BEGIN BEGIN SELECT SALARY, JOB_ID INTO V_SALARY, V_JOB_ID FROM EMPLOYEES WHERE EMPLOYEE_ID = V_EMPLOYEE_ID; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('El identificador de trabajo que ingresó, no existe'); END; END GET_EMPLOYEE; b) DECLARE V_SALARY EMPLOYEES.SALARY%TYPE; V_JOB_ID EMPLOYEES.JOB_ID%TYPE; BEGIN GET_EMPLOYEE(100, V_SALARY, V_JOB_ID); DBMS_OUTPUT.PUT_LINE('SALARIO: '||V_SALARY|| ' ID TRABAJO: '||V_JOB_ID); END; SALARIO: 3081.75 ID TRABAJO: AD_PRES PL/SQL procedure successfully completed. Elapsed: 00:00:00.017 c) DECLARE V_SALARY EMPLOYEES.SALARY%TYPE; V_JOB_ID EMPLOYEES.JOB_ID%TYPE; BEGIN GET_EMPLOYEE(300, V_SALARY, V_JOB_ID); DBMS_OUTPUT.PUT_LINE('SALARIO: '||V_SALARY|| ' ID TRABAJO: '||V_JOB_ID); 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