Oracle SQL:使用游标的函数,没有找到数据返回值

时间:2015-07-08 12:37:18

标签: sql oracle function cursor

我正在尝试编写一个函数来从字符串中提取代码,根据该代码在表中查找值并从查找表中返回值。

这是我的功能:

   create or replace function MATHS_F 
   (id IN varchar2)

   RETURN number
   is res_code VARCHAR2(2);
   points number(3,0);     

   cursor c1 is
   select substr(substr(lcstring, instr(lcstring,'02')+2),1,2) 
   from sdata s
   where s.id = id;

BEGIN

   open c1;
   fetch c1 into res_code;

   select c_points into points from lcresults l 
   where l.lc_code = res_code;

RETURN points;

close c1;

exception when no_data_found then
   return 99;

END;

我正在运行此语句来测试我的函数:

select id, lcstring,
substr(substr(lcstring, instr(lcstring,'02')+2),1,2) RESULT,
MATHS_F(id) as POINTS
from sdata
where id = 'S00101620'
;

此查询的结果是

S00101620    11OT02OV29HV05OT03OX30OQ  OV   99

对于LC_CODE' OV'在LCRESULTS表中C_POINTS中的值为35 ... 为什么我的函数没有找到数据?

1 个答案:

答案 0 :(得分:0)

python setup.py build_ext --inplace
Compiling bigNumbers.pyx because it changed.
[1/1] Cythonizing bigNumbers.pyx
running build_ext
building 'bigNumbers' extension
D:\WinPython3610\cgg64Bit\bin\gcc.exe -mdll -O -Wall -ID:\WinPython3610\python-3
.6.1.amd64\include -ID:\WinPython3610\python-3.6.1.amd64\include -c bigNumbers.c
 -o build\temp.win-amd64-3.6\Release\bignumbers.o
writing build\temp.win-amd64-3.6\Release\bigNumbers.cp36-win_amd64.def
D:\WinPython3610\cgg64Bit\bin\gcc.exe -shared -s build\temp.win-amd64-3.6\Releas
e\bignumbers.o build\temp.win-amd64-3.6\Release\bigNumbers.cp36-win_amd64.def -L
D:\WinPython3610\python-3.6.1.amd64\libs -LD:\WinPython3610\python-3.6.1.amd64\P
Cbuild\amd64 -lpython36 -lvcruntime140 -o L:\User\neon3_worksp
ace\CythonSource\src\bigNumbers.cp36-win_amd64.pyd

此查询的最后一行不使用您的输入参数。 尝试更改参数名称或将cursor c1 is select substr(substr(lcstring, instr(lcstring,'02')+2),1,2) from sdata s where s.id = id; 更改为where s.id = id;

另外一点是你没有正确关闭光标,因为你的where s.id = maths_f.id;语句是在返回之后..如果CLOSE被引发也不会关闭

NO_DATA_FOUND