PL-SQL Practice 10-2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5
At a glance
Powered by AI
The key takeaways are how to create a package with multiple procedures that call each other, and how to make procedures private vs public.

You can create a package with multiple procedures by defining the procedures in the package specification and body. The procedures in the body can call each other by name.

If you try to call a private procedure from an anonymous block, it will result in an error since private procedures can only be called from within the package.

Visibility

Local Variables

Global Variables

1. Create a package called hellofrom that contains three public procedures named
proc_1, proc_2

and proc_3. Each of these procedures should use DBMS_OUTPUT.PUT_LINE() to


display the

message “Hello from Proc x” where “x” is 1 or 2 or 3, as appropriate.

CREATE OR REPLACE PACKAGE hellofrom

AS

PROCEDURE proc_1;

PROCEDURE proc_2;

PROCEDURE proc_3;

END hellofrom;

CREATE OR REPLACE PACKAGE BODY hellofrom IS

PROCEDURE proc_1

IS

BEGIN

dbms_output.put_line('Hello from Proc 1');

proc_2;

END proc_1;

PROCEDURE proc_2

IS
BEGIN

dbms_output.put_line('Hello from Proc 2');

proc_3;

END proc_2;

PROCEDURE proc_3

IS

BEGIN

dbms_output.put_line('Hello from Proc 3');

END proc_3;

END hellofrom;

Also, proc_1 should call proc_2 and proc_2 should call proc_3, so you need to include a
reference

to proc_2 from proc_1, and a reference to proc_3 from proc_2.

You will be making changes to this package specification and body, so make sure you
save both

the specification and body in your Application Express session.

2. DESCRIBE the package to check that all three procedures are visible. Then, create
and execute an

anonymous block which displays all three messages with only one procedure call.

DESC hellofrom;

Begin

hellofrom.proc_1;

End;
Modify the hellofrom package specification (not the body) so that only proc_1 is public
and proc_2 and

proc_3 are private. Recreate the package and then DESCRIBE it. What do you see?

CREATE OR REPLACE PACKAGE hellofrom

AS

PROCEDURE proc_1;

END hellofrom;

3. What will happen if you try to run hellofrom.proc_1 now, before you make any
changes to the

body?

Try it.

ORA-04063: package body "US_B902_PLSQL_S07.HELLOFROM" has errors

ORA-06508: PL/SQL: could not find program unit being called:

"US_B902_PLSQL_S07.HELLOFROM"

4. What changes can you make to the package body in order for proc_1 to run
successfully? Make

the changes and recreate the body.

CREATE OR REPLACE PACKAGE BODY hellofrom IS

procedure proc_2;

procedure proc_3;

PROCEDURE proc_1

IS
BEGIN

dbms_output.put_line('Hello from Proc 1');

proc_2;

END proc_1;

PROCEDURE proc_2

IS

BEGIN

dbms_output.put_line('Hello from Proc 2');

proc_3;

END proc_2;

PROCEDURE proc_3

IS

BEGIN

dbms_output.put_line('Hello from Proc 3');

END proc_3;

END hellofrom;

5. Try to call each of the three procedures from anonymous blocks. What happens?
Explain why

some of these calls fail.

ORA-06550: line 2, column 11:

PLS-00302: component 'PROC_2' must be declared

ORA-06550: line 2, column 1:

PL/SQL: Statement ignored

1. begin
2. hellofrom.proc_2;

3. end;

The reason this happens is because the proc_2 and proc_3 procedures are private and
cannot be

called from external calls.

6. Look in USER_PROCEDURES for the hellofrom package. What do you see?

select * from user_procedures where object_name = 'HELLOFROM';

I see one defined procedure for HELLOFROM

7. Also have a look in USER_SOURCE. Unlike USER_PROCEDURES,


USER_SOURCE shows the

entire body, including the private procedures.

select * from user_source where name ='HELLOFROM';

8. Make sure you have saved the hellofrom package body code from question 1. Then
try to drop just

the body. What happens? What do you see when you DESCRIBE helloform?

drop package body hellofrom;

Desc hellofrom;

You might also like