This is an
excerpt from the book
PL/SQL: The Definitive Reference by Boobal Ganesan.
The below
approach explains how a function can be declared in an
anonymous block without creating them permanently in the
database. The declaration of the function should be always
at the end of the declare section of an anonymous block
after all the necessary declarations of the anonymous block
are declared.
These
functions cannot be viewed in the data dictionary tables as
these are not permanently saved in the database.
1.
DECLARE
2.
l_n_var1
NUMBER;
3.
l_vc_var2 VARCHAR2(30);
4.
FUNCTION func_conv(
5.
ip_vc_var1 IN VARCHAR2,
6.
op_vc_var2 OUT VARCHAR2)
7.
RETURN NUMBER
8.
IS
9.
BEGIN
10.
op_vc_var2:=upper(ip_vc_var1);
11.
RETURN
LENGTH(ip_vc_var1);
12.
END;
13.
BEGIN
14.
l_n_var1:=func_conv('Hello
World!',l_vc_var2);
15.
dbms_output.put_line('The
Length of the input is '||l_n_var1);
16.
dbms_output.put_line('The
input in upper case is '||l_vc_var2);
17.
END;
18.
/
Result:
The Length of the input is 12
The input in upper case is HELLO
WORLD!
Line no.
|
Description
|
1
|
Start of the declare section of the block.
|
2
|
The variable l_n_var1 of number data type is
declared.
|
3
|
The variable l_vc_var2 of varchar2 data type with
precision 30 is declared.
|
4
|
Start of the declaration of the function func_conv.
|
5
|
The input parameter ip_vc_var1 of varchar2 data type
is declared.
|
6
|
The output parameter op_vc_var2 of varchar2 data
type is declared.
|
7
|
The return statement's data type is declared as
number..
|
8
|
Start of the declare section of the function.
|
9
|
Start of the execution section of the function.
|
10
|
The case of the input parameter ip_vc_var1 is
converted to upper and assigned to the output
parameter op_vc_var2.
|
11
|
The length of the input parameter ip_vc_var1 is
calculated and returned using the RETURN statement.
|
12
|
End of the execution section of the function
|
13
|
Start of the execution section of the block.
|
14
|
The function func_conv is called with the text
'Hello World!' as its input parameter and the local
variable l_vc_var2 is used as its output variable
and the function is assigned to another local
variable l_n_var1.
|
15
|
The local variable l_n_var1's value is printed using
the DBMS_OUTPUT.PUT_LINE procedure.
|
16
|
The local variable l_vc_var2's value is printed
using the DBMS_OUTPUT.PUT_LINE procedure.
|
17,18
|
End of the execution section of the block.
|
|
|
|
Oracle Training from Don Burleson
The best on site
"Oracle
training classes" are just a phone call away! You can get personalized Oracle training by Donald Burleson, right at your shop!
|
|
|
|
|
Burleson is the American Team
Note:
This Oracle
documentation was created as a support and Oracle training reference for use by our
DBA performance tuning consulting professionals.
Feel free to ask questions on our
Oracle forum.
Verify
experience!
Anyone
considering using the services of an Oracle support expert should
independently investigate their credentials and experience, and not rely on
advertisements and self-proclaimed expertise. All legitimate Oracle experts
publish
their Oracle
qualifications.
Errata?
Oracle technology is changing and we
strive to update our BC Oracle support information. If you find an error
or have a suggestion for improving our content, we would appreciate your
feedback. Just
e-mail:
and include the URL for the page.
Copyright © 1996 - 2020
All rights reserved by
Burleson
Oracle ®
is the registered trademark of Oracle Corporation.
|
|