Calling a stored procedure with a select - sql

I have a query and i have to put it in a stored procedure but when I call the stored procedure it doesn´t return anything;
This is my query:
select * from mdc_cat_parametros where param_llave='SMTP_SERVER';
When I execute the query it returns one row but the stored procedure doesn´t.
This is my stored procedure:
CREATE OR REPLACE PROCEDURE MDC_UTIL_PROCEDURE
AS
RT MDC_CAT_PARAMETROS%ROWTYPE;
BEGIN
SELECT * INTO RT FROM MDC_CAT_PARAMETROS WHERE PARAM_LLAVE='SMTP_SERVER';
END MDC_UTIL_PROCEDURE;
I call the stored procedure:
EXECUTE MDC_UTIL_PROCEDURE;

The stored procedure is populating RT but you then need to select out of it:
CREATE OR REPLACE PROCEDURE MDC_UTIL_PROCEDURE (results OUT SYS_REFCURSOR)
AS
RT MDC_CAT_PARAMETROS%ROWTYPE;
BEGIN
SELECT * INTO RT FROM MDC_CAT_PARAMETROS WHERE PARAM_LLAVE='SMTP_SERVER';
OPEN results FOR SELECT * FROM RT;
END MDC_UTIL_PROCEDURE;
or you could simplify it to get rid of the RT variable:
CREATE OR REPLACE PROCEDURE MDC_UTIL_PROCEDURE (results OUT SYS_REFCURSOR)
AS
BEGIN
OPEN results FOR
SELECT * FROM MDC_CAT_PARAMETROS WHERE PARAM_LLAVE='SMTP_SERVER';
END MDC_UTIL_PROCEDURE;

Your procedure is just fetching the result set into a local variable in the procedure. When the procedure exits, the variable is no longer in scope. If you want to return data from a select, you need to use a ref cursor. Here is a link to some good examples

Related

executing a stored proc with output parameter

I am using SQLDEVELOPER TO make a stored procedure. this is my procedure
create or replace PROCEDURE SPP
(
inpt IN VARCHAR2,
opt OUT VARCHAR2
)
AS
BEGIN
SELECT U_ID
INTO opt
FROM TABLE_NAME
WHERE U_ID=inpt;
END;
so it should return the value in opt which i can then print or whatever.
i dont know how to execute it i tried running this script
var opt VARCHAR2;
exec SPP('test_user') := opt
but it doesnt work this is my first time doing stored procs and I'm really confused any help is appreciated
how do i make it run? i can make a stored proc run with input variables but with an output variable i mess up and I cant do it without using the SELECT (item) INTO format.
In order to call the procedure, you'd need to pass in a variable that the procedure can populate. Something like
DECLARE
l_output table_name.u_id%type;
BEGIN
spp( 'test_user', l_output );
END;
Now, that being said, declaring a stored procedure whose only purpose is to query the database and return a value is generally the wrong architectural choice. That's why functions exist.
CREATE OR REPLACE FUNCTION some_function( p_input IN table_name.u_id%type )
RETURN table_name.u_id%type
IS
l_uid table_name.u_id%type;
BEGIN
SELECT u_id
INTO l_uid
FROM table_name
WHERE u_id = p_input;
RETURN l_uid;
END;
which you can then call either in a PL/SQL block
DECLARE
l_output table_name.u_id%type;
BEGIN
l_output := some_function( 'test_user' );
END;
or in a SQL statement
SELECT some_function( 'test_user' )
FROM dual;

stored PL/sql function using select statement

Example i have a stored pl/sql
CREATE OR REPLACE PROCEDURE number( test in NUMBER )
........
// rest of code
isn't possible that i don't want run this execute
execute number(2);
i want run with
select * from number(2);
isn't possible to run stored pl/sql script with select statement to call the function instead of execute?
You can't execute a PROCEDURE from SQL; however, you can execute a FUNCTION from SQL.
First, redefine NUMBER as a FUNCTION:
CREATE OR REPLACE FUNCTION NUMBER(pTest IN NUMBER) RETURN NUMBER IS
someValue NUMBER := pTest * 100;
BEGIN
-- whatever
RETURN someValue;
END;
Then execute it from a SELECT statement as
SELECT NUMBER(2) FROM DUAL;
Share and enjoy.

how to access an Oracle procedure's OUT parameter when calling it?

If I write a simple function doSomething, I can get its result by executing :
select doSomething() from dual;
But, if I wish to call a procedure that has an OUT cursor being passed to it (along with another int parameter), how do I call that procedure inside a query and access the result of the cursor ?
Calling it inside a query is not compulsory.. its just that I want to access the results of that procedure
You can create a procedure like
CREATE OR REPLACE PROCEDURE your_procedure(out_cursor OUT sys_refcursor)
IS
BEGIN
OPEN out_cursor FOR
SELECT employee_name
FROM employees;
END;
/
Once you create your procedure wrap the procedure in a function which returns a cursor like the following
CREATE OR REPLACE FUNCTION your_function
RETURN sys_refcursor
AS
o_param sys_refcursor;
BEGIN
o_param := NULL;
your_procedure(o_param);
RETURN o_param;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
-- raise
WHEN OTHERS
THEN
-- raise
END your_function;
/
To see the results from sql do as
select your_function from dual;
Update 1
To see result in SQL Developer
Step 1
Double click on your results in SQL Developer
[Results][1]
Step 2 Single Click on the button with dots. That will pop up the values
[Grid][2]
You can Do Something Like This
select doSomething(cursor (select int_col from your_table)) colname from dual
Hope this Help

Use value of stored procedure in another stored procedure in PLSQL

Is there any way of calling a stored procedure from another stored procedure, and use retrieved value (from the second procedure) back in the first one?
-- procedure #1
create or replace procedure Proc1(p_RetValue out SomeDataType)
is
begin
-- any logic goes here
p_retValue := 5+5; -- for example. Let's assume that the SomeDataType is number;
end;
-- second procedure
create or replace procedure Proc2 is
l_variable number;
begin
Proc1(l_variable); -- the value 10 will be assigned to l_variable;
end;

Stored procedure in DB2

I have a stored procedure that starts with the following:
CREATE PROCEDURE somename.tablename (IN P_DATE DATE,
OUT P_ROWS_TO_INSERT INTEGER)
Can someone please explain the purpose of the IN and OUT parameter calls?
So SQL OUT parameter allows the stored procedure to the pass data value back to invoker. IN param is param what you adding to your stored procedure.
Basic example:
DECLARE
V_OUT INTEGER;
BEGIN
EXECUTE SomeName('22.2.2000', V_OUT);
DBMS_OUTPUT.PUT_LINE('OUT is: ' || V_OUT);
END;