RefCursor pl/sql
CREATE OR REPLACE Function FnLoc(p_unit VARCHAR2,
p_locationa VARCHAR2,
p_locationb VARCHAR2)
RETURN SYS_REFCURSOR
IS
LocResultSet SYS_REFCURSOR;
BEGIN
Open LOcResultSet is
select col1,col2,col3,col4,col5
from table_nm
where unit=punit
order by col1,col2; ---query1
Return LOcResultSet;
END;
Now I have to also add following conditions for p_locationa and plocationb in query1(refcursor)
IF p_locationa is null and p_locationz is null then
execute query1
if p_locationa is not null and p_locationb is null then
locationa=plocationa or locationb=plocationa
if p_locationa is null and p_locationb is not null then
locationa=plocationb or locationb=plocationb
if p_locationa is not null and p_locationb is not null then
locationa=plocationa and locationb=plocationb.
Please suggest.
Thanks.
before opening the ref cursor ,u can check the following conditions i.e.
IF p_locationa is null and p_locationz is null
THEN
Open LOcResultSet is
select col1,col2,col3,col4,col5
from table_nm
where unit=punit
order by col1,col2; ---query1
ELSIF p_locationa is not null and p_locationb is null
THEN
locationa := plocationa ;
locationb := plocationa;
ELSIF p_locationa is null and p_locationb is not null
THEN
locationa := plocationb ;
locationb := plocationb;
ELSIF p_locationa is not null and p_locationb is not null
THEN
locationa := plocationa;
locationb := plocationb;
ELSE
NULL;
END IF;
I think so above code satisfy your answer, otherwise just more clarify your query.
Similar Messages
-
Migration Workbench Ver 1.2.2 has migrated an SQL Server 6.5
strored procedure as a package containing only the Cursor
variable and a procedure with INOUT parameter with packaged
cusorvariable as one of the parameters to Oracle 8.0.5.
How do you execute this procedure from SQL + and from another
PL/SQL block where you have to retrive the data elements of the
cursor.(ie.How do you input the cursor variable parameter in the
EXECUTE stored procedure command.)An example of the type is
appreciated.
nullSurendra,
Using refcursors between procedures is covered in the 'Wrong
number or types of argument in call to stored proc' 4 jun thread,
with reference to the manuals.
Using refcursor bind variables is covered in the sqlplus user
guide and reference reproduced below (from the 8.1.5 version,
also in 8.0.5) available on line on OTN.
Hope that helps,
Turloch
Oracle Migration Workbench Team
Using REFCURSOR Bind Variables
SQL*Plus REFCURSOR bind variables allow SQL*Plus to fetch and
format the results of a SELECT statement contained in a PL/SQL
block.
REFCURSOR bind variables can also be used to reference PL/SQL
cursor variables in stored procedures. This allows you to store
SELECT statements in the database and reference them from
SQL*Plus.
A REFCURSOR bind variable can also be returned from a stored
function.
Note:
You must have Oracle7, Release 7.3 or above to assign
the return value of a stored function to a
REFCURSOR variable.
Example 3-18 Creating, Referencing, and Displaying REFCURSOR Bind
Variables
To create, reference and display a REFCURSOR bind variable, first
declare a local bind variable of the REFCURSOR datatype
SQL> VARIABLE dept_sel REFCURSOR
Next, enter a PL/SQL block that uses the bind variable in an OPEN
... FOR SELECT statement. This statement opens a cursor variable
and executes a query. See the PL/SQL User's Guide and Reference
for information on the OPEN command and cursor variables.
In this example we are binding the SQL*Plus dept_sel bind
variable to the cursor variable.
SQL> BEGIN
2 OPEN :dept_sel FOR SELECT * FROM DEPT;
3 END;
4 /
PL/SQL procedure successfully completed.
The results from the SELECT statement can now be displayed in
SQL*Plus with the PRINT command.
SQL> PRINT dept_sel
DEPTNO DNAME LOC
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
The PRINT statement also closes the cursor. To reprint the
results, the PL/SQL block must be executed again before using
PRINT.
Example 3-19 Using REFCURSOR Variables in Stored Procedures
A REFCURSOR bind variable is passed as a parameter to a
procedure. The parameter has a REF CURSOR type. First, define the
type.
SQL> CREATE OR REPLACE PACKAGE cv_types AS
2 TYPE DeptCurTyp is REF CURSOR RETURN dept%ROWTYPE;
3 END cv_types;
4 /
Package created.
Next, create the stored procedure containing an OPEN ... FOR
SELECT statement.
SQL> CREATE OR REPLACE PROCEDURE dept_rpt
2 (dept_cv IN OUT cv_types.DeptCurTyp) AS
3 BEGIN
4 OPEN dept_cv FOR SELECT * FROM DEPT;
5 END;
6 /
Procedure successfully completed.
Execute the procedure with a SQL*Plus bind variable as the
parameter.
SQL> VARIABLE odcv REFCURSOR
SQL> EXECUTE dept_rpt(:odcv)
PL/SQL procedure successfully completed.
Now print the bind variable.
SQL> PRINT odcv
DEPTNO DNAME LOC
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
The procedure can be executed multiple times using the same or a
different REFCURSOR bind variable.
SQL> VARIABLE pcv REFCURSOR
SQL> EXECUTE dept_rpt(:pcv)
PL/SQL procedure successfully completed.
SQL> PRINT pcv
DEPTNO DNAME LOC
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
Example 3-20 Using REFCURSOR Variables in Stored Functions
Create a stored function containing an OPEN ... FOR SELECT
statement:
SQL> CREATE OR REPLACE FUNCTION dept_fn RETURN -
cv_types.DeptCurTyp IS2 resultset cv_types.DeptCurTyp;
3 BEGIN
4 OPEN resultset FOR SELECT * FROM DEPT;
5 RETURN(resultset);
6 END;
7 /
Function created.
Execute the function.
SQL> VARIABLE rc REFCURSOR
SQL> EXECUTE :rc := dept_fn
PL/SQL procedure successfully completed.
Now print the bind variable.
SQL> PRINT rc
DEPTNO DNAME LOC
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
4 rows selected
The function can be executed multiple times using the same or a
different REFCURSOR bind variable.
SQL> EXECUTE :rc := dept_fn
PL/SQL procedure successfully completed.
SQL> PRINT rc
DEPTNO DNAME LOC
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
Surendra Kumar (guest) wrote:
: Migration Workbench Ver 1.2.2 has migrated an SQL Server 6.5
: strored procedure as a package containing only the Cursor
: variable and a procedure with INOUT parameter with packaged
: cusorvariable as one of the parameters to Oracle 8.0.5.
: How do you execute this procedure from SQL + and from
another
: PL/SQL block where you have to retrive the data elements of the
: cursor.(ie.How do you input the cursor variable parameter in
the
: EXECUTE stored procedure command.)An example of the type is
: appreciated.
Oracle Technology Network
http://technet.oracle.com
null -
Show refcursor returned by a PL/SQL function ?
Is it possible to show the results returned by a
PL/SQL function that returns a refcursor ?
In sqlplus it goes like this:
SQL> variable a refcursor;
SQL> execute :a := p_front.get_invoice_list;
PL/SQL procedure successfully completed.
SQL> print a;
INVOICE_ID CLIENT_ID INVOICE_D
101 100 01-APR-06
100 100 06-APR-06
If not, this would be on the top of my wishlist...
By the way: you did a good job on this tool!
Regards,
WillemIs it possible to show the results returned by a
PL/SQL function that returns a refcursor ?
In sqlplus it goes like this:
SQL> variable a refcursor;
SQL> execute :a := p_front.get_invoice_list;
PL/SQL procedure successfully completed.
SQL> print a;
INVOICE_ID CLIENT_ID INVOICE_D
101 100 01-APR-06
100 100 06-APR-06
If not, this would be on the top of my wishlist...
By the way: you did a good job on this tool!
Regards,
Willem -
How can I execute this procedure in sql developer, cursortype is refcursor
PROCEDURE GET_CNTRY(CNTRY_NME_IN IN VARCHAR2 DEFAULT NULL,
QRY_RSLT_OUT OUT CURSORTYPE) AS
BEGIN
OPEN QRY_RSLT_OUT FOR
select
CNTRY_NME,
FRGN_CNTRY_CDE,
INV_VEH_CNTRY_CDE,
FRGN_CNCY_CDE,
WTHH_PCT_COLA_CDE
FROM
CNTRY
WHERE
CNTRY_NME = NVL(CNTRY_NME_IN,CNTRY_NME);
END GET_CNTRY;In SQL Developer run the following code as script (You need to hit F5 to run it as script).
var rc refcursor
exec get_cntry (<give your cntry_nme_in>, :rc)
print :rc -
Refcursor not returning rows when called from non SQL*Plus IDE or other
Hi all,
I have a very weird problem.
We have recently performed a minor upgrade to our oracle database and are now using:
SQL> select * from v$version;
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bi
PL/SQL Release 10.2.0.5.0 - Production
CORE 10.2.0.5.0 Production
TNS for Linux: Version 10.2.0.5.0 - Production
NLSRTL Version 10.2.0.5.0 - Production
5 rows selected.We have a crystal report selecting data from a refcursor returned by a stored procedure.
The stored procedure updates data when called as well as returning the refcursor in question.
Observe the following test scenario executed in SQL*Plus:
SQL> create table testtab (teststr varchar2(100));
Table created.
Elapsed: 00:00:00.00
SQL> insert into testtab values ('X');
1 row created.
Elapsed: 00:00:00.00
SQL> create or replace procedure testtabproc (p_listcur in out sys_refcursor)
2 as
3 begin
4
5 open p_listcur for
6 select *
7 from testtab
8 where teststr = 'X';
9
10
11 update testtab
12 set teststr = 'Y';
13
14 commit;
15
16 end;
17 /
Procedure created.
Elapsed: 00:00:00.00
SQL> declare
2
3 v_list_cur sys_refcursor;
4
5 type t_out_rec is record (teststr varchar2(100) );
6
7
8
9 v_out_rec t_out_rec;
10
11 v_rec_count number := 0;
12 v_count_limit number := 10000;
13
14 begin
15
16 dbms_output.put_line('about to call proc');
17
18 testtabproc(v_list_cur);
19
20 dbms_output.put_line('about to fetch records');
21
22 fetch v_list_cur into v_out_rec;
23 while v_list_cur%found loop
24 v_rec_count := v_rec_count + 1;
25 if v_rec_count <= v_count_limit then
26 dbms_output.put_line(v_out_rec.teststr);
27 end if;
28 fetch v_list_cur into v_out_rec;
29 end loop;
30 dbms_output.put_line('complete. selected '||v_rec_count||' records.');
31
32
33 end;
34 /
about to call proc
about to fetch records
X
complete. selected 1 records.
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.00
SQL> select * from testtab;
TESTSTR
Y
1 row selected.
Elapsed: 00:00:00.00
SQL> as you can see, the cursor returns data and the table is updated.
now, our problem is with crystal and also when I use the same test case via another IDE like TOAD.
No data is returned from the list but the table is still updated.
We suspect that something is happening that is causing the table to be updated before the refcursor is opened, or at least before the predicates are applied.
has anyone else encountered this problem before?Tubby wrote:
WhiteHat wrote:
nope - it works from sqlplus itermitantly (i.e. we always get the debug output but the cursor only sometimes fetches the rows).
it is almost as if the commit is being executed before the cursor is opened.
I still havn't been able to reproduce it except with the actual scenario that I am working with...Is the code you are dealing with exactly the same as the skeleton you've posted in your original post? Do you perhaps have a generic exception catcher in there somewhere (perhaps catching and hiding an ORA-01555) when the cursor is being fetched?
Not that i expect it to make any difference, but i'm curious as to why you've declared your cursor as IN / OUT ?
p_listcur in out sys_refcursor
the code structure in the real example is almost identical to that test case I produced - the exception handler is only catering for no_data_found, all other errors should be raised as normal.
edit: sorry I forgot to add - it's in/out because apparently that's what crystal reports needs in order to use the refcursor..... I'm not a crystal guy so I can't be any more specific than that sorry......
Edited by: WhiteHat on Oct 11, 2010 9:34 AM -
SQLJ : sending refcursors via out parameters of PL/SQL functions possible ?
Hello,
in SQLJ it's possible to get a refcursor back from a PL/SQL
function via
the return value(the usual case in the online examples).
Is it also possible to give a refcursor back via an out
parameter
so that the return value could be used for others ways ?
SQLJ generation in JDeveloper handles the first way, but we
didn't
get it to work with the out parameter.
Thank you for your help.It's certainly possible to have a stored procedure with an OUT parameter that is a REF CURSOR and use that to return data to JDBC. That's actually the most common way that JDBC programmers get ResultSets from stored procedures. I would have to assume that SQLJ would facilitate that?
Is there a reason that you're looking to use a stored function rather than a stored procedure? The latter seems like a much more logical construct when you have OUT parameters. I've never seen a stored function that was defined with any OUT parameters.
Justin
Distributed Database Consulting, Inc.
www.ddbcinc.com -
Java.sql.SQLException: Refcursor value is invalid
Hi,
I faced this exception. The exception does not always happen, so it is not
easy to find solution.
javax.jdo.JDODataStoreException: java.sql.SQLException: Refcursor value is
invalid [code=17442;state=null]
NestedThrowables:
java.sql.SQLException: Refcursor value is invalid
at
com.solarmetric.kodo.impl.jdbc.runtime.SQLExceptions.throwDataStore(SQLExceptions.java:23)
at
com.solarmetric.kodo.impl.jdbc.runtime.JDBCStoreManager.initialize(JDBCStoreManager.java:254)
at
com.solarmetric.kodo.runtime.StateManagerImpl.loadInitialState(StateManagerImpl.java:112)
at
com.solarmetric.kodo.runtime.PersistenceManagerImpl.getObjectByIdFilter(PersistenceManagerImpl.java:859)
at
com.solarmetric.kodo.runtime.PersistenceManagerImpl.getObjectById(PersistenceManagerImpl.java:764)
Usually, it happens after web application, WAR file, is deployed on the
Tomcat server.
After the exception is occurred, Tomcat server is restarted manually. Then
the exception never happens.
Do you have any clue?
My Env:
Apache 1.3 & Tomcat 1.4.x
MVC2 Model with Struts
Oracle 8i database
Thanks.Brient-
Can you post the complete stack trace (as well as all the nested
exceptions)?
In article <cpqh8p$3nr$[email protected]>, Brient Oh wrote:
Hi,
I faced this exception. The exception does not always happen, so it is not
easy to find solution.
javax.jdo.JDODataStoreException: java.sql.SQLException: Refcursor value is
invalid [code=17442;state=null]
NestedThrowables:
java.sql.SQLException: Refcursor value is invalid
at
com.solarmetric.kodo.impl.jdbc.runtime.SQLExceptions.throwDataStore(SQLExceptions.java:23)
at
com.solarmetric.kodo.impl.jdbc.runtime.JDBCStoreManager.initialize(JDBCStoreManager.java:254)
at
com.solarmetric.kodo.runtime.StateManagerImpl.loadInitialState(StateManagerImpl.java:112)
at
com.solarmetric.kodo.runtime.PersistenceManagerImpl.getObjectByIdFilter(PersistenceManagerImpl.java:859)
at
com.solarmetric.kodo.runtime.PersistenceManagerImpl.getObjectById(PersistenceManagerImpl.java:764)
Usually, it happens after web application, WAR file, is deployed on the
Tomcat server.
After the exception is occurred, Tomcat server is restarted manually. Then
the exception never happens.
Do you have any clue?
My Env:
Apache 1.3 & Tomcat 1.4.x
MVC2 Model with Struts
Oracle 8i database
Thanks.
Marc Prud'hommeaux
SolarMetric Inc. -
Call a procedure that returns a refcursor, use it in another proc's sql?
I have a procedure that returns a refcursor; how can I include that refcursor in a sql statement that's in another procedure, or view etc?
This is the kind of foolery I've tried so far (myproc1 returns a ref cursor):
create or replace procedure myproc2
rc ref cursor
AS
rc1 ref cursor;
begin
EXECUTE myproc1(rc1);
open rc for
select rc1.* from rc1;
end myproc2;lecaro wrote:
To reiterate, I was interested in consuming the refcursor output of one proc in another proc. I wanted to take what I regarded as the records returned by the first sproc and manipulate them using SQL in the second sproc.
Since I can't use that approach, what are some hopefully fairly straightforward solutions that might work?Something like:
create or replace
procedure p1(
p_stmt varchar2,
p_refcursor OUT sys_refcursor
is
begin
open p_refcursor for p_stmt;
end;
create or replace
procedure p2(
p_stmt varchar2
is
v_refcursor sys_refcursor;
v_var varchar2(30);
begin
p1(p_stmt,v_refcursor);
loop
fetch v_refcursor into v_var;
exit when v_refcursor%notfound;
dbms_output.put_line(v_var);
end loop;
end;
set serveroutput on
exec p2('select ename from emp where deptno = 20');
SQL> set serveroutput on
SQL> exec p2('select ename from emp where deptno = 20');
SMITH
JONES
SCOTT
ADAMS
FORD
PL/SQL procedure successfully completed.
SQL> SY. -
Select from refcursor in PL/SQL pkg
Greetings,
I would like to be able to return a set of values in some sort of temporary structure(temp table, cursor..it don't matter) to be used as part of the WHERE clause in several other queries. Normally I would just use a query in each of the other queries like so:
SELECT a.1, a.2, a.3
FROM bigtbl a,
(SELECT x, y, z FROM criteriakeys)b
WHERE a.x = b.x AND
a.y = b.y AND
a.z = b.z
SELECT c.1, c.2, c.3
FROM bigrtbl c,
(SELECT x, y, z FROM criteriakeys)b
WHERE c.x = b.x AND
c.y = b.y AND
c.z = b.z
etc...
The problem is that the query(b in the above) is a complex one that will be hitting against a very large number of records. I understand that temp tables are not supposed to be needed in Oracle. I haven't in 10 years and until now did not even know that it was possible. So I would use a refcursor BUT I don't know how to use a refcursor as the base of a SELECT something like:
rc ref cursor;
getcriteriakeys(rc); -- send in a refcursor and come back full of records
SELECT a.1, a.2, a.3
FROM bigtbl a,
(SELECT x, y, z FROM rc)b
WHERE a.x = b.x AND
a.y = b.y AND
a.z = b.z
This seems, to me anyway, to be an obvious use of refcursor but I cannot find any mention of it either way. I can infer from the way refcursors are used that they only send back a row at a time. This is nice but would produce sub-optimal results.
Any suggestions or comments will be welcome.
ThanksIf query b is really complex, takes a "while" to execute (long enough that you don't want to do it more than once in a set of statements), and not straightforward as in your example, one option would be to load the results of query b into a global temporary table - and then join to that table in your successive query.
A ref cursor is not really applicable in this context.
The routine would be something like:
insert into gtt
select * from ...; -- complex query b
-- the selects below would either be cursor for loops or single row select/into
-- (couldn't tell from your sample)
select *
from a, gtt
where a.x = gtt.x
and ...;
select *
from b, gtt
where b.x = gtt.x
and ...; -
How to view refcursor value in pl/sql
Hi,
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
i have one stored procedure like this
CREATE OR REPLACE PROCEDURE PRCURRENCYEDIT
CURRENCYCODE TBLCURRENCY.FLDCURRENCYCODE%TYPE,
CV1 OUT SYS_REFCURSOR
IS
BEGIN
OPEN CV1 FOR
SELECT FLDCURRENCYCODE,
FLDCURRENCYNAME,
FLDCURRENCYSYMBOL,
FLDPREFIXSYMBOL,
FLDEXCHANGERATE
FROM TBLCURRENCY
WHERE FLDCURRENCYCODE = CURRENCYCODE ;
END ;
i want to view the value for cv_1 ..
Thanks in advance
Regards
AriffThanks for the reply
But i have one problem with this stored procedure
PROCEDURE PRCURRENCYSEARCH
CURRENCYNAME IN NVARCHAR2 DEFAULT NULL ,
BATCHNUMBER IN NUMBER DEFAULT NULL ,
BATCHSIZE IN NUMBER DEFAULT NULL ,
RESULTCOUNT OUT NUMBER,
TOTALPAGECOUNT OUT NUMBER,
CV_1 OUT SYS_REFCURSOR
IS
V_ROWFROM NUMBER(19,0) ;
V_CURRENCYNAME TBLCURRENCY.FLDCURRENCYNAME%TYPE ;
BEGIN
V_CURRENCYNAME := NVL(CURRENCYNAME, '') || '%';
INSERT INTO GTT_CURRENCY
( FLDCURRENCYCODE )
SELECT FLDCURRENCYCODE
FROM TBLCURRENCY
WHERE (CURRENCYNAME = '%'
OR FLDCURRENCYNAME LIKE '%currencyname' )
ORDER BY FLDCURRENCYNAME ;
RESULTCOUNT := SQL%ROWCOUNT;
TOTALPAGECOUNT := FNTOTALPAGECOUNT(RESULTCOUNT, BATCHSIZE);
V_ROWFROM := FNROWFROM(BATCHNUMBER, BATCHSIZE);
OPEN CV_1 FOR
SELECT * FROM
( SELECT R.FLDCURRENCYCODE, C.FLDCURRENCYNAME
FROM GTT_CURRENCY R
JOIN TBLCURRENCY C
ON R.FLDCURRENCYCODE = C.FLDCURRENCYCODE
WHERE R.FLDROWNUMBER > V_ROWFROM
ORDER BY R.FLDROWNUMBER )
WHERE ROWNUM <= BATCHSIZE ;
END ;
when i execute this code using this code
DECLARE
CURR_CUR TBLCURRENCY%ROWTYPE;
CV1 SYS_REFCURSOR;
V1 NUMBER;
V2 NUMBER;
BEGIN
PRCURRENCYSEARCH('INR',NULL,1,V1,V2,CV1);
LOOP
FETCH CV1 INTO CURR_CUR;
EXIT WHEN CV1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(V1);
DBMS_OUTPUT.PUT_LINE(V2);
DBMS_OUTPUT.PUT_LINE(CURR_CUR.FLDCURRENCYCODE||' '||CURR_cUR.FLDROWNUMBER);
END LOOP;
END;
after execution,the following error occurs
DECLARE
ERROR at line 1:
ORA-06504: PL/SQL: Return types of Result Set variables or query do not match
ORA-06512: at line 9
Note:
desc tblcurrency
Name Null? Type
FLDCURRENCYCODE NOT NULL NUMBER
FLDCURRENCYNAME NOT NULL NVARCHAR2(100)
FLDCURRENCYSYMBOL NOT NULL NVARCHAR2(50)
FLDPREFIXSYMBOL NOT NULL NUMBER
FLDEXCHANGERATE NOT NULL NUMBER(18,6)
Thanks in advance
Regards
ariff -
CREATE OR REPLACE PACKAGE QUERIESLOOKUP_PKG
AS
TYPE PKG_REC_TYPE IS RECORD(
OP_DATE DATE);
TYPE pkgrec IS REF CURSOR RETURN pkg_rec_type;
END QUERIESLOOKUP_PKG;
CREATE OR REPLACE PROCEDURE QUERIESLOOKUP_PRC(
outrec OUT QUERIESLOOKUP_PKG.PKGREC) AS
querystr1 varchar2(4000);
BEGIN
querystr1:='select SYSDATE FROM DUAL';
EXECUTE IMMEDIATE querystr1 INTO OUTREC;
END QUERIESLOOKUP_PRC;
SET SERVEROUTPUT ON
declare
REC1 QUERIESLOOKUP_Pkg.pkgrec;
TEST REC1%ROWTYPE;
begin
QUERIESLOOKUP_PRC(REC1);
LOOP
FETCH REC1 INTO TEST;
EXIT WHEN REC1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('======' );
DBMS_OUTPUT.PUT_LINE(TEST.OP_DATE );
END LOOP;
CLOSE rec1;
END;
declare
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected NUMBER got DATE
ORA-06512: at "QUERIESLOOKUP_PRC", line 9
ORA-06512: at line 6
Hi,
I'm getting this err plz help me!
ThomasThe problem is ref cursor cannot be dynamic.Come again??
SQL> CREATE OR REPLACE FUNCTION dynamic_refcurs
2 ( rpt IN NUMBER := 0)
3 RETURN sys_refcursor
4 AS
5 querystr1 varchar2(4000);
6 q sys_refcursor;
7 BEGIN
8 IF rpt = 0 THEN
9 querystr1:='select sysdate from dual';
10 ELSIF rpt = 1 THEN
11 querystr1:='select sysdate from dual union select sysdate+1 from dual';
12 ELSE
13 querystr1:='select sysdate from dual union select sysdate+1 from dual union select sysd
ate+2 from dual';
14 END IF;
15 OPEN q FOR querystr1;
16 RETURN q;
17 END dynamic_refcurs;
18 /
Function created.
SQL>
SQL> SELECT dynamic_refcurs(1) FROM dual
2 /
DYNAMIC_REFCURS(1)
CURSOR STATEMENT : 1
CURSOR STATEMENT : 1
SYSDATE
08-NOV-04
09-NOV-04
SQL> SELECT dynamic_refcurs(2) FROM dual
2 /
DYNAMIC_REFCURS(2)
CURSOR STATEMENT : 1
CURSOR STATEMENT : 1
SYSDATE
08-NOV-04
09-NOV-04
10-NOV-04
SQL> CREATE OR REPLACE FUNCTION dynamic_refcurs
2 ( incr IN NUMBER := 0)
3 RETURN sys_refcursor
4 AS
5 querystr1 varchar2(4000);
6 q sys_refcursor;
7 BEGIN
8 querystr1:='select sysdate + :1 from dual';
9 OPEN q FOR querystr1 USING incr;
10 RETURN q;
11 END dynamic_refcurs;
12 /
Function created.
SQL> SELECT dynamic_refcurs(1) FROM dual
2 /
DYNAMIC_REFCURS(1)
CURSOR STATEMENT : 1
CURSOR STATEMENT : 1
SYSDATE+:
09-NOV-04
SQL> SELECT dynamic_refcurs(2) FROM dual
2 /
DYNAMIC_REFCURS(2)
CURSOR STATEMENT : 1
CURSOR STATEMENT : 1How much more dynamism do you require?
Cheers, APC -
Table function on a collection in Dynamic SQL
Hello,
I am trying to create a refcursor by selecting from a collection using table function.
If I use the Select statement the query executes, but if I put the Select statement in a string
the collection variable does not get resolved. The resaon I am putiing it in a string is because the
WHERE clause will be passed a parameter. The code below is an anonymous block but will be changed to a
procedure once I get it to work.
I have tried many different ways but was unsuccessful.
Please see if anybody cann assist or what I am trying to achive is not possible, so provide an alternative.
The error I am getting is
ORA-00904: "V_ALARM_REC_TABLE": invalid identifier
ORA-06512: at line 50
Thanks.
Bimal
DECLARE
TYPE c_refcurtype IS REF CURSOR;
x c_refcurtype;
p_recordset c_refcurtype;
v_rec mc2_dev2.mc2_alarm_rec_type := mc2_dev2.mc2_alarm_rec_type(null,null,null,null,null,null,null,null,
null,null,null,null,null,null,null,null,
null,null,null,null,null,null,null);
v_alarm_rec_table mc2_dev2.mc2_alarm_rec_table := mc2_dev2.mc2_alarm_rec_table();
v_select varchar2(200) := 'select a.* from ';
v_table varchar2(200) := 'table(v_alarm_rec_table) a ';
v_where varchar2(200) := 'where a.alarm_rule_def_uid = 9';
v_query varchar2(32000);
BEGIN
MC2_ALARM.create_mc2_alarm(x, 1); --- ( X is a refcursor, which I will use to populate v_alarm_rec_table a (nested table collection)
LOOP
FETCH x INTO v_rec.record_cnt,
v_rec.rn,
v_rec.alarm_precision_order,
v_rec.alarm_rule_def_uid,
v_rec.alarm_type_def_uid,
v_rec.alarm_rule_scope_uid,
v_rec.trigger_tpl_master_uid,
v_rec.alarm_scope_def_uid,
v_rec.alarm_object_uid,
v_rec.error_type,
v_rec.all_error_codes,
v_rec.enabled,
v_rec.start_hour,
v_rec.end_hour,
v_rec.day_type,
v_rec.alarm_severity_def_uid,
v_rec.on_watch_duration,
v_rec.update_on_status_change,
v_rec.log_ind,
v_rec.email_to,
v_rec.email_from,
v_rec.send_email,
v_rec.stale_period;
EXIT WHEN x%NOTFOUND;
v_alarm_rec_table.extend;
v_alarm_rec_table(v_alarm_rec_table.last) := v_rec;
END LOOP;
CLOSE x;
v_query := v_select||v_table||v_where; -- ERROR OCCURS AT THIS LINE as it cannot resolve the TABLE name v_alarm_rec_table)
dbms_output.put_line('sql: '||v_query);
OPEN p_recordset FOR v_query;
LOOP
FETCH p_recordset INTO v_rec.record_cnt,
v_rec.rn,
v_rec.alarm_precision_order,
v_rec.alarm_rule_def_uid,
v_rec.alarm_type_def_uid,
v_rec.alarm_rule_scope_uid,
v_rec.trigger_tpl_master_uid,
v_rec.alarm_scope_def_uid,
v_rec.alarm_object_uid,
v_rec.error_type,
v_rec.all_error_codes,
v_rec.enabled,
v_rec.start_hour,
v_rec.end_hour,
v_rec.day_type,
v_rec.alarm_severity_def_uid,
v_rec.on_watch_duration,
v_rec.update_on_status_change,
v_rec.log_ind,
v_rec.email_to,
v_rec.email_from,
v_rec.send_email,
v_rec.stale_period;
EXIT WHEN p_recordset%NOTFOUND;
some dbms_output statements...
END LOOP;
END;
The error I am getting is
ORA-00904: "V_ALARM_REC_TABLE": invalid identifier
ORA-06512: at line 50Thanks Timur/Solomon,
mc2_dev2 is the schema name.
mc2_alarm_rec_table is a SQL type.
Here are the scripts:
CREATE OR REPLACE TYPE MC2_DEV2.mc2_alarm_rec_type IS OBJECT
( record_cnt NUMBER,
rn number,
alarm_precision_order NUMBER(6),
alarm_rule_def_uid NUMBER(6),
alarm_type_def_uid NUMBER(6),
alarm_rule_scope_uid NUMBER(6),
trigger_tpl_master_uid NUMBER(6),
alarm_scope_def_uid NUMBER(6),
alarm_object_uid NUMBER(6),
error_type VARCHAR2(1),
all_error_codes VARCHAR2(1),
enabled VARCHAR2(1),
start_hour NUMBER(2),
end_hour NUMBER(2),
day_type NUMBER(2),
alarm_severity_def_uid NUMBER(6),
on_watch_duration NUMBER(6),
update_on_status_change VARCHAR2(1),
log_ind VARCHAR2(1),
email_to VARCHAR2(128),
email_from VARCHAR2(128),
send_email VARCHAR2(1),
stale_period NUMBER(6)
CREATE OR REPLACE TYPE MC2_DEV2.MC2_ALARM_REC_TABLE IS TABLE OF MC2_DEV2.mc2_alarm_rec_type;
If I popoulate the cursor with the following code:
OPEN p_recordset FOR
select a.* from table (v_alarm_rec_table) a where a.alarm_rule_def_uid = 9;
there is no issue it works just fine.
But when when I use
OPEN p_recordset FOR v_query; ---- where v_query := v_select||v_table||v_where;
the variable v_alarm_rec_table does not get resolved.
Regards,
Bimal -
PL/SQL Ad-hoc Reporting Implementation Questions
Recently, I have been put on a development team that is developing a small reporting module for one of our applications.
I'm trying to mask the underlying structure from the application by having the application run PL/SQL procedures which return REF CURSORS to the client with the requested data. Right now I'm struggling with how to implement the PL/SQL in the best fashion.
Since this is a reporting tool the user has many combinations of selections. For example at a high level this application has a combination of multiple dropdowns (4-6), radio buttons, and check boxes. As you can see this results in a high amount of possible combinations that have to be sent to the database.
Basically, the user chooses the following:
1. Columns to receive (ie different SELECT lists in the PL/SQL)
2. Specific conditions (ie different WHERE clauses in the PL/SQL)
3. Aggregate functions (SUMS, TOTALS, AVERAGES based on #1 and #2)
4. Trends based on #3.
So... with that said I see two possibilities:
1. Create a static query for each combination of parameters (in this case that would most likely result in at least 300 queries that would have to be written, possibly 600+).
The problem I see with this is that I will have to write a significant amount of queries. This is a lot of front end work that, while is tedious, could result in a better performing system because it would be a parse once, execute many scenario which is scalable.
The downside though is that if any of the underlying structure changes I have to go through and change tens of queries.
2. Use DBMS_SQL and dynamically generate the queries based on input conditions.
This approach (possibly) sacrifices performance (parse once, execute once situation), but has increased maintainability because it is more likely that I'll have to make one change vice a number of changes in scenario 1.
A downside to this is that, it may be harder to debug (and hence maintain) because the SQL is generated on the fly.
My questions to all is:
1. Which is the approach that would best manage maintainability / performance?
2. Is there any other approaches to using PL/SQL as a reporting tool that I am not thinking of?
The database is 10.2.0.3, and the 'application' is PHP 5.1 running on IIS 6.
If you need me to provide any additional information please let me know.
Thanks!Ref cursors are an ugly solution (different though in 11g).
You build a dynamic SQL. It must/should have bind variables. But a ref cursor does not allow you to dynamically bind values to bind variables. You need to code the actual bind variables into the code as part of the open cursor command. And at coding time you have no idea how many bind variables there will be in that dynamic SQL for the ref cursor.
The proper solution is DBMS_SQL as it allows exactly this. Also one of the reasons why APEX uses this for its report queries.
The only sensible way to implement this type of thing in PL/SQL is by not trying to make it generic (as one could with DBMS_SQL). Instead use polymorphisms and have each procedure construct the appropriate ref cursor with bind variables.
E.g.
SQL> create or replace package query as
2 procedure Emp( c in out sys_refcursor );
3 procedure Emp( c in out sys_refcursor, nameLike varchar2 );
4 procedure EMp( c in out sys_refcursor, deptID number );
5 end;
6 /
Package created.
SQL>
SQL> create or replace package body query as
2
3 procedure Emp( c in out sys_refcursor ) is
4 begin
5 open c for select * from emp order by 1;
6 end;
7
8 procedure Emp( c in out sys_refcursor, nameLike varchar2 ) is
9 begin
10 open c for select * from emp where ename like nameLike order by 1;
11 end;
12
13 procedure EMp( c in out sys_refcursor, deptID number ) is
14 begin
15 open c for select * from emp where deptno = deptID order by sal;
16 end;
17 end;
18 /
Package body created.
SQL>
SQL> var c refcursor
SQL> exec query.Emp( :c, 'S%' )
PL/SQL procedure successfully completed.
SQL>
SQL> print c
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 1980-12-17 00:00:00 800 20
7788 SCOTT ANALYST 7566 1987-04-19 00:00:00 3000 20
SQL> -
ORA-01008 with ref cursor and dynamic sql
When I run the follwing procedure:
variable x refcursor
set autoprint on
begin
Crosstab.pivot(p_max_cols => 4,
p_query => 'select job, count(*) cnt, deptno, row_number() over (partition by job order by deptno) rn from scott.emp group by job, deptno',
p_anchor => Crosstab.array('JOB'),
p_pivot => Crosstab.array('DEPTNO', 'CNT'),
p_cursor => :x );
end;I get the following error:
^----------------
Statement Ignored
set autoprint on
begin
adsmgr.Crosstab.pivot(p_max_cols => 4,
p_query => 'select job, count(*) cnt, deptno, row_number() over (partition by
p_anchor => adsmgr.Crosstab.array('JOB'),
p_pivot => adsmgr.Crosstab.array('DEPTNO', 'CNT'),
p_cursor => :x );
end;
ORA-01008: not all variables bound
I am running this on a stored procedure as follows:
create or replace package Crosstab
as
type refcursor is ref cursor;
type array is table of varchar2(30);
procedure pivot( p_max_cols in number default null,
p_max_cols_query in varchar2 default null,
p_query in varchar2,
p_anchor in array,
p_pivot in array,
p_cursor in out refcursor );
end;
create or replace package body Crosstab
as
procedure pivot( p_max_cols in number default null,
p_max_cols_query in varchar2 default null,
p_query in varchar2,
p_anchor in array,
p_pivot in array,
p_cursor in out refcursor )
as
l_max_cols number;
l_query long;
l_cnames array;
begin
-- figure out the number of columns we must support
-- we either KNOW this or we have a query that can tell us
if ( p_max_cols is not null )
then
l_max_cols := p_max_cols;
elsif ( p_max_cols_query is not null )
then
execute immediate p_max_cols_query into l_max_cols;
else
RAISE_APPLICATION_ERROR(-20001, 'Cannot figure out max cols');
end if;
-- Now, construct the query that can answer the question for us...
-- start with the C1, C2, ... CX columns:
l_query := 'select ';
for i in 1 .. p_anchor.count
loop
l_query := l_query || p_anchor(i) || ',';
end loop;
-- Now add in the C{x+1}... CN columns to be pivoted:
-- the format is "max(decode(rn,1,C{X+1},null)) cx+1_1"
for i in 1 .. l_max_cols
loop
for j in 1 .. p_pivot.count
loop
l_query := l_query ||
'max(decode(rn,'||i||','||
p_pivot(j)||',null)) ' ||
p_pivot(j) || '_' || i || ',';
end loop;
end loop;
-- Now just add in the original query
l_query := rtrim(l_query,',')||' from ( '||p_query||') group by ';
-- and then the group by columns...
for i in 1 .. p_anchor.count
loop
l_query := l_query || p_anchor(i) || ',';
end loop;
l_query := rtrim(l_query,',');
-- and return it
execute immediate 'alter session set cursor_sharing=force';
open p_cursor for l_query;
execute immediate 'alter session set cursor_sharing=exact';
end;
end;
/I can see from the error message that it is ignoring the x declaration, I assume it is because it does not recognise the type refcursor from the procedure.
How do I get it to recognise this?
Thank you in advanceThank you for your help
This is the version of Oracle I am running, so this may have something to do with that.
Oracle9i Enterprise Edition Release 9.2.0.8.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.8.0 - Production
I found this on Ask Tom (http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:3027089372477)
Hello, Tom.
I have one bind variable in a dynamic SQL expression.
When I open cursor for this sql, it gets me to ora-01008.
Please consider:
Connected to:
Oracle8i Enterprise Edition Release 8.1.7.4.1 - Production
JServer Release 8.1.7.4.1 - Production
SQL> declare
2 type cur is ref cursor;
3 res cur;
4 begin
5 open res for
6 'select * from (select * from dual where :p = 1) connect by 1 = 1'
7 using 1;
8 end;
9 /
declare
ERROR at line 1:
ORA-01008: not all variables bound
ORA-06512: at line 5
SQL> declare
2 type cur is ref cursor;
3 res cur;
4 begin
5 open res for
6 'select * from (select * from dual where :p = 1) connect by 1 = 1'
7 using 1, 2;
8 end;
9 /
PL/SQL procedure successfully completed.
And if I run the same thing on 10g -- all goes conversely. The first part runs ok, and the second
part reports "ORA-01006: bind variable does not exist" (as it should be, I think). Remember, there
is ONE bind variable in sql, not two. Is it a bug in 8i?
What should we do to avoid this error running the same plsql program code on different Oracle
versions?
P.S. Thank you for your invaluable work on this site.
Followup June 9, 2005 - 6pm US/Eastern:
what is the purpose of this query really?
but it would appear to be a bug in 8i (since it should need but one). You will have to work that
via support. I changed the type to tarray to see if the reserved word was causing a problem.
variable v_refcursor refcursor;
set autoprint on;
begin
crosstab.pivot (p_max_cols => 4,
p_query =>
'SELECT job, COUNT (*) cnt, deptno, ' ||
' ROW_NUMBER () OVER ( ' ||
' PARTITION BY job ' ||
' ORDER BY deptno) rn ' ||
'FROM emp ' ||
'GROUP BY job, deptno',
p_anchor => crosstab.tarray ('JOB'),
p_pivot => crosstab.tarray ('DEPTNO', 'CNT'),
p_cursor => :v_refcursor);
end;
/Was going to use this package as a stored procedure in forms but I not sure it's going to work now. -
Having issues with bind variable refcursor
Hi,
I have a procedure which returns just the list of employees from the emp table.
while executing this package, it gives me an error
Ex:
sql> variable cur refcursor;
sql> exec emp.getemplist(:cur);
error:
not all variables bound
Please let me know what is wrong here.
Thanks
Manjumanjukn wrote:
package is a simple one..
Exact Oracle version? Works fine on 10.2.0.4.0:
SQL> create or replace package body emp_pkg is
2 PROCEDURE getemplist (result_cursor OUT sys_refcursor)
3 IS
4 begin
5 open result_cursor for select * from emp where deptno=10;
6 end getemplist;
7 end emp_pkg;
8 /
Package body created.
SQL> variable cur refcursor;
SQL> exec emp_pkg.getemplist(:cur);
PL/SQL procedure successfully completed.
SQL> print cur
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7839 KING PRESIDENT 17-NOV-81 5000 10
7934 MILLER CLERK 7782 23-JAN-82 1300 10
SQL> SY.
Maybe you are looking for
-
A vertical white band has appeared on my screen and I don't know how to get rid of it.
About an inch wide, going from the top of the screen all the way to the bottom. It doesn't interfere with online activities or word processing, but it pulses and is distracting. I can't move it to the left or right. Shutting the computer down doesn't
-
the system is a new system.client800. i study on this system. when i first do the goods receive for the PO. But.when i post.the system did abap. the message is that: Runtime Errors MESSAGE_TYPE_X Date and Time 2010.02.22 19:27:32 Sho
-
CS6 is not recognizing my PNG files
Hi Everyone, I cnanot find a PNG forum for the life of me and I cannot open any of my PNG files in CS6. Where do I go for that question? Can someone please point me in the right forum?? Thanks! Angela
-
Hi, I have configured IP SLA to monitor the remote peer ip address sucessfully with icmp. Below is the configuration :- ip sla 1 icmp-echo 192.168.10.1 source-interface Loopback1 timeout 1000 threshold 2 frequency 3 ip sla schedule 1 life forever sta
-
How do i take pictures with web cam
I have a hp 2000 windows 7 with a built in web camera and im trying to find out how to take pictures with my web camera. It only lets me use it on web sites. This question was solved. View Solution.