Package in Oracle (facing error while calling it) - oracle

I am using Oracle 11G database. I have created a package as described below:
create or replace package forward_emp is
Function emp_sal_avg return number;
Procedure emp_new_sal;
End forward_emp;
/
package created.
Then I created the package body:
create or replace package body forward_emp is
Function emp_sal_avg return number is
avg_sal emp.salary%type;
Begin
Select avg (salary) into avg_sal
from emp;
Return avg_sal;
End emp_sal_avg;
Procedure emp_new_sal is
Begin
Insert into Emp (salary) values (emp_sal_avg);
End emp_new_sal;
End forward_emp;
/
Package body created.
Now when I tried calling the package its is showing error.
Call forward_emp.emp_new_sal;
ORA- 06576: not a valid function or procedure.
Please help. Not been able to understand the problem.

CALL is a SQL statement, not a PL/SQL statement, not a SQLPLUS command. CALL can be used in SQLPLUS directly, but, when you execute a function, the returning result should be stored somewhere, hence into clause of the call statement is needed. And parenthesis, even if a function or a stored procedure has no arguments, kind of mandatory.
Here is an example:
SQL> create or replace package pkg as
2 function f1 return number;
3 procedure p1;
4 end;
5 /
Package created.
SQL> create or replace package body pkg as
2 function f1 return number is
3 begin
4 return 12345;
5 end;
6 procedure p1 is
7 begin
8 null; -- does nothing
9 end;
10 end;
11 /
Package body created.
Now let's execute those procedure and function defined withing the PKG package:
-- variable, which is going to store result the function returns
SQL> variable f_res number;
-- executing the F1 function
SQL> call pkg.f1() into :f_res;
Call completed.
-- print the result
SQL> print f_res;
F_RES
----------
12345
-- executing the P1 procedure
SQL> call pkg.p1();
Call completed.
This what happens if we simply omit parenthesis or do not specify into clause when executing a stored procedure or a type method using CALL statement:
SQL> call pkg.p1;
call pkg.p1
*
ERROR at line 1:
ORA-06576: not a valid function or procedure name
SQL> call pkg.f1 into :f_res;
call pkg.f1 into :f_res
*
ERROR at line 1:
ORA-06576: not a valid function or procedure name
SQL> call pkg.f1();
call pkg.f1()
*
ERROR at line 1:
ORA-06576: not a valid function or procedure name

I think you're trying to use CALL as a SQL*Plus command. As AHWNN pointed out, in sqlplus you would use EXECUTE to run the procedure. However you could in SQL test with CALL by using an anonymous block:
BEGIN
CALL the_pack.the_proc;
END;
/

Related

Oracle PL/SQL public variable with db link in package body

I'm trying to use a global variable in package body with db link but SQLDeveloper throws me PL:00352
CREATE OR REPLACE PACKAGE BODY my_package
AS
global_variable ANOTHER_TABLE#MY_DB_LINK.ANOTHER_FIELD%TYPE;
PROCEDURE my_procedure IS
variable1 my_table.my_field%TYPE;
BEGIN
do things;
END;
END my_package;
The global_variable line is throwing me PLS-00352: Unable to access another database 'MY_DB_LINK';
Maybe I am wrong with syntax?
Thanks in advance.
Wrong syntax.
Example: my database link works:
SQL> select * from dual#dbl_scott;
D
-
X
Declaring a variable that inherits datatype from emp table, empno column, over the database link:
SQL> declare
2 gl_var emp.empno#dbl_scott%type;
3 begin
4 null;
5 end;
6 /
PL/SQL procedure successfully completed.
SQL>
So, it is: table.column#database_link%type
In your case:
global_variable another_table.another_field#my_db_link%type;

What can be the smallest Procedure?

