I have a procedure like this:
CREATE OR REPLACE PROCEDURE RIC.TEST
(P_VAR1 IN VARCHAR2, P_VAR2 IN VARCHAR2, P_VAR3 IN VARCHAR2, P_VAR4 IN VARCHAR2)
IS
L_RC SYS_REFCURSOR;
BEGIN
INSERT INTO RIC.TEMP_TABLE
SELECT * FROM RIC.TABLE WHERE COL1=P_VAR1 AND COL2=P_VAR2 AND COL3=P_VAR3 AND COL4=P_VAR4 AND ...;
OPEN L_RC
FOR 'SELECT .... FROM RIC.TEMP_TABLE WHERE ...';
...
TEMP_TABLE is a temporary table. I need to return and show second select output (SELECT .... FROM RIC.TEMP_TABLE WHERE ...) when I run this procedure. How can I do that? Is cursor wrong way for this?
Thank you.
Modify the procedure to
CREATE OR REPLACE PROCEDURE ric.test (p_var1 IN VARCHAR2,
p_var2 IN VARCHAR2,
p_var3 IN VARCHAR2,
p_var4 IN VARCHAR2,
p_cursor OUT SYS_REFCURSOR)
IS
BEGIN
INSERT INTO ric.temp_table
SELECT *
FROM ric.table
WHERE col1 = p_var1 AND col2 = p_var2 AND col3 = p_var3;
OPEN p_cursor FOR SELECT * FROM ric.temp_table;
END;
/
and execute the procedure as
VARIABLE cur REFCURSOR;
EXEC test (:cur);
PRINT cur;
If you would like to execute the procedure using SQL, wrap the procedure in a function which should be followed by calling the function in a SQL statement.
Example
Related
I have this SQL query:
select title
from DEPARTMENT;
I tried to write a stored procedure:
create PROCEDURE select_some
(whats VARCHAR2 ,c_select_some OUT SYS_REFCURSOR)
AS
BEGIN
OPEN c_select_some FOR
SELECT whats
FROM department;
END select_some;
/
But where I execute it with "title" parameter I got 8 rows with 'title' instead actual content. What's wrong?
Execution:
var whats varchar2(20)
variable whats = 'Title'
variable mycursor refcursor;
exec select_some (:whats, mycursor);
For this, you need to use dynamic SQL.
Something like this
create or replace procedure select_from_department(
col_name in varchar2,
c_res out sys_refcursor
)
is
l_sql varchar2(300);
begin
l_sql := 'select ' || dbms_assert.simple_sql_name(col_name) || ' from departments';
open c_res for l_sql ;
end;
DEMO
How I can pass a string list to stored procedure?
this is my Stored Procedure:
CREATE OR REPLACE PROCEDURE sp_search (p_name IN VARCHAR2,
r_cursor SYS_REFCURSOR)
AS
BEGIN
OPEN cursor FOR SELECT name
FROM tableN
WHERE name IN (p_name);
END;
Example:
var c_ref refcursor;
sp_search('Andy,Marty,miky',:c_ref);
print c_ref;
I think, this should do it
CREATE OR REPLACE PROCEDURE sp_search (
p_name IN VARCHAR2,
r_cursor OUT SYS_REFCURSOR)
AS
Declare
v_sql VARCHAR2(200);
BEGIN
v_sql := 'SELECT name FROM table WHERE name IN (' || p_name || ')';
OPEN r_cursor FOR v_sql;
END;
Only make sure that when you pass Andy,Marty,miky, you add ' so that final sql looks like
SELECT name FROM table WHERE name IN ('Andy','Marty','miky')
I have an SP
create or replace PROCEDURE ALTERNATE_NAME_LOOKUP
( P_NAME IN VARCHAR2,
P_TYPE IN VARCHAR2, retCursor OUT SYS_REFCURSOR
)
I didn't paste the rest of its body; The above procedure works fine on its own (with the body of course)
Now I want to call it from another stored procedure, and I want to traverse over the refcursor.
What I am doing is declaring an_last_cur SYS_REFCURSOR; and calling ALTERNATE_NAME_LOOKUP procedure as ALTERNATE_NAME_LOOKUP(p_req.LASTNAMEEXP,c_LAST, an_last_cur); It compiles.
but when I add following block -
ALTERNATE_NAME_LOOKUP('Roman Reigns','LAST',an_last_cur);
For alt in an_last_cur
Loop
DBMS_OUTPUT.PUT_LINE('ok');
end loop;
It gives compilation error -
PLS-00221: 'AN_LAST_CUR' is not a procedure or is undefined
What am I doing wrong?
create or replace procedure alternate_name_lookup
( p_name in varchar2, p_type in varchar2, retcursor out sys_refcursor )
as
begin
open retcursor for select * from user_objects ;
end;
set serveroutput on
declare
an_last_cur sys_refcursor;
type my_objects is table of user_objects%rowtype;
objects my_objects;
begin
alternate_name_lookup('Roman Reigns','LAST',an_last_cur);
fetch an_last_cur bulk collect into objects;
dbms_output.put_line(objects.count);
for indx in 1 .. objects.count
loop
dbms_output.put_line(objects(indx).object_name);
end loop;
close an_last_cur;
end;
Try this one. Hope this helps. I dont have workspace with me so pardon
syntax erro r if any.
CREATE OR REPLACE PROCEDURE test_ref_prc
( p_ref_out OUT sys_refcursor)
AS
BEGIN
OPEN p_ref_out FOR
SELECT LEVEL FROM DUAL CONNECT BY LEVEL < 10;
END;
CREATE OR REPLACE PROCEDURE test_ref2
AS
refc sys_refcursor;
num_ntt NUMBER_NTT;
BEGIN
test_ref_prc(refc);
FETCH refc BULK COLLECT INTO num_ntt;
FOR I IN num_ntt.FIRST..num_ntt.LAST LOOP
dbms_output.put_line(num_ntt(i));
END LOOP;
END;
exec test_ref2;
EDITED :
I am trying to create a procedure where a Ref Cursor is defined as an OUT parameter, my question is how do I call out that Ref Cursor when I execute it for example here is how I want to call it in my EXEC sql command :
EXEC film_not_in_stock(2,2,vcur);
Here is the procedure :
CREATE OR REPLACE PROCEDURE film_not_in_stock( p_film_id IN NUMBER, p_store_id IN NUMBER, vcur OUT SYS_REFCURSOR)
IS
cur sys_refcursor;
v_cur inventory.inventory_id%TYPE --or the same type the inventory_id column is
v_cur sys_refcursor;
BEGIN
OPEN cur FOR SELECT inventory_id
FROM inventory
WHERE film_id = p_film_id
AND store_id = p_store_id
AND inventory_id NOT IN (SELECT inventory_in_stock(inventory_id) FROM dual);
fetch cur into v_cur;
EXIT WHEN cur%NOTFOUND;
END;
/
I know there are typos in procedure as well, will appreciate if you can help me put it right.
Thank you very very much !!
Tonya
Try as
variable p_cursor REFCURSOR;
DECLARE
BEGIN
film_not_in_stock(2,2,:p_cursor);
end;
/
print p_cursor;
And modify procedure as
CREATE OR REPLACE PROCEDURE film_not_in_stock (
p_film_id IN inventory.film_id%TYPE,
p_store_id IN inventory.store_id%TYPE,
vcur OUT SYS_REFCURSOR)
AS
BEGIN
OPEN vcur FOR
SELECT inventory_id
FROM inventory
WHERE film_id = p_film_id
AND store_id = p_store_id
AND inventory_id NOT IN
(SELECT inventory_in_stock (inventory_id) FROM DUAL);
END;
/
The following will do :
var results refcursor
exec film_not_in_stock(2,2,:results);
You need to declare a variable to fetch your cursor results into...
Something like this...
CREATE OR REPLACE PROCEDURE film_not_in_stock( p_film_id IN NUMBER, p_store_id IN NUMBER, p_film_count OUT NUMBER, cur OUT SYS_REFCURSOR)
IS
cur sys_refcursor;
v_cur inventory.inventory_id%TYPE --or the same type the inventory_id column is
BEGIN
OPEN cur FOR SELECT inventory_id
FROM inventory
WHERE film_id = p_film_id
AND store_id = p_store_id
AND inventory_id NOT IN (SELECT inventory_in_stock(inventory_id) FROM dual);
--SELECT FOUND_ROWS() INTO p_film_count FROM dual; <<----- **This is the line**
fetch cur into v_cur;
EXIT WHEN %NOTFOUND;
--do whatever you want
END;
/
How can I fetch from a ref cursor that is returned from a stored procedure (OUT variable) and print the resulting rows to STDOUT in SQL*PLUS?
ORACLE stored procedure:
PROCEDURE GetGrantListByPI(p_firstname IN VARCHAR2, p_lastname IN VARCHAR2,
p_orderby IN VARCHAR2, p_cursor OUT grantcur);
PL/SQL:
SET SERVEROUTPUT ON;
DECLARE
TYPE r_cursor IS REF CURSOR;
refCursor r_cursor;
CURSOR grantCursor IS
SELECT last_name, first_name
FROM ten_year_pis
WHERE year_added = 2010;
last_name VARCHAR2(100);
first_name VARCHAR2(100);
BEGIN
OPEN grantCursor;
FETCH grantCursor INTO last_name, first_name;
WHILE grantCursor%FOUND LOOP
PMAWEB_PKG.GetGrantListByPI(last_name, first_name, 'last_name', refCursor);
--HOW DO I LOOP THROUGH THE RETURNED REF CURSOR (refCursor)
--AND PRINT THE RESULTING ROWS TO STDOUT?
FETCH grantCursor into last_name, first_name;
END LOOP;
CLOSE grantCursor;
END;
/
Note: This code is untested
Define a record for your refCursor return type, call it rec. For example:
TYPE MyRec IS RECORD (col1 VARCHAR2(10), col2 VARCHAR2(20), ...); --define the record
rec MyRec; -- instantiate the record
Once you have the refcursor returned from your procedure, you can add the following code where your comments are now:
LOOP
FETCH refCursor INTO rec;
EXIT WHEN refCursor%NOTFOUND;
dbms_output.put_line(rec.col1||','||rec.col2||','||...);
END LOOP;
You can use a bind variable at the SQLPlus level to do this. Of course you have little control over the formatting of the output.
VAR x REFCURSOR;
EXEC GetGrantListByPI(args, :x);
PRINT x;
If you want to print all the columns in your select clause you can go with the autoprint command.
CREATE OR REPLACE PROCEDURE sps_detail_dtest(v_refcur OUT sys_refcursor)
AS
BEGIN
OPEN v_refcur FOR 'select * from dummy_table';
END;
SET autoprint on;
--calling the procedure
VAR vcur refcursor;
DECLARE
BEGIN
sps_detail_dtest(vrefcur=>:vcur);
END;
Hope this gives you an alternate solution
More easier option is to use DBMS_SQL.return_result();
Lets say your package / procedure / cursor spec is as below.
create or replace PACKAGE my_package IS
TYPE my_ref_cursor_type IS REF CURSOR;
PROCEDURE my_procedure (
p_in_param1 IN VARCHAR2,
p_in_param2 IN VARCHAR2,
p_in_param3 IN VARCHAR2,
p_my_ref_cursor OUT my_ref_cursor_type,
p_err_code OUT NUMBER,
p_err_msg OUT VARCHAR2
);
END my_package;
Try this to invoke the procedure from your sql developer WORKSHEET
SET SERVEROUTPUT ON;
DECLARE
P_MY_REF_CURSOR my_schema.my_package.my_ref_cursor_type;
P_ERR_CODE NUMBER;
P_ERR_MSG VARCHAR2(200);
BEGIN
my_package.my_procedure(
'VALUE1',
'VALUE2',
'VALUE3',
P_MY_REF_CURSOR => P_MY_REF_CURSOR,
P_ERR_CODE => P_ERR_CODE,
P_ERR_MSG => P_ERR_MSG
);
DBMS_OUTPUT.PUT_LINE(P_ERR_MSG);
DBMS_OUTPUT.PUT_LINE(P_ERR_CODE);
DBMS_SQL.return_result(P_MY_REF_CURSOR);
END;
Hope this helps !
There are many ways for displaying the sys_refcursor result set and one of them that is so easy is using SQL Developer to fetch sys_refcursor and print output which are:
Create a test function to print its result
Execute the function
View the output
Verify the result set