Output:
SQL> call proc();
hello world
Ex2:
CREATE OR REPLACE PROC(a in number,b in number) IS
BEGIN
dbms_output.put_line('a = ' || a || ' b = ' || b);
END PROC;
Output:
SQL> call proc(5,6);
a = 5 b = 6
Ex3:
CREATE OR REPLACE FUNCTION FUN
RETURN VARCHAR IS
BEGIN
return 'hello
world';
END FUN;
Output:
SQL> variable v varchar(20)
SQL> call fun() into :v;
SQL> print v
hello world
CALL BY REFERENCE AND CALL BY VALUE
4
In parameters by default call by reference where as out and
in out call by value.
5
When parameter passed by reference, a pointer to the actual
parameter is passed to the corresponding formal parameter.
6
When parameter passed by value it copies the value of the actual
parameter to the formal parameter.
7
Call by reference is faster than the call by value because it
avoids the copying.
SUBPROGRAMS OVERLOADING
1
Possible with different number of parameters.
2
Possible with different types of data.
3
Possible with same type with objects.
4
Can not be possible with different types of modes.
5
We can overload local subprograms also.
Ex:
SQL> create or replace type t1 as object(a
number);/
SQL> create or replace type t1 as object(a
number);/
DECLARE
i t1 := t1(5);
j t2 := t2(5);
PROCEDURE P(m t1) IS
BEGIN
dbms_output.put_line('a = ' || m.a);
END P;
PROCEDURE P(n t2) IS
BEGIN
dbms_output.put_line('b = ' || n.b);
END P;
PROCEDURE PRODUCT(a number,b number) IS
BEGIN
dbms_output.put_line('Product of a,b =
' || a * b);
END PRODUCT;
PROCEDURE PRODUCT(a number,b number,c
number) IS
BEGIN
dbms_output.put_line('Product of a,b =
' || a * b * c);
END PRODUCT;
BEGIN
p(i);
p(j);
product(4,5);
product(4,5,6);
END;
Output:
a = 5
b = 5
Product of a,b = 20
Product of a,b = 120
BENEFITS OF OVERLOADING
Ø Supporting many data combinations
Ø Fitting the program to the user.
RESTRICTIONS ON OVERLOADING
1
Overloaded programs with parameter lists that differ only by name
must be called using named notation.
2
The parameter list of overloaded programs must differ by more than
parameter mode.
3
All of the overloaded programs must be defined within the same PL/SQL scope or block.
4
Overloaded functions must differ by more than their return type.
IMPORTANT POINTS ABOUT SUBPROGRAMS
8
When a stored subprogram is created, it is stored in the data
dictionary.
9
The subprogram is stored in compile form which is known as p-code
in addition to the source text.
10 The p-code has all of the references
in the subprogram evaluated, and the source code is translated into a form that
is easily readable by PL/SQL
engine.
11 When the subprogram is called, the
p-code is read from the disk, if necessary, and executed.
12 Once it reads from the disk, the
p-code is stored in the shared pool portion of the system global area (SGA), where it can be accessed by multiple
users as needed.
13 Like all of the contents of the shared
pool, p-code is aged out of the shared pool according to a least recently used (LRU) algorithm.
14 Subprograms can be local.
15 Local subprograms must be declared in
the declarative section of PL/SQL
block and called from the executable section.
16 Subprograms can not have the
declarative section separately.
17 Stored subprograms can have local
subprograms;
18 Local subprograms also can have local
subprograms.
19 If the subprogram contains a variable with
the same name as the column name of the table then use the dot method to
differentiate (subprogram_name.sal).
20 Subprograms can be invalidated.
PROCEDURES V FUNCTIONS
13 Procedures may return through out and
in out parameters where as function must return.
14 Procedures can not have return clause
where as functions must.
15 We can use call statement directly for
executing procedure where as we need to declare a variable in case of
functions.
16 Functions can use in select statements
where as procedures can not.
17 Functions can call from reports
environment where as procedures can not.
18 We can use exec for executing
procedures where as functions can not.
19 Function can be used in dbms_output
where as procedure can not.
20 Procedure call is a standalone
executable statement where as function call is a part of an executable
statement.
STORED V LOCAL SUBPROGRAMS
1
The stored subprogram is stored in compiled p-code in the database,
when the procedure is called it does not have to be compiled.
The local subprogram is compiled as part of its containing block. If the
containing
block is anonymous and is run multiple times, the subprogram has to be
compiled
each time.
1
Stored subprograms can be called from any block submitted by a user
who has execute privileges on the subprogram.
Local subprograms can be called only from the block containing the
subprogram.
2
By keeping the stored subprogram code separate from the calling
block, the calling block is shorter and easier to understand.
The local subprogram and the calling block are one and the same, which
can lead to
part confusion. If a change to the calling block is made, the subprogram
will be
recompiled as of the recompilation of the containing block.
3
The compiled p-code can be pinned in the shared pool using the
DBMS_SHARED_POOL Package. This can improve performance.
Local subprograms cannot be pinned in the shared pool by themselves.
4
Stand alone stored subprograms can not be overloaded, but packaged
subprograms can be overloaded within the same package.
5
Local subprograms can be overloaded within the same block.
Ex1:
CREATE OR REPLACE PROCEDURE P IS
BEGIN
dbms_output.put_line('Stored subprogram');
END;
Output:
SQL> exec p
Stored subprogram
Ex2:
DECLARE
PROCEDURE P IS
BEGIN
dbms_output.put_line('Local
subprogram');
END;
BEGIN
p;
END;
Output:
Local
subprogram
COMPILING SUBPROGRAMS
6
SQL> Alter procedure P1 compile;
7
SQL> Alter function F1 compile;
SUBPROGRAMS DEPENDECIES
8
A stored subprogram is marked as invalid in the data dictionary if
it has compile errors.
9
A stored subprogram can also become invalid if a DDL operation is
performed on one of its dependent objects.
10 If a subprogram is invalidated, the PL/SQL engine will automatically attempt to
recompile in the next time it is called.
11 If we have two procedures like P1 and
P2 in which P1 depends on P2. If we compile P2 then P1 is invalidated.
SUBPROGRAMS DEPENDENCIES IN REMOTE DATABASES
1
We will call remote subprogram using connect string like P1@ORACLE;
2
If we have two procedures like P1 and P2 in which P1 depends on P2
but P2 was in remote database. If we compile P2 it will not invalidate P1
immediately because the data dictionary does not track remote dependencies.
3
Instead the validity of remote objects is checked at runtime. When
P1 is called, the remote data dictionary is queried to determine the status of
P2.
4
P1 and P2 are compared to see it P1 needs to be recompiled, there
are two different methods of comparision
ü Timestamp Model
ü Signature Model
TIMESTAMP MODEL
Ø This is the default model used by oracle.
Ø With this model, the timestamps of the last modifications of the
two objects are
compared.
Ø The last_ddl_time field of user_objects contains the
timestamp.
Ø If the base object has a newer timestamp than the dependent object,
the
dependent object will be recompiled.
ISSUES WITH THIS MODEL
1
If the objects are in different time zones, the comparison is
invalid.
2
When P1 is in a client side PL/SQL engine such as oracle forms, in this case it may not possible to
recompile P1, because the source for it may not be included with the forms.
SIGNATURE MODEL
1
When a procedure is created, a signature is stored in the data
dictionary in addition to the p-code.
2
The signature encodes the types and order of the parametes.
3
When P1 is compiled the first time, the signature of P2 is
included. Thus, P1 only needs to recompiled when the signature of P2 changes.
4
In order to use the signature model, the parameter REMOTE_DEPENDENCIES_MODE must be set to SIGNATURE. This is a parameter in the database
initialization file.
THREE WAYS OF SETTING THIS MODE
1
Add the line REMOTE_DEPENDENCIES_MODE=SIGNATURE to the database initialization file.
The next time the database is started, the mode will be set to SIGNATURE for all sessions.
2
Alter system set remote_dependencies_mode = signature;
This will affect the entire database (all sessions) from the time the
statement is
issued. You must have the ALTER SYSTEM privilege to issue this command.
1
Alter session set remote_dependencies_mode = signature;
This will only affect your session
ISSUES WITH THIS MODEL
2
Signatures don’t get modified if the default values of formal
parameters are
changed.
3
Suppose P2 has a default value for one of its parameters, and P1 is
using this
default value. If the default in the specification for P2 is changed, P1
will not be
recompiled
by default. The old value for the default parameter will still be used until
P1 is
manually recompiled.
2
If P1 is calling a packaged procedure P2, and a new overloaded
version of P2 is added to the remote package, the signature is not changed. P1
will still use the old version(not the new overloaded one) until P1 is
recompiled manually.
FORWARD DECLERATION
Before going to use the procedure in any other subprogram
or other block , you must declare the prototype of the procedure in declarative
section.
Ex1:
DECLARE
PROCEDURE P1 IS
BEGIN
dbms_output.put_line('From procedure
p1');
p2;
END P1;
PROCEDURE P2 IS
BEGIN
dbms_output.put_line('From procedure
p2');
p3;
END P2;
PROCEDURE P3 IS
BEGIN
dbms_output.put_line('From procedure
p3');
END P3;
BEGIN
p1;
END;
Output:
p2;
*
ERROR at line 5:
ORA-06550: line 5, column 1:
PLS-00313: 'P2' not declared in this
scope
ORA-06550: line 5, column 1:
PL/SQL: Statement ignored
ORA-06550: line 10, column 1:
PLS-00313: 'P3' not declared in this
scope
ORA-06550: line 10, column 1:
PL/SQL: Statement ignored
Ex2:
DECLARE
PROCEDURE P2;
--
forward declaration
PROCEDURE P3;
PROCEDURE P1 IS
BEGIN
dbms_output.put_line('From procedure
p1');
p2;
END P1;
PROCEDURE P2 IS
BEGIN
dbms_output.put_line('From procedure
p2');
p3;
END P2;
PROCEDURE P3 IS
BEGIN
dbms_output.put_line('From procedure
p3');
END P3;
BEGIN
p1;
END;
Output:
From procedure p1
From procedure p2
From procedure p3
PRIVILEGES AND STORED SUBPROGRAMS
EXECUTE PREVILEGE
4
For stored subprograms and packages the relevant privilege is EXECUTE.
5
If user A had the procedure called emp_proc then user A grants
execute privilege on procedure to user B with the following command.
SQL> Grant execute on emp_proc to user B.
6
Then user B can run the procedure by issuing
SQL> Exec user A.emp_proc
userA created the following procedure
CREATE OR REPLACE PROCEDURE P IS
cursor is select *from student1;
BEGIN
for v in c loop
insert into student2
values(v.no,v.name,v.marks);
end loop;
END P;
userA granted execute privilege to userB using
SQL> grant execute on p to userB
Then userB executed the procedure
SQL> Exec userA.p
If suppose userB also having student2 table then which
table will populate whether userA’s or userB’s.
The answer is userA’s student2 table only because by
default the procedure will execute under the privlige set of its owner.
The above procedure is known as definer’s procedure.
HOW TO POPULATE USER B’s TABLE
7
Oracle introduces Invoker’s and Definer’s rights.
8
By default it will use the definer’s rights.
9
An invoker’s rights routine can be created by using AUTHID clause to populate the
userB’s table.
10 It is valid for stand-alone
subprograms, package specifications, and object type
specifications only.
userA created the following procedure
CREATE OR REPLACE PROCEDURE P
AUTHID CURRENT_USER IS
cursor is select *from student1;
BEGIN
for v in c loop
insert into student2
values(v.no,v.name,v.marks);
end loop;
END P;
Then grant execute privilege on p to userB.
Executing the procedure by userB, which populates userB’s
table.
The above procedure is called invoker’s procedure.
Instead of current_user of authid clause, if you use
definer then it will be called definer’ procedure.
STORED SUBPROGRAMS AND ROLES
we have two users saketh and sudha in which saketh has
student table and sudha does not.
Sudha is going to create a procedure based on student table
owned by saketh. Before doing this saketh must grant the permissions on this
table to sudha.
SQL> conn saketh/saketh
SQL> grant all on student to sudha;
then sudha can create procedure
SQL> conn sudha/sudha
CREATE OR REPLACE PROCEDURE P IS
cursor c is select *from saketh.student;
BEGIN
for v in c loop
dbms_output.put_line(‘No = ‘ ||
v.no);
end loop;
END P;
here procedure will be created.
If the same privilege was granted through a role it wont
create the procedure.
Examine the following code
SQL> conn saketh/saketh
SQL> create role saketh_role;
SQL> grant all on student to
saketh_role;
SQL> grant saketh_role to sudha;
then conn sudha/sudha
CREATE OR REPLACE PROCEDURE P IS
cursor c is select *from saketh.student;
BEGIN
for v in c loop
dbms_output.put_line(‘No = ‘ ||
v.no);
end loop;
END P;
The above code will raise error instead of creating
procedure .
This is because of early binding which PL/SQL uses by default in which references
are evaluated in compile time but when you are using a role this will affect
immediately.
ISSUES WITH INVOKER’S RIGHTS
1
In an invoker’s rights routine, external references in SQL statements will be resolved using the
caller’s privilege set.
2
But references in PL/SQL
statements are still resolved under the owner’s privilege set.
TRIGGERS, VIEWS AND INVOKER’S RIGHTS
1
A database trigger will always be executed with definer’s rights
and will execute under the privilege set of the schema that owns the triggering
table.
2
This is also true for PL/SQL
function that is called from a view. In this case, the function will execute
under the privilege set of the view’s owner.
No comments:
Post a Comment