I want to create Procedure/Function without Parameters.
create or replace procedure p_newname ()
iS
begin
dbms_output.put_line('ok');
end p_newname;
/
But, i am getting the following error message:
"Warning: Procedure created with compilation errors."
Actually i wanted to call this Procedure using - Standalone Execution but without any involvement of parameters be it Formal parameters or Actual parameters:
EXECUTE p_newname();
Expected result should be - ok
Because, you need to remove parentheses after procedure name if procedure doesn't have any parameter during the creation.
But,you can call either by
SQL> exec p_newname();
/
or
SQL> exec p_newname;
/
or
SQL> begin
p_newname;
end;
/
or
SQL> begin
p_newname();
end;
/
of course without forgetting to issue
SQL> set serveroutput on
before them the results to be able to be printed on the screen.
As the question is
What can be the smallest Procedure?
and if it means "use as few letters as possible", then something like this might be the answer:
SQL> create procedure p as begin null; end;
2 /
Procedure created.
SQL>

How to call stored procedure with only OUT parameter?

I have created a stored procedure in Oracle 11g:
CREATE OR REPLACE PROCEDURE greetings(cnt OUT VARCHAR2)
AS
BEGIN
SELECT COUNT(*)
INTO cnt
FROM SYS.all_tables;
END greetings;
but I am unable to call it.
I have tried the following code snippets:
EXEC GREETINGS();
EXEC GREETINGS;
CALL GREETINGS;
The procedure requires one parameter, so - provide it.
SQL> CREATE OR REPLACE PROCEDURE greetings(cnt OUT VARCHAR2)
2 AS
3 BEGIN
4 SELECT COUNT(*)
5 INTO cnt
6 FROM SYS.all_tables;
7 END greetings;
8 /
Procedure created.
One option, which works everywhere, is to use an anonymous PL/SQL block:
SQL> set serveroutput on
SQL> declare
2 l_cnt number;
3 begin
4 greetings(l_cnt);
5 dbms_output.put_line(l_cnt);
6 end;
7 /
87
PL/SQL procedure successfully completed.
Another one works in SQL*Plus (or any other tool which is capable of simulating it):
SQL> var l_cnt number;
SQL> exec greetings(:l_cnt);
PL/SQL procedure successfully completed.
SQL> print l_cnt;
L_CNT
----------
87
Regarding the call example, this is explained in EXECUTE recognizes a stored procedure, CALL does not. It's not obvious from the syntax documentation but it does require brackets, so it is (rather unhelpfully) rejecting the whole thing and giving the impression that greetings is the problem, when actually it is not:
SQL> call greetings;
call greetings
*
ERROR at line 1:
ORA-06576: not a valid function or procedure name
while using the mandatory brackets gets you the real issue:
SQL> call greetings();
call greetings()
*
ERROR at line 1:
ORA-06553: PLS-306: wrong number or types of arguments in call to 'GREETINGS'
As others have pointed out, you are missing the parameter.
SQL> var n number
SQL>
SQL> call greetings(:n);
Call completed.
SQL> print :n
N
----------
134
execute is just a handy SQL*Plus shortcut for the PL/SQL block begin xxx; end; which is less fussy about brackets and gives the same error message with or without them.
(variable and print are SQL*Plus commands and may not be supported in other environments.)
There's no problem with the procedure body. You can call like this :
SQL> var nmr number;
SQL> exec greetings(:nmr);
PL/SQL procedure successfully completed
nmr
------------------------------------------
306 -- > <a numeric value returns in string format>
Oracle doesn't care assigning a numeric value to a string. The execution prints the result directly, but whenever you want you can recall that value of variable(nmr) again, and print as
SQL> print nmr
nmr
---------
306

when try to excure procedure it said compliation error in oracle?

