Oracle函数和查询返回不同的结果

3 function oracle10g

我正在使用oracle 10g数据库.

功能是:

create or replace FUNCTION FUNC_FAAL(myCode number,firstDate date
  , secondDate date) 
  RETURN INTEGER as
  rtr integer;
BEGIN
  select count(*) into rtr 
  from my_table tbl where tbl.myDateColumn between firstDate and 
          secondDate and tbl.kkct is null and tbl.myNumberColumn  = myCode ;
  return (rtr);
END FUNC_FAAL;
Run Code Online (Sandbox Code Playgroud)

此函数返回117177作为结果.

但是如果我单独在函数中运行相同的查询;

select count(*)
from my_table tbl 
where tbl.myDateColumn between firstDate and secondDate 
and tbl.kkct is null and tbl.myNumberColumn  = myCode ;
Run Code Online (Sandbox Code Playgroud)

我得到不同的结果11344(这是正确的).

可能是什么问题?

谢谢.

Gar*_*ers 6

你已经混淆了你的代码,我怀疑在这个过程中隐藏了这个问题.我怀疑你的代码更像

 create or replace FUNCTION FUNC_FAAL(myNumberColumn number,firstDate date
  , secondDate date) 
  RETURN INTEGER as
  rtr integer;
BEGIN
  select count(*) into rtr 
  from my_table tbl where tbl.myDateColumn between firstDate and 
          secondDate and tbl.kkct is null and tbl.myNumberColumn  = myNumberColumn ;
  return (rtr);
END FUNC_FAAL;
Run Code Online (Sandbox Code Playgroud)

其中参数或局部变量与表中的列具有相同的名称.在SQL中,表列优先,因此不使用该变量,并将列与自身进行比较,从而提供更多匹配.

最好为变量和参数(例如v_和p_)添加前缀以避免此类问题.