0

Inside the function ,

I am having the following function,

create or replace 
FUNCTION DiffMaxMinPrice
 return double precision
is 
 diffprice double precision := 0;
 minprice long; 
 maxprice long;
 value long;
 indexid number(19,0);
begin

for row in 
 (SELECT  x.* into minprice , maxprice 
       FROM sampletable ,
            XMLTABLE ('//book'
                      PASSING sampletable.xmlcol
                      COLUMNS maxprice VARCHAR2(30) PATH '@maxprice',
                              minprice VARCHAR2(30) PATH '@minprice') x 
                              where sampletable.indexid = 2)
 LOOP
  ....
 END LOOP;


 return 1;
end;

Instead of hard coding with 2, i would like to substiute the variable idxid. I keep getting invalid number when I substituted a variable there.

indexid :=2 And the where part of the select statement as

sampletable.indexid = indexid

1 Answers1

0

You can make it as a cursor with a parameter, like this:

cursor cs ( pIndexId IN number ) is 
(SELECT  x.* into minprice , maxprice 
           FROM sampletable ,
                XMLTABLE ('//book'
                          PASSING sampletable.xmlcol
                          COLUMNS maxprice VARCHAR2(30) PATH '@maxprice',
                                  minprice VARCHAR2(30) PATH '@minprice') x 
                                  where sampletable.indexid = pIndexId );

csrom cs%rowtype;

And then you use it as:

open cs( someVariableYouNeed );
loop
    fetch cs into csrom
    exit when cs%NOT_FOUND;

    --do whatever you need here

end loop;
close cs;

If you have this variable in your code you can also use it on your code. It will work fine.

for row in 
  (SELECT  x.* into minprice , maxprice 
       FROM sampletable ,
            XMLTABLE ('//book'
                      PASSING sampletable.xmlcol
                      COLUMNS maxprice VARCHAR2(30) PATH '@maxprice',
                              minprice VARCHAR2(30) PATH '@minprice') x 
                              where sampletable.indexid = someVariableYouNeed )

If you are getting: Invalid number that means that either the column is not of type number or your variable has a value that is not a number.

Jorge Campos
  • 22,647
  • 7
  • 56
  • 87