Parameters issue on PLSQL Procedure - oracle

I've have an issue while running a PL SQL stored procedure.
It says it need parameters but it is defined without anyone.
I've tried to run as this execute SP_LIST_PRICE_BWI;
Here is the procedure declaration:
create or replace PROCEDURE SP_LIST_PRICE_BWI (P_ERROR_CODE OUT VARCHAR2,P_ERROR_TEXT OUT VARCHAR2,P_DATA_TEXT OUT VARCHAR2 )
I will really appreciate if you could help me out.
Regards,
Nicolás.

You cannot call a procedure without providing the variable names for OUT parameters.
Either you use bind variables
VARIABLE v_ERROR_CODE VARCHAR2
VARIABLE v_ERROR_TEXT VARCHAR2
VARIABLE v_DATA_TEXT VARCHAR2
EXECUTE SP_LIST_PRICE_BWI(:v_ERROR_CODE ,:v_ERROR_TEXT, :v_DATA_TEXT);
OR
declare variables with proper size and pass them
DECLARE
v_ERROR_CODE VARCHAR2(5);
v_ERROR_TEXT VARCHAR2(100);
v_DATA_TEXT VARCHAR2(1000);
BEGIN
SP_LIST_PRICE_BWI(v_ERROR_CODE ,v_ERROR_TEXT, v_DATA_TEXT);
END;

Related

how to execute procedure which has out varchar

I have a package:
CREATE OR REPLACE PACKAGE BODY pack1
AS
procedure proc1(column_1 in table1.column1%TYPE, column_2 in table1.column2%TYPE, results out varchar)
//code....
......
.....
....//
END pack1;
/
I want to know how to execute this procedure. I know it's a silly question, But I am very confused about this.
I tried this: exec pack1.proc1(123,'abc')
But this is not working. Please tell me how to execute this
You may use a bind variable in SQL* Plus to get the results and display it using PRINT
VARIABLE v_result VARCHAR2
EXECUTE pack1.proc1(123, 'abc', :v_result); --note the colon.
PRINT v_result --display the results.
If you're calling it from a PL/SQL block or another procedure, you may just display it using DBMS_OUTPUT.PUT_LINE()
SET SERVEROUTPUT ON
DECLARE
l_results VARCHAR2(20);
BEGIN
pack1.proc1(123, 'abc', l_results);
DBMS_OUTPUT.PUT_LINE(l_results);
END;
/
Note: Use VARCHAR2 instead of VARCHAR as the datatype for procedure parameter results

Not passing an OUT parameter in PL/SQL

So I have this interesting situation. I have a PL/SQL procedure where I pass two IN parameters and two OUT parameters:
PROCEDURE p_merge_catalog(p_merge_from_code VARCHAR2,
p_merge_to_code VARCHAR2,
msg_type_out OUT VARCHAR2,
msg_out OUT VARCHAR2)
IS
.
.
My conundrum is I'm using a job submission/scheduler program to call the procedure that does not seem to deal with OUT variables. I'm not able to bind variables to the OUT parameters; it only passes values. Is there a way to call this procedure without specifying an OUT parameter? Or maybe trick it and use some kind of hidden variable?
Not very beautiful for two outs, but working ;)
CREATE OR REPLACE FUNCTION f_merge_catalog_wrapper (
p_merge_from_code VARCHAR2,
p_merge_to_code VARCHAR2)
RETURN emp%ROWTYPE
IS
msg_type_out VARCHAR2(2000);
msg_out VARCHAR2(2000);
BEGIN
p_merge_catalog(p_merge_from_code, p_merge_to_code, msg_type_out, msg_out);
return msg_type_out || '#' || msg_out;
END;
Didn't check for typos. But the mechanic should be clear :o).
If you don't need the output:
CREATE OR REPLACE PROCEDURE p_merge_catalog_wrapper (
p_merge_from_code VARCHAR2,
p_merge_to_code VARCHAR2)
RETURN emp%ROWTYPE
IS
msg_type_out VARCHAR2(2000);
msg_out VARCHAR2(2000);
BEGIN
p_merge_catalog(p_merge_from_code, p_merge_to_code, msg_type_out, msg_out);
END;

Use bind variables inside pl/sql stored procedure

