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
Related
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
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;
I am fairly new to PL/SQL but have been reading up on it and have used some templates, including some which I found from here.
What I want to do is to write an anonymous block to call some procedures which wrere written earlier in a sql developer project. I have attempted it but it isn't running properly. It returns an error of "Error starting at line : 2 in command " and then reports a "closed connection."
This is my attempt:
DECLARE
P_USER_NAME VARCHAR;
P_DEBUG_FLAG VARCHAR;
P_DEBUG_FIELD VARCHAR;
P_DEBUG_VALUE VARCHAR;
BEGIN
schema.package.procedure(
OutParam1, OutParam2, OutParam3, OutParam4);
dbms_output.put_line('OutParam1: ' || P_USER_NAME);
dbms_output.put_line('OutParam2: ' || P_DEBUG_FLAG);
dbms_output.put_line('OutParam3: ' || P_DEBUG_FIELD);
dbms_output.put_line('OutParam4: ' || P_DEBUG_VALUE);
END;
/
And these are the procedure I want to call:
PROCEDURE CLEAR_DEBUG (P_USER_NAME IN VARCHAR2);
PROCEDURE WRITE_DEBUG (P_USER_NAME IN VARCHAR2,
P_DEBUG_FLAG IN VARCHAR2,
P_DEBUG_FIELD IN VARCHAR2,
P_DEBUG_VALUE IN VARCHAR2);
PROCEDURE READ_DEBUG (P_USER_NAME IN VARCHAR2,
P_REF_CURSOR OUT SYS_REFCURSOR);
END P_DEBUG;
There are more errors in your code:
declare the variables properly - VARCHAR requires length constraint
OutParam1, OutParam2, OutParam3, OutParam4 are not declared - use the declared variables as arguments instead, beware of the variables passed as arguments must have the same data type
I expect you wanted to call P_DEBUG.WRITE_DEBUG(P_USER_NAME, P_DEBUG_FLAG, P_DEBUG_FIELD, P_DEBUG_VALUE);
Trying to set a local variable to parameter value that is passed to Oracle stored procedure However i am getting nottihgn..what am i doing wrong. When I test the package v_param is empty.
I am passing a long text
create or replace package MA_MAIN_SP_TESTCLOB IS
v_param clob;
procedure RUNALL( iOffice clob);
end MA_MAIN_SP_TESTCLOB;
create or replace package body MA_MAIN_SP_TESTCLOB IS
procedure RUNALL(iOffice clob) IS
BEGIN
v_param := iOffice;
--dbms_output.put_line('Parmeters are ' || v_param);
insert into ICCMSDW_CR.OfficeHolder(POFFICE)
values(v_param);
END RUNALL;
END MA_MAIN_SP_TESTCLOB;
Is there a variable name that stores name of the stored procedure currently running? Something similar to $0 in Unix.
CREATE OR REPLACE
PROCEDURE my_sproc(
param1 IN NUMBER,
)
AS
BEGIN
exec other_sproc(XXX);
END;
END;
XXX <- stores the string "my_sproc".
Depending on the Oracle version, you may be able to use conditional compilation and $$PLSQL_UNIT
If other_sproc just prints out the value passed in
create or replace procedure other_sproc( p_in in varchar2 )
as
begin
dbms_output.put_line( p_in );
end;
/
then in Oracle 11g, you can use $$PLSQL_UNIT in the caller
SQL> create or replace procedure my_sproc
2 as
3 begin
4 other_sproc( $$PLSQL_UNIT );
5 end;
6 /
Procedure created.
SQL> exec my_sproc;
MY_SPROC
PL/SQL procedure successfully completed.
This doesn't work as well when you are using packages though (and your stored procedures should almost always be in packages) because the $$PLSQL_UNIT will be the package name not the procedure name.
Note as well that you do not use EXEC in a PL/SQL block. EXEC is a SQL*Plus command. You simply call other_sproc like I do here.