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;
Related
I want to know how is ROT_TMLN_ARRAY type added to a variable in sql developer. I see that ROT_TMLN_ARRAY is also a type and I want to create something similar with another variable.
create or replace PACKAGE BODY AS PKG TMLN
PROCEDURE SP TMLN SVC (
rotnPrngNb IN VARCHAR2
empiRotnDetails OUT ROT_TMLN ARRAY)
U can add the variable as a Type in both IN or OUT Parameter.
CREATE OR REPLACE PROCEDURE P_TMLN_SVC(I_ID IN NUMBER,
O_RESULT OUT T_TABLE)
AS
BEGIN
SELECT OWNER_TYPE BULK COLLECT INTO O_RESULT FROM OWNERES WHERE OWNERS_ID=I_ID;
END P_TMLN_SVC;
Calling Statement:
DECLARE
T_ARRAY T_TABLE;
BEGIN
P_TMLN_SVC(100,T_ARRAY);
END;
declare
TYPE stag_tab IS TABLE OF d_staging%ROWTYPE;
stag_tab1 stag_tab;
begin
--Bulk Collect
select * bulk collect into staging_tab1 from d_staging;
PKG_T.PROC_T(stag_tab1);
end;
/
Package definition:
--Package
CREATE OR REPLACE PACKAGE PKG_T
AS
TYPE staging IS TABLE OF d_staging%ROWTYPE;
PROCEDURE PROC_T(p_staging IN staging);
END PKG_T;
/
-- Package Body
CREATE OR REPLACE PACKAGE BODY PKG_T
AS
PROCEDURE PROC_T (p_staging IN staging)
AS
VAR1 d_staging%ROWTYPE;
CUR1 SYS_REFCURSOR;
QUERY_STRING VARCHAR2(2000);
BEGIN
OPEN CUR1 FOR SELECT * from table(p_staging);
LOOP
FETCH CUR1 into VAR1;
EXIT WHEN cur1%NOTFOUND;
INSERT into d (testdata) VALUES (var1.testval1);
COMMIT;
END LOOP;
END;
END PKG_T;
/
You are receiving the error because the procedure PKG_T.PROC_T is expecting a parameter of type staging, but when you are calling the procedure you are passing it a variable of type stag_tab. The type of the variable being passed to the procedure needs to match the type of the parameter definition for the procedure.
Your procedure declaration:
PROCEDURE PROC_T (p_staging IN staging)
Takes the argument as type staging.
You are passing the argument as a locally defined type:
TYPE stag_tab IS TABLE OF d_staging%ROWTYPE;
These are different types. Instead, you need the PL/SQL block to be:
declare
stag_tab1 package_name.staging;
begin
select *
bulk collect into stag_tab1
from d_staging;
PKG_T.PROC_T(stag_tab1);
end;
/
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
i'm kind of new to Oracle Pl\SQL. I was just trying to create a simple Package with a procedure that returns a set of object id's; the code is as follows:
--Package Spec
CREATE OR REPLACE PACKAGE TEST IS
--GET OBJECT ID'S FROM CONTROL TABLE
PROCEDURE get_object_id_control(p_obj_id OUT abc_table%ROWTYPE);
END;
--Package Body
PROCEDURE get_object_id_control(p_obj_id OUT abc_table%ROWTYPE) AS
BEGIN
SELECT object_id
INTO p_obj_id
FROM abc_table
WHERE fec_proc IS NULL;
END;
I get Error: PL/SQL: ORA-00913: too many values. Is this the correct way for returning multiple values of same data type, or is there a better approach. Thanks in advance.
You can create a custom table type and set the out parameter of the procedure to that type.
CREATE TABLE ABC_TABLE(ID varchar2(100));
create or replace type abc_tab is table of varchar2(100);
/
CREATE OR REPLACE PACKAGE TEST IS
PROCEDURE get_object_id_control(p_obj_id OUT abc_tab);
END;
/
CREATE OR REPLACE PACKAGE BODY TEST IS
PROCEDURE get_object_id_control(p_obj_id OUT abc_tab) AS
BEGIN
SELECT id
bulk collect INTO p_obj_id
FROM abc_table;
END;
END;
/
Then you can call it like so:
declare
v abc_tab;
begin
TEST.get_object_id_control(p_obj_id => v);
for i in v.first..v.last loop
dbms_output.put_line(v(i));
end loop;
end;
/
Similar to GurV's answer (since he beat me by like 30 seconds...), you can use a PL/SQL object type as well. You do not need the CREATE TYPE statement if you don't need to reference the type in SQL.
--Package Spec
CREATE OR REPLACE PACKAGE TEST AS
TYPE id_table_type IS TABLE OF NUMBER;
--GET OBJECT ID'S FROM CONTROL TABLE
PROCEDURE get_object_id_control(p_obj_id_list OUT id_table_type);
END;
--Package Body
CREATE OR REPLACE PACKAGE BODY TEST AS
PROCEDURE get_object_id_control(p_obj_id_list OUT id_table_type) AS
BEGIN
SELECT object_id
BULK COLLECT INTO p_obj_id_list
FROM abc_table
WHERE fec_proc IS NULL;
END;
END;
To use it:
DECLARE
l_id_list test.id_table_type;
BEGIN
test.get_object_id_control (p_obj_id_list => l_id_list);
FOR i IN l_id_list.FIRST .. l_id_list.LAST LOOP
DBMS_OUTPUT.put_line (l_id_list (i));
END LOOP;
END;
Using a package, how can i pass a variable assign in a procedure to the parameter of another procedure?
I'm not sure if you are wondering about global variables or how OUT parameters work. An example of using a procedure's OUT parameter is below. More info on using IN and OUT can be found here
create or replace package TEST_PKG
IS
procedure test(p_input IN NUMBER, p_output OUT NUMBER)
IS
BEGIN
p_output := p_input * p_input;
END test ;
procedure testRun
IS
v_input NUMBER := 0;
v_output NUMBER;
BEGIN
test(v_input, v_output);
DBMS_OUTPUT.PUT_LINE('OUTPUT : ' || v_output );
END test ;
END xyz;
create or replace package xyz
IS
procedure test
IS
v_temp varchar2(20); --local variable
BEGIN
v_temp:=20; --assigning value to a variable
--if procedure is within the same package,as this example shows ,then call as below
test2(v_temp);
--if procedure is in another package say PQR then call as below,
--but the procedure should be declared in the specification of the package PQR
PQR.test2(v_temp);
--if procedure is not inside any package ,just a single procedure is there
--,then call as below
test2(v_temp);
END test ;
procedure test2(p_temp IN varchar2)
IS
BEGIN
--do you processing
END test2;
END xyz;
Hope this helps