I try to write select procedure in oracle.but it compile success, when I try to execute it given error.
set serveroutput on;
CREATE OR REPLACE PROCEDURE retrieve_decrypt(
custid in NUMBER,
column_name in VARCHAR2,
test_value OUT VARCHAR2
)
AS
BEGIN
-- enc_dec.decrypt(column_name,password) into test_value from employees where custid=5;
COMMIT;
END;
/
set serveroutput on;
EXEC retrieve_decrypt(5,'creditcardno');
the error says ,
This is your procedure:
SQL> create or replace procedure retrieve_decrypt
2 (custid in number,
3 column_name in varchar2,
4 test_value out varchar2
5 )
6 as
7 begin
8 -- your code goes here
9 null;
10 end;
11 /
Procedure created.
SQL>
This is how you call it (and get the error):
SQL> exec retrieve_decrypt(5, 'creditcardno');
BEGIN retrieve_decrypt(5, 'creditcardno'); END;
*
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'RETRIEVE_DECRYPT'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
SQL>
The cause of the error is:
the procedure contains 3 parameters:
2 of them are IN - you provided their values
1 of them is OUT - you didn't provide it and got the error
Here's what you should have done: as the 3rd parameter is OUT, you'll have to DECLARE it:
SQL> declare
2 l_out varchar2(20);
3 begin
4 retrieve_decrypt(5, 'creditcardno', l_out);
5 end;
6 /
PL/SQL procedure successfully completed.
SQL>
EXEC you used is a SQL*Plus command so it might not work everywhere; DECLARE-BEGIN-END block will so I'd suggest you use it.
Alternatively, in SQL*Plus, it could be rewritten as
SQL> var l_out varchar2
SQL>
SQL> exec retrieve_decrypt(5, 'creditcardno', :l_out);
PL/SQL procedure successfully completed.
SQL>
but - once again - you'd better use DECLARE-BEGIN-END PL/SQL block.
The initial error is:
wrong number or types of arguments in call to 'RETRIEVE_DECRYPT'
The procedure requires 3 parameters. You are only passing 2 (or apparently only 1, in the attempt that generated the error message shown).
Why do you also see the message "Usually a PL/SQL compilation error"? The EXEC command in SQLPlus creates a PL/SQL block containing the text you provide, and sends that to Oracle for execution. Oracle attempts to compile that PL/SQL block (just like it compiles the procedure when you create it). In this case, the compilation fails because of the mismatch in the number of arguments.

Initialization section of the package

This is the package specification:
CREATE OR REPLACE PACKAGE employee_info
IS
PROCEDURE p;
FUNCTION f RETURN BOOLEAN;
END employee_info;
This is the package body:
CREATE OR REPLACE PACKAGE body employee_info
IS
PROCEDURE p IS
BEGIN
dbms_output.put_line('This is procedure');
dbms_output.put_line(chr(10));
END;
FUNCTION f RETURN BOOLEAN IS
BEGIN
dbms_output.put_line('This is function ');
dbms_output.put_line(chr(10));
RETURN true;
END;
BEGIN
dbms_output.put_line('This is the initialization section of package body');
END employee_info;
When should I use this initialization section of the package?
As in the above example, when I first execute the code then only i can see that begin..end of package body is executed and for rest of call it is not executed.
I am executing the procedure and function using below statements:
execute employee_info.p;
declare
p1 boolean;
begin
p1 := employee_info.f;
end;
Package initialization section as the name suggest is executed when package is initialized. This happens when first procedure/function from the package is executed after session is established or after package is (re)compiled. The purpose is to initialize the global state of the package that can be used during session lifetime. All package global variables are kept and you can access them later.
Example:
HUSQVIK#hq_pdb_tcp> CREATE OR REPLACE PACKAGE test_package
2 IS
3 PROCEDURE foo;
4 END;
5 /
Package created.
HUSQVIK#hq_pdb_tcp> CREATE OR REPLACE PACKAGE BODY test_package
2 IS
3 PROCEDURE foo
4 IS
5 BEGIN
6 DBMS_OUTPUT.PUT_LINE('Procedure executed. ');
7 END;
8
9 BEGIN
10 DBMS_OUTPUT.PUT_LINE('Package initialized. ');
11 END;
12 /
Package body created.
HUSQVIK#hq_pdb_tcp> EXEC test_package.foo
Package initialized.
Procedure executed.
PL/SQL procedure successfully completed.
HUSQVIK#hq_pdb_tcp> EXEC test_package.foo
Procedure executed.
PL/SQL procedure successfully completed.
HUSQVIK#hq_pdb_tcp>
You see that after package is compiled the initialization section is executed when procedure foo is executed. The package is initialized now. Any subsequent execution of foo executes only the procedure.

Resources