I have a package whose body consists of a stored procedure.
The stored procedure makes a call to a stored procedure in another package. However I need to pass the result of a select statement as a parameter. so I stored the result in a variable and passed it as a parameter. However when the procedure call is performed, it says, the variable is not declared.
This is the sample code.
create or replace
PACKAGE BODY PKG_BODY
PROCEDURE PROC (error OUT VARCHAR2,
RESULT OUT BOOLEAN ) AS
-----------------------------
-----------------------------
DECLARE
PARAM TABLE1.COLUMN1%TYPE;
BEGIN
SELECT COLUMN1
INTO PARAM
FROM TABLE1
WHERE COLUMN2=ID AND COLUMN3='NAME';
END;
PUBLISH_MSG.PUBLISH_PMM_MSG('ID',PARAM,PO_ERR_MSG); --another procedure call
END PROC;
I am getting the following error :
PLS:00201 'Identifier PARAM must be declaed'
Do I have to declare the PARAM before somewhere else? Please help !!
You have defined that variable in a nested PL/SQL block - it can't be accessed from an outer block (it doesn't even exist after the block is finished). You should move definition of your param variable to the outer block, for example:
PROCEDURE PROC (error OUT VARCHAR2,
RESULT OUT BOOLEAN )
AS
PARAM TABLE1.COLUMN1%TYPE;
BEGIN
-----------------------------
-----------------------------
SELECT COLUMN1
INTO PARAM
FROM TABLE1
WHERE COLUMN2=ID AND COLUMN3='NAME';
PUBLISH_MSG.PUBLISH_PMM_MSG('ID',PARAM,PO_ERR_MSG); --another procedure call
END PROC;
Related
I have four working stored procedures (DB2), each accepting their own parameters, but a lot of those params are the same.
I'm trying to create a larger wrapper procedure that will basically just call each in order. There are about 10 outside values coming into the wrapper procedure as it's own parameters.
Within the body, where I call all 4 child parameters, I will be using a mixture of outside params as well as a main ID param that is derived from the first call.
How can I properly wrap these four procedure calls using the outside params as well as the captured/derived parameter from my first call?
Parameters:
FIRST_NAME
LAST_NAME
PROFILE_IMAGE
CODE
START_DATE
EXPIRE_DATE
PRIORITY
CUST_NUMBER
CONTACT_TYPE
CONTACT_VALUE
CALL FIRST_PROC(FIRST_NAME,LAST_NAME,PROFILE_IMAGE)
--This returns ```FIRST_PROC_ID```
CALL SECOND_PROC(FIRST_PROC_ID, CODE,START_DATE,EXPIRE_DATE,PRIORITY)
CALL THIRD_PROC(FIRST_PROC_ID,CODE,CUST_NUMBER,START_DATE,EXPIRE_DATE,PRIORITY)
CALL FOURTH_PROC(FIRST_PROC_ID,CONTACT_TYPE,CONTACT_VALUE,START_DATE,EXPIRE_DATE)
Use the GET DIAGNOSTICS statement.
--#SET TERMINATOR #
SET SERVEROUTPUT ON#
CREATE OR REPLACE PROCEDURE TEST1(P_FIRST_PROC_ID INT)
BEGIN
RETURN P_FIRST_PROC_ID;
END#
BEGIN
DECLARE V_RC INT;
CALL TEST1(10);
GET DIAGNOSTICS V_RC = DB2_RETURN_STATUS;
CALL DBMS_OUTPUT.PUT_LINE('Return Status: '||V_RC);
END#
I need to call stored procedure into another stored procedure and return value stored in variable in db2.
Thanks
One examples could be this one:
DECLARE STMT STATEMENT;
PREPARE STMT FROM 'CALL MODIFY_DESCENDANTS (?, ?)';
EXECUTE STMT USING LOG_ID, LEVEL;
Taken from log4db2 - https://github.com/angoca/log4db2/blob/master/src/main/sql-pl/03-UtilityBody.sql#L729
If the stored procedure returns a result set, and you want to take values from that, then you need to call the stored procedure and use the associate and allocate statements. For an example, you can visit https://www.toadworld.com/platforms/ibmdb2/w/wiki/7460.call-allocate-and-associate
CALL EMPR;
ASSOCIATE RESULT SET LOCATOR (LOC1) WITH PROCEDURE EMPR;
ALLOCATE C1 CURSOR FOR RESULT SET LOC1;
I am trying to return a cursor from a procedure which will display the contents of a table invoice. The package and procedure was successfully created but when I run the line - Exec CursorPckg.CursorTest_Proc(); I get the following error component 'CURSORTEST_PROC' must be declared any ideas? I'm running SQL*PLUS
CREATE or REPLACE PACKAGE CursorPckg
IS
TYPE salary_type IS REF CURSOR RETURN Invoice%ROWTYPE;
END CursorPckg;
CREATE OR REPLACE PACKAGE BODY CursorPckg AS
PROCEDURE CursorTest_Proc (c1 OUT CursorPckg.salary_type)
IS
BEGIN
OPEN c1 FOR
SELECT * FROM Invoice;
END CursorTest_Proc;
END CursorPckg;
You need to add procedure declaration in package declaration for it to be accessible from outside your package.
Like this:
CREATE or REPLACE PACKAGE CursorPckg
IS
TYPE salary_type IS REF CURSOR RETURN Invoice%ROWTYPE;
PROCEDURE CursorTest_Proc (c1 OUT CursorPckg.salary_type);
END CursorPckg;
Edit:
To answer Your comment. You cannot execute this procedure without providing parameter of type CursorPckg.salary_type.
I have a stored procedure in oracle and want to test it from SQLPlus.
If I use
execute my_stored_proc (-1,2,0.01)
I get this error
PLS-00306: wrong number or types of arguments in call to my_stored_proc
The beginning for the proc is this
create or replace PROCEDURE my_stored_proc
( a IN NUMBER,
b IN NUMBER,
c IN NUMBER,
z out NUMBER
) AS ....
Do I need to provide the a var for the out parameter, is so how? I tried:
var z NUMBER;
But get this error when I try to run the proc
execute my_stored_proc (-1,2,0.01,z)
PLS-00201: identifier 'Z' must be declared
Also when I was in SQL-Developer it gave me the usage and it show the inputs in reverse order, that is:
execute my_stored_proc(z number,c number,b number,a number);
Do you provide them in reverse order or is that just something with SQL-Developer
I did not write the procedure and I don't normally deal with them so I could be missing something obvious.
Thanks
You have two options, a PL/SQL block or SQL*Plus bind variables:
var z number
execute my_stored_proc (-1,2,0.01,:z)
print z
You forgot to put z as an bind variable.
The following EXECUTE command runs a PL/SQL statement that references a stored procedure:
SQL> EXECUTE -
> :Z := EMP_SALE.HIRE('JACK','MANAGER','JONES',2990,'SALES')
Note that the value returned by the stored procedure is being return into :Z
What is the difference between function and procedure in PL/SQL ?
A procedure does not have a return value, whereas a function has.
Example:
CREATE OR REPLACE PROCEDURE my_proc
(p_name IN VARCHAR2 := 'John') as begin ... end
CREATE OR REPLACE FUNCTION my_func
(p_name IN VARCHAR2 := 'John') return varchar2 as begin ... end
Notice how the function has a return clause between the parameter list and the "as" keyword. This means that it is expected to have the last statement inside the body of the function read something like:
return(my_varchar2_local_variable);
Where my_varchar2_local_variable is some varchar2 that should be returned by that function.
A function can be in-lined into a SQL statement, e.g.
select foo
,fn_bar (foo)
from foobar
Which cannot be done with a stored procedure. The architecture of the query optimiser limits what can be done with functions in this context, requiring that they are pure (i.e. the same inputs always produce the same output). This restricts what can be done in the function, but allows it to be used in-line in the query if it is defined to be "pure".
Otherwise, a function (not necessarily deterministic) can return a variable or a result set. In the case of a function returning a result set, you can join it against some other selection in a query. However, you cannot use a non-deterministic function like this in a correlated subquery as the optimiser cannot predict what sort of result set will be returned (this is computationally intractable, like the halting problem).
In dead simple way it makes this meaning.
Functions :
These subprograms return a single value; mainly used to compute and return a value.
Procedure :
These subprograms do not return a value directly; mainly used to perform an action.
Example Program:
CREATE OR REPLACE PROCEDURE greetings
BEGIN
dbms_output.put_line('Hello World!');
END ;
/
Executing a Standalone Procedure :
A standalone procedure can be called in two ways:
• Using the EXECUTE keyword
• Calling the name of procedure from a PL/SQL block
The procedure can also be called from another PL/SQL block:
BEGIN
greetings;
END;
/
Function:
CREATE OR REPLACE FUNCTION totalEmployees
RETURN number IS
total number(3) := 0;
BEGIN
SELECT count(*) into total
FROM employees;
RETURN total;
END;
/
Following program calls the function totalCustomers from an another block
DECLARE
c number(3);
BEGIN
c := totalEmployees();
dbms_output.put_line('Total no. of Employees: ' || c);
END;
/
Both stored procedures and functions are named blocks that reside in the database and can be executed as and when required.
The major differences are:
A stored procedure can optionally return values using out parameters, but can also be written in a manner without returning a value. But, a function must return a value.
A stored procedure cannot be used in a SELECT statement whereas a function can be used in a SELECT statement.
Practically speaking, I would go for a stored procedure for a specific group of requirements and a function for a common requirement that could be shared across multiple scenarios. For example: comparing between two strings, or trimming them or taking the last portion, if we have a function for that, we could globally use it for any application that we have.
The following are the major differences between procedure and function,
Procedure is named PL/SQL block which performs one or more tasks. where function is named PL/SQL block which performs a specific action.
Procedure may or may not return value where as function should return one value.
we can call functions in select statement where as procedure we cant.
In the few words - function returns something. You can use function in SQL query.
Procedure is part of code to do something with data but you can not invoke procedure from query, you have to run it in PL/SQL block.
we can call a stored procedure inside stored Procedure,Function within function ,StoredProcedure within function but we can not call function within stored procedure.
we can call function inside select statement.
We can return value from function without passing output parameter as a parameter to the stored procedure.
This is what the difference i found. Please let me know if any .