Is it possible to use bind variables,(declared and initialized outside pl/sql block) inside pl/sql stored procedure
I am just trying to print bind variable defined as shown below :
Bind variable declaration and initialization
var jdata varchar2(4000);
exec :jdata := ''{"PONumber":12,"Reference":"StackOver"}'';
pl/sql procedure
create or replace PROCEDURE generic_procedure( v_typename IN VARCHAR2,v_path IN VARCHAR2 DEFAULT '$') IS
BEGIN
dbms_output.put_line(:jdata);
END generic_procedure;
/
Error
fails at compilation:
SQL> show errors
Errors for PROCEDURE GENERIC_PROCEDURE:
ERROR: bad bind variable 'JDATA'
Here, I'm trying to call jdata inside generic_procedure(stored pl/sql procedure) . but it says JDATA as
bad bind variable
You could use a substitution variable:
define jdata='{"PONumber":12,"Reference":"StackOver"}';
create or replace PROCEDURE generic_procedure( v_typename IN VARCHAR2,
v_path IN VARCHAR2 DEFAULT '$') IS
BEGIN
dbms_output.put_line('&jdata');
END generic_procedure;
/
But you can also pass it as a parameter to the procedure. Which seems a bit cleaner to me. Since the procedure does not depend on some variable that has, or has not been, declared outside of it. It just works on the parameters alone.
create or replace PROCEDURE generic_procedure( v_data IN varchar2,
v_typename IN VARCHAR2,
v_path IN VARCHAR2 DEFAULT '$') IS
BEGIN
dbms_output.put_line(v_data);
END generic_procedure;
/
exec generic_procedure('&jdata','typename');
Here's some more about variables in sqlplus
AFAIK, don't think this is possible as the PL/SQL variables need to be defined in the scope of the package or procedure.
You can create a package to hold this variable and then set and use it's value in the procedure.
CREATE OR REPLACE PACKAGE P_test
is
jdata CONSTANT varchar2(4000):=''{"PONumber":12,"Reference":"StackOver"}'';
END P_test ;
create or replace PROCEDURE generic_procedure( v_typename IN VARCHAR2,v_path
IN VARCHAR2 DEFAULT '$') IS
BEGIN
dbms_output.put_line(P_test.jdata);
END generic_procedure;
Hope it helps
Vishad

Temporary and Permanent Stored Procedure

I am creating Stored Procedure in Oracle and one of them is created permanently and another one is temporarily created and vanished after serving its purpose.How it is working please give your guidance when to use and how it is created.
---- This is not created in DB, just temporarily created and vanished
DECLARE name varchar2(10);
PROCEDURE printVal (name varchar2) IS
BEGIN
dbms_output.put_line ('name:' || name);
END;
BEGIN
name := 'Joe';
printVal(name);
END;
/
---- This is created in DB and permanently available
create PROCEDURE printVal (name varchar2) IS
BEGIN
dbms_output.put_line ('name:' || name);
END;
To understand, Dividing your sql in two parts.
Stored Procedure:
Stored procedures are stored in database.
We can call stored procedures any time after creation.
Stored procedures also supports input output parameters.
Anonymous Block:
These are unnamed pl/sql blocks.
Anonymous blocks are not stored in database.
Cannot pass paramters
---------- Stored Procedure Start--------
DECLARE name varchar2(10);
PROCEDURE printVal (name varchar2) IS
BEGIN
dbms_output.put_line ('name:' || name);
END;
--------- Stored Procedure End-----------
----------anonymous block Start----------
BEGIN
name := 'Joe';
printVal(name);
END;
/
----------anonymous block end ------------
Well, clearly there is different syntax -- the first one is an anonymous block, and the second creates a stored procedure. The expected behaviour is exactly what you observe, and covered by Oracle PL/SQL documentation. https://docs.oracle.com/cloud/latest/db112/LNPLS/overview.htm#LNPLS141

Find out name of PL/SQL procedure

Can PL/SQL procedure in Oracle know it's own name?
Let me explain:
CREATE OR REPLACE procedure some_procedure is
v_procedure_name varchar2(32);
begin
v_procedure_name := %%something%%;
end;
After %%something%% executes, variable v_procedure_name should contain 'SOME_PROCEDURE'. It is also OK if it contains object_id of that procedure, so I can look up name in all_objects.
Try:
v_procedure_name := $$PLSQL_UNIT;
There's also $$PLSQL_LINE if you want to know which line number you are on.
If you are pre-10g, you can 'dig' (parse) it out of
dbms_utility.format_call_stack
Procedures/functions in packages can be overloaded (and nested), so the package name/line number is normally better than the name.
In 10g and 11g I use the "owa_util.get_procedure" function. I normally use this in packages as it will also return the name of an internal procedure or function as part of the package name, i.e. (package_name).(procedure name). I use this to provide a generic EXCEPTION template for identifying where an exception occured.
CREATE OR REPLACE procedure some_procedure is
v_procedure_name varchar2(32);
begin
v_procedure_name := owa_util.get_procedure;
end;
CREATE OR REPLACE PACKAGE some_package
AS
FUNCTION v_function_name
RETURN DATE;
END;
/
CREATE OR REPLACE PACKAGE BODY some_package
AS
FUNCTION v_function_name
RETURN DATE
IS
BEGIN
RETURN SYSDATE;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('ERROR IN '||owa_util.get_procedure);
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
END;
/
Here's a neat function that takes advantage of REGEXP_SUBSTR.
I've tested it in a package (and it even works if another procedure in the package calls it):
FUNCTION SET_PROC RETURN VARCHAR2 IS
BEGIN
RETURN NVL(REGEXP_SUBSTR(DBMS_UTILITY.FORMAT_CALL_STACK,
'procedure.+\.(.+)\s', 1,1,'i',1), 'UNDEFINED');
END SET_PROC;

Resources