Question : dynamic_cursor

If i have a procedure with a cursor like this
            
            CURSOR my_list
            IS
              SELECT * FROM TABLE1, TABLE 2
               WHERE.
               
               AND vendor like p_vendcode
               
               
            Users want to be able to run query with filter on vendcode and without filter.
            
            is there a way the same cursor based on the value passed with "p_vendcode" or do i need to make two cursors
            and use the appropriate one.
            
The cursor is very long with several UNIONS, etc. It would be easier if i can modify it

I think one option is to change that to bind variable and then do something like

If (p_flag is 'F') Then
open my_cursor using :p_vendcode
else
open my _cursor using 1=1
end if;
 

Answer : dynamic_cursor

Check out the following simple testcase.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
drop table tab1 purge;
create table tab1 (vendor char(1));

insert into tab1 values('A');
insert into tab1 values('B');
insert into tab1 values('D');
commit;


create or replace procedure myProc (p_filter in char, p_vendcode in char)
is
	my_var	varchar2(100);
	myCur	sys_refcursor;
	myResult char(1);
begin

	My_var := 'select vendor from tab1 where vendor like decode( ''' || p_filter || ''',''A'',''%'',''F'', ''' || p_vendcode|| ''')';

	open myCur for my_var;
	fetch myCur into myResult;
	dbms_output.put_line('got: ' || myResult);
	close myCur;
end;
/

exec myProc('A','B');
exec myProc('F','D');
Random Solutions  
 
programming4us programming4us