Beruflich Dokumente
Kultur Dokumente
com/academy
Part 1 of this document contains two PL/SQL code samples and questions. Answer the
questions that follow each of the PL/SQL code samples. Attempt to spend no more than 2
minutes on each question.
Part 2 of this document contains practice questions on a few topics which were not included
in the earlier practices.
Copyright 2015, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of
their respective owners.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
Copyright 2015, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
Copyright 2015, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of
their respective owners.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Copyright 2015, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
51
52
53
54
55
56
dbms_sql.v7);
ignore := dbms_sql.execute(conCreCursor);
END LOOP;
dbms_sql.close_cursor(conCreCursor);
END LOOP;
END;
1. In the PL/SQL program above there are a number of errors. How many can you
find?
To help get started here are some of the lines with errors in: 8, 9, 11,12 14, 23.
Some of the errors are logical errors, some are syntax errors and some are typing
mistakes.
2. Could the program have been written in any other way, using any other
mechanisms other than DBMS_SQL?
Copyright 2015, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
PART 2
Some of these questions use a copy of the EMPLOYEES table. Create this copy by
executing:
CREATE TABLE cert_emps AS SELECT * FROM employees;
1.
Enter and run the following code, then answer the questions below.
CREATE OR REPLACE PROCEDURE give_salary_raise
(p_employee_id cert_emps.employee_id%TYPE,
p_salary_incr
cert_emps.salary%TYPE)
IS
v_salary_ok BOOLEAN;
FUNCTION check_max_sal
RETURN BOOLEAN
IS
v_max_sal cert_emps.salary%TYPE;
v_new_sal cert_emps.salary%TYPE;
BEGIN
SELECT max(salary) INTO v_max_sal FROM cert_emps;
SELECT (salary + p_salary_incr) INTO v_new_sal
FROM cert_emps WHERE employee_id = p_employee_id;
IF v_new_sal <= v_max_sal THEN
RETURN TRUE;
ELSE
RETURN FALSE;
END IF;
END check_max_sal;
BEGIN
v_salary_ok := check_max_sal;
-- Line A
IF v_salary_ok THEN
UPDATE cert_emps SET salary = salary + p_salary_incr
WHERE employee_id = p_employee_id;
ELSE
RAISE_APPLICATION_ERROR(-20201, 'New salary exceeds maximum');
END IF;
END give_salary_raise;
Copyright 2015, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of
their respective owners.
A. In your own words, explain what a Local Subprogram is. What is the local
subprogram in this code example?
B. What limit does this local subprogram place on salary increases?
C. Invoke the procedure twice as follows:
BEGIN
give_salary_raise(200, 1000);
give_salary_raise(100, 1000);
END;
Which of these salary increases has failed, and why?
D. Will the following invocation succeed or fail? Why ?
DECLARE
v_bool BOOLEAN;
BEGIN
v_bool := check_max_salary;
END;
E. Imagine that the following line of code is added at Line A:
DBMS_OUTPUT.PUT_LINE('New salary is: ' || v_new_sal);
Will this procedure compile now? Why or why not?
Copyright 2015, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of
their respective owners.
2.
Using USER_ERRORS.
A. What is the purpose of the USER_ERRORS dictionary table?
B. Enter and run the following code, then display all the compiler errors by
SELECTing from USER_ERRORS:
CREATE OR REPLACE PROCEDURE find_max_sal
IS
v_max_sal employees.salary%TYPE;
BEGIN
SELECT MAX(salary) INTO v_max FROM employees;
DBMS_OUTPUT.PUT_LINE('The maximum salary is: ' || v_max);
END;
C. Now try to create a second procedure using the following code:
CREATE OR REPLACE PROCEDURE find_min_sal
IS
v_min_sal employees.salary%TYP;
BEGIN
SELECT MIN(salary) INTO v_min_sal FROM employees;
DBMS_OUTPUT.PUT_LINE('The minimum salary is: ' || v_min_sal);
END;
D. Display compilation errors in all your procedures using a single SELECT
from USER_ERRORS. Note: in addition to FIND_MAX_SAL and
FIND_MIN_SAL, you may see errors from earlier procedures which failed to
compile.
E. Now correct the error in the FIND_MIN_SAL procedure and recompile it
successfully. Then repeat step D to see all your procedure errors. Why is the
earlier error in FIND_MIN_SAL not displayed?
F. Display all the errors in the FIND_MAX_SAL procedure, together with the
incorrect lines of source code. (Hint: join USER_ERRORS and
USER_SOURCE).
Copyright 2015, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of
their respective owners.
3.
Copyright 2015, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of
their respective owners.
10
D. Enter and run the following code to create another row trigger:
CREATE OR REPLACE TRIGGER sal_change_trigg
BEFORE UPDATE OF salary ON cert_emps
FOR EACH ROW
BEGIN
IF :NEW.job_id <> 'AD_PRES'
AND TO_CHAR(SYSDATE, 'DY') = 'SUN'
THEN
RAISE_APPLICATION_ERROR(-20202,
'Only the President can receive a new salary on Sundays');
END IF;
END;
E. Modify your code to use a WHEN clause, and re-create the trigger.
Copyright 2015, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of
their respective owners.
11
Copyright 2015, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of
their respective owners.
12
6. Remote Dependencies.
You cannot test remote dependencies in Application Express because you
would need two distinct databases. Instead, imagine that a second (remote)
database exists. This database contains a procedure called REMOTE_PROC.
You have created a database link, MY_DB_LINK, which points to the remote
database.
In your local APEX database you are using TIMESTAMP mode, and you have
created the following procedure:
CREATE OR REPLACE PROCEDURE local_proc
IS
BEGIN
remote_proc@my_db_link;
END;
A. REMOTE_PROC is recompiled successfully at 11:30, then LOCAL_PROC is
recompiled successfully at 13:15. Then, LOCAL_PROC is invoked twice.
What timestamps are now stored in the two procedures?
B. At 15:00, REMOTE_PROC is again recompiled successfully. Now what
timestamps are stored in the two procedures?
C. At 15:30, LOCAL_PROC is invoked. What will happen, and why? What is
the status of the two procedures now, valid or invalid?
D. At 16:00, LOCAL_PROC is invoked again. What will happen this time, and
why? Now what is the status of the two procedures?
E. In your own words, explain how using SIGNATURE mode would help to
avoid the problem in step C.
F. Which SQL statement must you execute in order to use signature mode? In
which database?
Copyright 2015, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of
their respective owners.