0

在函数内部,

我有以下功能,

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;

我不想用 2 进行硬编码,而是想替换变量 idxid。当我在那里替换一个变量时,我不断得到无效的数字。

indexid :=2 而 select 语句的 where 部分为

sampletable.indexid = indexid

4

1 回答 1

0

您可以将其作为带有参数的游标,如下所示:

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;

然后你把它用作:

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

    --do whatever you need here

end loop;
close cs;

如果你的代码中有这个变量,你也可以在你的代码中使用它。它会正常工作。

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 )

如果您得到:无效数字,这意味着该列不是数字类型,或者您的变量的值不是数字。

于 2014-07-15T13:53:00.153 回答