NESTED TABLE BIND VARIABLE IN A REF CURSOR
Hi,
this works:
open c_ref for v_sql using cp_asset_type
where cp_asset_type is a nested tables passed into the proc as an 'in' parameter, but since the number of passed tables varies I loaded the nemes into an a nested table and tried the following:
-- ELSIF BIND_COUNT.COUNT = 8 THEN
-- OPEN C_REF FOR V_SQL
-- USING BIND_COUNT(1),BIND_COUNT(2),BIND_COUNT(3),BIND_COUNT(4),BIND_COUNT(5),BIND_COUNT(6),BIND_COUNT(7),BIND_COUNT(8);
-- END IF;
which produced :
ORA-22905 CANNOT ACCESS ROWS FROM A NON-NESTED TABLE ITEM
my guess is that I'm passing the varchar2 names of the nested tables and the 'using' statement needs the actual table ????
if this is true is there any way to pass a pointer for the bind variable nested tables?
Thanks,
Victor
<br>i removed the AND...but m still getting the same error.
<br>Is this a versioning problem...since urs is 9i and m using
<br>Oracle8i Enterprise Edition Release 8.1.7.4.0
<br> PROCEDURE sp_SearchByDriverName(i_C in varchar2,
i_F in varchar2,
i_LN in varchar2,
i_FN in varchar2,
o_Result out ABC_CURTYPE) is
<br> tm_corp varchar2(2);
<br> tm_fleet varchar2(6);
<br> dc ABC_curtype;
<br> begin
<br> tm_c := getformattedc(i_C);
<br> tm_f := getformattedf(i_f);
<br> if i_FN is not null then
<br> open dc for
<br> SELECT distinct b.bus_ref_access_value,
<br> i.individual_id,
<br> br.mf_driver_last_name LN,
<br> br.mf_driver_first_name FN,
<br> substr(b.bus_ref_access_value, 4, 6) FLEET1,
<br> (select '4444' from dual) --error is still coming here
<br> FROM bus_ref_access_values b,
<br> bus_ref_list brl,
<br> individual i,
<br> unit_contact_list f,
<br> unit u,
<br> bus_ref_current_prop br
<br> WHERE b.bus_ref_id = br.bus_ref_id AND
<br> b.bus_ref_id = brl.bus_ref_id AND
<br> brl.reference_id = u.reference_id AND
<br> u.unit_id = f.unit_id(+) AND
<br> i.individual_id = f.individual_id AND
<br> f.contact_type_ref = 'DR' AND
<br> br.mf_driver_last_name like i_LN || '%' AND
<br> br.mf_driver_first_name like i_FN || '%' AND
<br> substr(b.bus_ref_access_value, 1, 2) = tm_c AND
<br> substr(b.bus_ref_access_value, 4, 6) = tm_f AND
<br> b.bus_ref_access_label = 'UNIT NUMBER'
<br> ORDER BY 4, 3, 2;
<br> close dc;
<br>end if;
Similar Messages
-
Number of Bind variables passed AND Ref Cursor
hi all
well i have an interesting problem
i need to construct a query for a refcursor besade on conditions....
as:
input params: in_lname and in_fname
sql_stmt := 'Select first_name, ' || 'last_name, ' ||
'from table1';
IF in_lname is not null then
-- we are selecting guests by Lastname, Lastname & Firstname,
-- Lastname & Firstname & Zip, or Lastname & Zip
sql_stmt := sql_stmt || 'where gc_last_name = :1 ';
IF in_fname is not null then
sql_stmt := sql_stmt || ' and gc_first_name = :2 ';
END IF;
END IF;
open fm_lom_cv for sql_stmt using in_lname, in_fname;
Now here the in_lname is required BUT in_fname can be NULL
hence we dont excute or create the sql with sql_stmt || ' and gc_first_name = :2 ';
so :2 will not be used
hence it gives error IF in_fname is null ie ORA-01006: bind variable does not exist
Please suggest how to handle it....
thanksmay you have to set a some variable to check
not perfect but somthing like this
declare
ct number:=1;
sql_stmt varchar2(150);
BEGIN
sql_stmt := 'Select first_name, ' || 'last_name, ' ||
'from table1';
IF in_lname is not null then
-- we are selecting guests by Lastname, Lastname & Firstname,
-- Lastname & Firstname & Zip, or Lastname & Zip
sql_stmt := sql_stmt || 'where gc_last_name = :1 ';
ct:=1;
IF in_fname is not null then
sql_stmt := sql_stmt || ' and gc_first_name = :2 ';
ct:=2;
END IF;
END IF;
case when ct=1 then
Open fm_lom_cv for sql_stmt using in_lname;
when ct=2 then
Open fm_lom_cv for sql_stmt using in_lname,in_fname;
end case;
-
How to populate table name dynamically to a ref cursor
Hi,
I came accross with a requirement that in ref cursor how can i pass the table name
for ex
open ref_cur for select * from emp;Like that i've some 100 tables , instead of typing each and every time the table name
that should be dynamically changed
Like below
open ref_cur for select * from &tbl_nm;How can i do that??
Thank youI assume you are using SQL*Plus:
SQL> variable ref_cur refcursor;
SQL> begin
2 open :ref_cur for select * from &tbl_nm;
3 end;
4 /
Enter value for tbl_nm: emp
old 2: open :ref_cur for select * from &tbl_nm;
new 2: open :ref_cur for select * from emp;
PL/SQL procedure successfully completed.
SQL> print ref_cur
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 12/17/1980 00:00:00 800 20
7499 ALLEN SALESMAN 7698 02/20/1981 00:00:00 1600 300 30
7521 WARD SALESMAN 7698 02/22/1981 00:00:00 1250 500 30
7566 JONES MANAGER 7839 04/02/1981 00:00:00 2975 20
7654 MARTIN SALESMAN 7698 09/28/1981 00:00:00 1250 1400 30
7698 BLAKE MANAGER 7839 05/01/1981 00:00:00 2850 30
7782 CLARK MANAGER 7839 06/09/1981 00:00:00 2450 10
7788 SCOTT ANALYST 7566 04/19/1987 00:00:00 3000 20
7839 KING PRESIDENT 11/17/1981 00:00:00 5000 10
7844 TURNER SALESMAN 7698 09/08/1981 00:00:00 1500 0 30
7876 ADAMS CLERK 7788 05/23/1987 00:00:00 1100 20
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7900 JAMES CLERK 7698 12/03/1981 00:00:00 950 30
7902 FORD ANALYST 7566 12/03/1981 00:00:00 3000 20
7934 MILLER CLERK 7782 01/23/1982 00:00:00 1300 10
14 rows selected.
SQL> begin
2 open :ref_cur for select * from &tbl_nm;
3 end;
4 /
Enter value for tbl_nm: dept
old 2: open :ref_cur for select * from &tbl_nm;
new 2: open :ref_cur for select * from dept;
PL/SQL procedure successfully completed.
SQL> print ref_cur
DEPTNO DNAME LOC
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> SY. -
Selecting the contents of a table(collection) into a strong REF Cursor
I'm trying to roll some data into a table collection and return it as a strong named cursor.
I have not been able to do this successfully yet.
I have tried casting the table and I couldn't get that to work either.
I have included the whole procedure but here is the line I am getting errors on:
SELECT * bulk collect into o_response_data_cur from table (response_data_tbl);
Any help on this would be great.
P.S. - As this is being picked up by BizTalk I can't return a table.
Thanks,
Todd M
PROCEDURE create_customer (
i_interface_hdr IN BizTalk_TestCustomer.interface_hdr_rec,
i_customer_rec IN BizTalk_TestCustomer.customer_rec,
i_address_cur IN BizTalk_TestCustomer.CUR_Addresses,
i_contact_cur IN BizTalk_TestCustomer.CUR_Contact,
o_interface_status OUT varchar2,
o_response_data_cur OUT BizTalk_TestCustomer.CUR_CreateCustResponse)
IS
l_response_rec create_cust_response_rec;
response_data_tbl create_cust_response_tbl;
BEGIN
FOR i IN 1 .. 10
LOOP
l_response_rec.ERROR_TYPE := 'Pre-Validation Error';
l_response_rec.ERROR_CODE := 'DUMMY-' || i;
l_response_rec.error_message := 'Test Error Message-' || i;
response_data_tbl (i) := l_response_rec;
END LOOP;
SELECT * bulk collect into o_response_data_cur from table (response_data_tbl);
o_interface_status := 'FAILURE';
END create_customer;
END BizTalk_TestCustomer;
Here is the important Spec info:
TYPE create_cust_response_rec
IS
RECORD (
orig_system_party_ref varchar2 (240),
orig_system_cust_acct_ref varchar2 (240),
orig_system_site_ref varchar2 (240),
oracle_party_id number,
oracle_customer_id number,
oracle_site_id number,
ERROR_TYPE strar_cust_intf_err.ERROR_TYPE%TYPE,
ERROR_CODE strar_cust_intf_err.ERROR_CODE%TYPE,
error_message strar_cust_intf_err.error_message%TYPE
TYPE CUR_Addresses IS REF CURSOR RETURN BizTalk_TestCustomer.address_rec;
TYPE CUR_Contact IS REF CURSOR RETURN BizTalk_TestCustomer.contact_rec;
TYPE CUR_CreateCustResponse IS REF CURSOR RETURN BizTalk_TestCustomer.create_cust_response_rec;
TYPE create_cust_response_tbl
IS
TABLE OF create_cust_response_rec
INDEX BY binary_integer;I think this is one of the most complicated one to develop and execute perfectly. ;)
Here is one such case ->
satyaki>
satyaki>select * from v$version;
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Prod
PL/SQL Release 10.2.0.3.0 - Production
CORE 10.2.0.3.0 Production
TNS for 32-bit Windows: Version 10.2.0.3.0 - Production
NLSRTL Version 10.2.0.3.0 - Production
Elapsed: 00:00:00.55
satyaki>
satyaki>
satyaki>create or replace type d_obj as object
2 (
3 buff varchar2(310)
4 );
5 /
Type created.
Elapsed: 00:00:00.05
satyaki>
satyaki>
satyaki>create or replace type d_rec as table of d_obj;
2 /
Type created.
Elapsed: 00:00:01.14
satyaki>
satyaki>
satyaki>
satyaki>
satyaki>create or replace function pipe_buff(e_sal in number)
2 return d_rec
3 pipelined
4 is
5 cursor c1
6 is
7 select d_obj(
8 ename||' Joined On '||to_char(hiredate,'DD-MON-YYYY hh24:mi:ss')
9 ) str
10 from emp
11 where sal > e_sal;
12
13 r1 c1%rowtype;
14 begin
15 for r1 in c1
16 loop
17 pipe row(r1.str);
18 end loop;
19 return;
20 end;
21 /
Function created.
Elapsed: 00:00:01.69
satyaki>
satyaki>
satyaki>
satyaki>create or replace procedure gen_cur_pipe(
2 s_sal in number,
3 rc in out sys_refcursor
4 )
5 is
6 str1 varchar2(500);
7 begin
8 str1 := 'select *
9 from table(cast(pipe_buff('||s_sal||') as d_rec))';
10
11 open rc for str1;
12 exception
13 when others then
14 dbms_output.put_line(sqlerrm);
15 end;
16 /
Procedure created.
Elapsed: 00:00:00.05
satyaki>
satyaki>
satyaki>
satyaki>create table test_dual
2 (
3 dummy varchar2(310)
4 );
Table created.
Elapsed: 00:00:00.10
satyaki>
satyaki>
satyaki>
satyaki>declare
2 rec_x test_dual%rowtype;
3 w sys_refcursor;
4 begin
5 dbms_output.enable(1000000);
6 gen_cur_pipe(&num,w);
7 loop
8 fetch w into rec_x;
9 exit when w%notfound;
10 dbms_output.put_line('Employee Special Deatils: '||rec_x.dummy);
11 end loop;
12 close w;
13 exception
14 when others then
15 dbms_output.put_line(sqlerrm);
16 end;
17 /
Enter value for num: 1000
old 6: gen_cur_pipe(&num,w);
new 6: gen_cur_pipe(1000,w);
Employee Special Deatils: SATYAKI Joined On 02-NOV-2008 12:07:30
Employee Special Deatils: SOURAV Joined On 14-SEP-2008 00:07:21
Employee Special Deatils: WARD Joined On 22-FEB-1981 00:00:00
Employee Special Deatils: JONES Joined On 02-APR-1981 00:00:00
Employee Special Deatils: MARTIN Joined On 28-SEP-1981 00:00:00
Employee Special Deatils: BLAKE Joined On 01-MAY-1981 00:00:00
Employee Special Deatils: CLARK Joined On 09-JUN-1981 00:00:00
Employee Special Deatils: SCOTT Joined On 19-APR-1987 00:00:00
Employee Special Deatils: KING Joined On 17-NOV-1981 00:00:00
Employee Special Deatils: TURNER Joined On 08-SEP-1981 00:00:00
Employee Special Deatils: ADAMS Joined On 23-MAY-1987 00:00:00
Employee Special Deatils: FORD Joined On 03-DEC-1981 00:00:00
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.30
satyaki>
satyaki>
satyaki>/
Enter value for num: 4000
old 6: gen_cur_pipe(&num,w);
new 6: gen_cur_pipe(4000,w);
Employee Special Deatils: SATYAKI Joined On 02-NOV-2008 12:07:30
Employee Special Deatils: SOURAV Joined On 14-SEP-2008 00:07:21
Employee Special Deatils: CLARK Joined On 09-JUN-1981 00:00:00
Employee Special Deatils: KING Joined On 17-NOV-1981 00:00:00
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.13
satyaki>I'm not so sure about the performance.
Regards.
Satyaki De. -
How to represent Nested table as variable/Object in OCI
Hi All,
I'm new to OCI.
I've following nested table in my database.
Nested table:
create type type1 as object (name varchar2(20));
create type type2 as table of type1;
create table table1 (col1 varchar2(20), col2 type2) nested table col2 store as table2;
Can anyone help me to present col2 as C structure/typedef so as to use it with OCIDefineObject?You can have a look at Chapter 11 of the OCI Programmer's Guide. Look at the section Collections in it. You can represent the nested table as OCITable *. Further, you can generate structure representation of your object type by using OTT. Please let us know if this answers your question. In case you are not able to proceed please let us know.
Thanks,
Sumit -
How to pass a list as bind variable?
How can I pass a list as bind variable in Oracle?
The following query work well in SQL Developer if I set ":prmRegionID=2".
SELECT COUNTRY_ID,
COUNTRY_NAME
FROM HR.COUNTRIES
WHERE REGION_ID IN (:prmRegionID);
The problem is that I can't find how to set ":prmRegionID=2,3".
I know that I can replace ":prmRegionID" by a substitution variable "&prmRegionID". The above query work well with"&prmRegionID=2" and with "&prmRegionID=2,3".
But with this solution, I lost all advantage of using binds variables (hard parse vs soft parse, SQL injection possibility, etc.).
Can some one tell me what is the approach suggest by Oracle on that subject? My developer have work a long time too find how but didn't found any answer yet.
Thank you in advance,
MBBlais wrote:
The problem is that I can't find how to set ":prmRegionID=2,3".Wrong problem. Setting the string bind variable to that means creating a single string that contains the text "+2,3+". THE STRING DOES NOT CONTAIN TWO VALUES.
So the actual problem is that you are using the WRONG data type - you want a data type that can have more than a single string (or numeric) value. Which means that using the string (varchar2) data type is the wrong type - as this only contains a single value.
You need to understand the problem first. If you do not understand the problem, you will not realise or understand the solution too.
What do you want to compare? What does the IN clause do? It deals with, and compares with, a set of values. So it needs a set data type for the bind variable. A set data type enables you to assign multiple values to the bind variable. And use this bind variable for set operations and comparisons in SQL.
Simple example:
SQL> --// create a set data type
SQL> create or replace type TStringSet is table of varchar2(4000);
2 /
Type created.
SQL>
SQL>
SQL> var c refcursor
SQL>
SQL> --// use set as bind variable
SQL> declare
2 names TStringSet;
3 begin
4 --// assign values to set
5 names := new TStringSet('BLAKE','SCOTT','SMITH','KING');
6
7 --// use set as a bind variable for creating ref cursor
8 open :c for
9 'select * from emp where ename in (select column_value from TABLE(:bindvar))'
10 using names;
11 end;
12 /
PL/SQL procedure successfully completed.
SQL> print c
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7698 BLAKE MANAGER 7839 1981/05/01 00:00:00 2850 30
7788 SCOTT ANALYST 7566 1987/04/19 00:00:00 3000 20
7369 SMITH CLERK 7902 1980/12/17 00:00:00 800 20
7839 KING PRESIDENT 1981/11/17 00:00:00 5000 10
SQL>
SQL> --// alternative set comparison
SQL> declare
2 names TStringSet;
3 begin
4 --// assign values to set
5 names := new TStringSet('BLAKE','SCOTT','SMITH','KING');
6
7 --// use set as a bind variable for creating ref cursor
8 open :c for
9 'select * from emp where TStringSet(ename) submultiset of (:bindvar)'
10 using names;
11 end;
12 /
PL/SQL procedure successfully completed.
SQL> print c
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 1980/12/17 00:00:00 800 20
7698 BLAKE MANAGER 7839 1981/05/01 00:00:00 2850 30
7788 SCOTT ANALYST 7566 1987/04/19 00:00:00 3000 20
7839 KING PRESIDENT 1981/11/17 00:00:00 5000 10
SQL> -
Open cursor for and bind variables
Hello all,
how can I assign values to a bind variable before opening a cursor?
example code:
DECLARE
TYPE ref_cur_t IS REF CURSOR;
l_ref_cur ref_cur_t;
l_query VARCHAR2 (100)
:= 'select * from table t1 where ndx= :var_index' ;
BEGIN
-- assign a value to :var_index
OPEN l_ref_cur FOR l_query;
END;Thanks!Something like this ->
scott@ORCL>
scott@ORCL>DECLARE
2 l_ref_cur sys_refcursor;
3 l_query VARCHAR2 (100) := 'select * from emp where empno = :var_index';
4 l_empno number(4);
5 BEGIN
6 l_empno := &emno;
7 -- assign a value to :var_index
8 OPEN l_ref_cur FOR l_query using l_empno;
9 END;
10 /
Enter value for emno: 7698
old 6: l_empno := &emno;
new 6: l_empno := 7698;
PL/SQL procedure successfully completed.
Elapsed: 00:00:19.78
scott@ORCL>Regards.
Satyaki De. -
URGENT!!passing variables to ref-cursor stored procedures
I have build forms6 block base ona stored procedures with ref
cursor.
But....it's impossible to pass variables from block.item to
in-out parameter of ref cursor..to make a where clause for
example!!
I've tried all..but nothing happens..
Can someone help me?
Thanks..
nullManish Wadhwa (guest) wrote:
: Gigi (guest) wrote:
: : I have build forms6 block base ona stored procedures with ref
: : cursor.
: : But....it's impossible to pass variables from block.item to
: : in-out parameter of ref cursor..to make a where clause for
: : example!!
: : I've tried all..but nothing happens..
: : Can someone help me?
: : Thanks..
: >>
: It is not possible to send values as parameter to the stored
: procedure because, oracle uses the trigger query-procedure for
: calling the stored procedure and it does not entertain any
: changes to that trigger. This is a problem with block based on
: stored procedure, we have also tried it with table of records
: instead of ref cursor, but doesn't work.
: Manish
Thanks Manish..
i was afraid about that..
But ..i ask to myself(retoric question..) it's possible the
development oracle team build a "black box" like a stored
procedure with ref o table of records who permit to select
million of records in few second( i selected 5 million of records
in 2 seconds..) and cannot permit to passing variables to these
cursor.. every end users production forms must be working in
this way..I don't understand..
Gigi
null -
Parallel piplelined function not parallelizing with ref cursor
RDBMS 11.2.0.3
I have a function with the following signature
function to_file (
p_source in sys_refcursor
, p_file_name in varchar2
, p_directory in varchar2 default 'DD_DUMP'
return dd_dump_ntt
pipelined
parallel_enable ( partition p_source by any )
authid current_user;The function works in parallel when I use a cursor expression like this
begin
for rec in ( select *
from table(dd_dump.to_file( cursor(select /*+ parallel(i 4) */ c1||chr(9)||c2 from mytable i), 'f.out' ))
loop
dbms_output.put_line(rec.file_name || chr(9) || rec.num_records );
end loop;
end;
f.out_162 276234
f.out_213 280399
f.out_230 286834
f.out_70 289549But when I use a refcursor, it does not run in parallel
declare
rc sys_refcursor;
begin
open rc for 'select /*+ parallel(i 4) */ c1||chr(9)||c2 from mytable i';
for rec in ( select *
from table(dd_dump.to_file( rc, 'f.out' ))
loop
dbms_output.put_line(rec.file_name || chr(9) || rec.num_records );
end loop;
end;
f.out_914 1133016Is this an expected behavior or am I doing something wrong? How can I use the function when the client returns the SQL statement as a character string
Edited by: Sanjeev Chauhan on Mar 9, 2012 11:54 AMSanjeev Chauhan wrote:
I am not performing any DML in the pipelined function. If you read the note carefuly it shows parallel_enable works only when you use:
table(table_function(<font color=red>CURSOR</font>(select...)))and not when you use
table(table_function(<font color=red>ref-cursor-name</font>))SY. -
Portal bind variables in Dynamic Pages
I have two bind variables in a dynamic Page:
Variable Default Value
:p_lrg_item_id 12153
:p_lrg_color 'Rock/Black'
I put the two variables in the where clause of my select
statement:
<ORACLE>
Select distinct
prod.prfield5,
prdat.paval
from product@ecom2 prod,
prodatr@ecom2 prdat
where prod.prprfnbr = :p_lrg_item_id
and prdat.paprnbr = prod.prrfnbr
and prod.prpub = 1
and prdat.paname = 'color'
and prdat.paval = :p_lrg_color;
Then when I run the Page the query does not return any rows. So
I tested my query on the database and it returned the row. So I
took out the "and prdat.paval = :p_lrg_color" and the query
returned all of the rows. I also put the following line in the
code to view the variable: "htp.p('<p>'||upper(:p_lrg_color)
||'</p>')" and it printed 'Rock/Black' on my page. Has anyone
else ran into this problem with bind variables.
Thanks SteveWhy are you using dynamic sql for that?
Is it really necessary to proceed as such?
It will be better to use Static PL/SQL(stored procedures , functions and package) as far as there will be an "auto binding". If you use static SQL you will not have to care about using bind variables inside your PL/SQL code. All you have to do is to make sure you will call your stored PL/SQL procedure using bind values in the input parameters
see the following blog article for more details
http://hourim.wordpress.com/2011/06/16/bind-variable-shared-pool-and-cursor-sharing-parameter/
Best regards
Mohamed Houri
www.hourim.wordpress.com -
Hi,
I would like to be able to store results of a query in the database. So far, I believe the best method is to store it as a nested table so that I may refer to it by criteria (e.g. date, name, etc.). I am somewhat new to Oracle and I'm not sure how to do this.
The query will always return the same columns, so that should make it easier for me. Also, I need to be able to query the data that is stored into an APEX report region.
Can someone tell me if this is the best method to do this? Also, is there a tutorial anywhere that explains how to do what I want?
Thanks in advance,
-Davidwould like to be able to store results of a query in the database. So far,
I believe the best method is to store it as a nested table so that I may refer
to it by criteria (e.g. date, name, etc.). I am somewhat new to Oracle and
I'm not sure how to do this.Given that you're new to Oracle how did you decide the best approach was a nested table? As most people who know anything at all about Oracle would take the opposite view (as Dan has done).
Let's drill down into your requirements some more. How long do what to retain the results for? Do you want to retain the results of many queries? If so, will these queries be the same shape (same number and type of columns) or will they be different? Do you want one user to be able to see the results of another user's queries? Do you want to associate metadata with these result sets?
In fact, why do you want to be able to store these result sets at all? Oracle is actually pretty good at caching data, especially in 11g. So you may be re-inventing a wheel and designing it wonky at that.
Cheers, APC
Blog : http://radiofreetooting.blogspot.com/ -
How to convert the varray to ref cursor
Hi,
Is there any way to convert varray to ref cursor....
i dont want to use any table or record as an ref cursor..
i just want to create a procedure which returns a ref cursor..
below is the sample procedure for it..
create or replace procedure FETCH_DATA1
tab_name in varchar2,
p_recordset1 OUT fetch_data_pak.ref_cursor
AS
type v_array1 is varray(1000) of t_transaction%rowtype;
v_array2 v_array1;
cursor s1 is select * from t_transaction where lastupdate_date > '08-Aug-09';
begin
open s1;
fetch s1 bulk collect into v_array2 limit 100;
close s1;
select * from table(cast (v_array2 as p_recordset1));
end FETCH_DATA1;
I need to convert the varray to ref cursor.....Why put it into a varray at all?
You can just open the ref cursor for that select you are desiring.
And if you need to limit it to returning just the first 100 (as your code seems to imply), wrap it and filter on rownum.
Something similar to this:
create or replace procedure FETCH_DATA1
tab_name in varchar2,
p_recordset1 OUT fetch_data_pak.ref_cursor
AS
begin
open p_recordset1 for
select /*+ first_rows(100) */ * from (
select * from t_transaction
where lastupdate_date > to_date('08-08-2009','DD-MM-YYYY')
--order by something
where rownum <= 100;
end FETCH_DATA1;And note, that you really should have an order by when you wish to limit yourself to "first 100 rows" - otherwise it will be "random 100 rows".
That goes for your cursor solution as well...
Edited by: Kim Berg Hansen on Nov 28, 2011 10:25 AM
Added explicit date conversion - it is bad practice not to explicitly convert dates ;-) -
Bind variables + SQL Developer
Hi,
I have a couple of very simple pl/sql scripts with bind variables (there are no cursors in the scripts) - when I execute these scripts in SQL Developer, it doesn't work as it should.
It seems as if SQL developer doesn't register the bind variables - in one script, the first execution worked ok, but after that I need to comment the bind variable declarations, otherwise it does not work. In the other script, NULL value is assigned to the variable eventhough a number literal was assigned to the variable.
When I execute the scripts in SQL*PLUS, they work just fine!
Why is this? Is there something I need to know inorder to use bind variables in SQL Developer?
Thanks!Once you use '&&', the only way to 'reset' the variables is by using UNDEFINE (just like SQL*Plus).
...Unless you run a second script from a second Tab within SQLDeveloper and the second script references the '&&' variables from the first. Then, you get prompted again. In this case, it doesn't seem to see the same '&&' variables that are set in the first script. The second tab seems to have it's own "state" -- something that doesn't happen in SQL*Plus because.......well, it doesn't have tabs.
--s -
Using BIND VARIABLES in REF CURSOR(s)
Hello I am having trouble making my pl/sql program work using bind variables.
here is a little snipit from my code:
declare
type ref_type is REF CURSOR;
ref_cursor ref_type;
summation_table varchar2(4000);
begin
summation_table := 'table_sum tsum';
open ref_cursor for
select * from :summation_table
where tsum.revenue = 1000
USING summation_table;
end;
The Error that I get is
"bad bind variable 'summation_table'"
could someone please help? I think the way 'tsum' is used could be a problem, but I don't know.SQL> CREATE TABLE TABLE_SUM(REVENUE NUMBER(10),
2 OTHER NUMBER(10));
Table created.
SQL> INSERT INTO TABLE_SUM VALUES(1000,1);
1 row created.
SQL> INSERT INTO TABLE_SUM VALUES(1000,2);
1 row created.
SQL> variable alpha refcursor
SQL> INSERT INTO TABLE_SUM VALUES(2000,3);
1 row created.
SQL> DECLARE
2 summation_table varchar2(30) := 'table_sum tsum';
3 PROCEDURE MYTEST(p_out out sys_refcursor)
4 IS
5 BEGIN
6 OPEN p_out for 'select * from '||summation_table||
7 ' where tsum.revenue = :val' using 1000;
8 END;
9 BEGIN
10 MYTEST(:alpha);
11 END;
12 /
PL/SQL procedure successfully completed.
SQL> print alpha
REVENUE OTHER
1000 1
1000 2
SQL> -
Version details
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
PL/SQL Release 10.2.0.4.0 - Production
CORE 10.2.0.4.0 Production
TNS for Solaris: Version 10.2.0.4.0 - Production
NLSRTL Version 10.2.0.4.0 - ProductionBelow is the procedure where I'm using ref cursor
CREATE OR REPLACE PROCEDURE ref_sample
p_account_nbr in varchar2,
p_ref_out out sys_refcursor
IS
BEGIN
OPEN p_ref_out FOR
SELECT account_nbr,status,(
CASE
WHEN status = 'Pending' THEN
req_mail_date
WHEN status IN ('Rejected','Approved') THEN
NVL(verified_mail_date,req_mail_date)
END
)req_mail_date ,
CASE
WHEN status = 'Pending' THEN
NULL
WHEN status IN ('Rejected','Approved') THEN
NVL(verified_user_id,req_user_id)
END
)req_user_id
FROM X_tbl
WHERE account_nbr IN p_account_nbr
AND TRUNC(upload_date) = TRUNC(SYSDATE)
ORDER BY upload_date DESC ;
END;
/My input parameter p_account_nbr looks like ('a1','a2','a3')
Now,after knowing the importance of bind variables I'd like to make use of them in the above ref cursor.
But,here my input parameter is a string of varying length..either I've to go for the approach suggested here
http://asktom.oracle.com/pls/asktom/f?p=100:11:3667281145899708::::P11_QUESTION_ID:110612348061
or
http://www.dba-oracle.com/t_cursor_sharing.htm
I'm not much clear with the first approach,so I'm thinking of to modify my procedure as below
CREATE OR REPLACE PROCEDURE ref_sample
p_account_nbr in varchar2,
p_ref_out out sys_refcursor
IS
BEGIN
alter session set cursor_sharing=force;
OPEN p_ref_out FOR
SELECT account_nbr,status,(
CASE
WHEN status = 'Pending' THEN
req_mail_date
WHEN status IN ('Rejected','Approved') THEN
NVL(verified_mail_date,req_mail_date)
END
)req_mail_date ,
CASE
WHEN status = 'Pending' THEN
NULL
WHEN status IN ('Rejected','Approved') THEN
NVL(verified_user_id,req_user_id)
END
)req_user_id
FROM X_tbl
WHERE account_nbr IN p_account_nbr
AND TRUNC(upload_date) = TRUNC(SYSDATE)
ORDER BY upload_date DESC ;
alter session set cursor_sharing=exact;
END;
/Please let me know if the above modified code is fine or should I use bind variables??Also let me know better approach of both.The correct way to do this is use an array type for the input values as in this example.
SQL> create or replace procedure p
2 (
3 p_values sys.odcivarchar2list,
4 c out sys_refcursor
5 ) as
6 begin
7 open c for
8 select object_name, owner, object_type
9 from all_objects
10 where object_name in (select column_value from table(p_values));
11 end;
12 /
Procedure created.
SQL> var c refcursor
SQL> exec p (sys.odcivarchar2list('DUAL','USER_VIEWS'), :c)
PL/SQL procedure successfully completed.
SQL> print c
OBJECT_NAME OWNER OBJECT_TYPE
DUAL SYS TABLE
DUAL PUBLIC SYNONYM
USER_VIEWS SYS VIEW
USER_VIEWS PUBLIC SYNONYM
SQL> exec p (sys.odcivarchar2list('DUAL','USER_VIEWS','ALL_OBJECTS','ALL_SOURCE'), :c)
PL/SQL procedure successfully completed.
SQL> print c
OBJECT_NAME OWNER OBJECT_TYPE
DUAL SYS TABLE
DUAL PUBLIC SYNONYM
ALL_OBJECTS SYS VIEW
ALL_OBJECTS PUBLIC SYNONYM
USER_VIEWS SYS VIEW
USER_VIEWS PUBLIC SYNONYM
ALL_SOURCE SYS VIEW
ALL_SOURCE PUBLIC SYNONYM
8 rows selected.
SQL>That and other methods are described here.
http://tkyte.blogspot.com/2006/06/varying-in-lists.html
You would not use dynamic SQL.
Maybe you are looking for
-
I have a MacBook Pro. When surfing the web it will eventually slow to a crawl. When this occurs, there will be a small multi-colored wheel spinning until my latest command is handled. What is causing this and is there a way that I can modify or pr
-
Newbie Q?.. Opens in Tablet page on Desktop?
Hey guys.. have just started playing with Muse.. Watched the Terry White tutorial.. Now in muse.. Made the 3 basic pages (all default settings) First for Desktop, then Tablet, then Phone.. Added 1 small line of text eg. "This is the desktop page!" et
-
WTA: How to use output type with medium T (Task)
Dear Gurus, Can anyone shed some light how to use output medium T? I tried one but at the end the output analysis says: Task T 50000125 is not available for object category BUS2032 My task clearly calling BUS2032 with method DELETEBILLINGBLOCK Anyone
-
Error when checking out from external punch out catalog to SRM
Hi guys, We have configured an external punchout in spro. Now when in portal, I am able to access the catalog. But when i check out from catalog to srm, it is throwing error telling "HTTP 404 not found". Please help me resolve this issue. Regards, Ma
-
[SOLVED] pacman -Syu took an extremely long time today
Hey, this is the first time I've encountered such an issue: today I upgraded as usual (pacman -Syu) but the process took an extremely long time (~20 minutes). The last time I upgraded was three days ago. The culprits seem to have been the libreoffice