Oracle - 此范围内不存在名称为 X 的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13583574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 01:17:35  来源:igfitidea点击:

Oracle - no function with name X exists in this scope

oracleoracle-cursor

提问by user1831003

The function is clearly there, because I can navigate to it using SQL Developer and it compiles all fine, but when I try to use the function with or without "call", it throws:

该函数显然在那里,因为我可以使用 SQL Developer 导航到它并且编译一切正常,但是当我尝试使用带有或不带有“调用”的函数时,它会抛出:

Error(36,24): PLS-00222: no function with name 'x' exists in this scope

错误(36,24):PLS-00222:此范围内不存在名称为“x”的函数

This is how the function looks like:

这是函数的样子:

create or replace function testfunction
  (
    somevalue in varchar2 
  )
  return varchar2
  AS
  cursor testcursor IS 
  select column1, column2 from table1 t
  where t.column1 = somevalue; 
  testcursorrec testcursor %rowtype;
  messaget VARCHAR2(500);
  begin
       open testcursor ; 
       fetch testcursor into testcursorrec ; 
       close testcursor ; 
       messaget := testcursor.column1;
      return messaget ;
  end;

This is how I'm calling it:

我是这样称呼它的:

messaget := testfunction(somevalue); 

where both messageT and somevalue are declared as varchar2 type.

其中 messageT 和 somevalue 都声明为 varchar2 类型。

Are cursors not allowed inside function or something like that?

函数内部是否不允许使用游标或类似的东西?

回答by DazzaL

the error would be messaget := testcursor.column1;as the cursor is closed by then (you should just use testcursorrec.column2.

错误将是messaget := testcursor.column1;因为到那时光标已关闭(您应该只使用testcursorrec.column2.

you're code isn't checking for no rows, nor duplicate rows. you can simplify this to

你的代码没有检查没有行,也没有检查重复的行。您可以将其简化为

create or replace function testfunction
  (
    somevalue in table1.column1%type
  )
  return table1.column2%type
  AS
  messaget table1.column2%type; -- use %type where possible.
  begin
    select t.column2
      into messaget
      from table1 t
     where t.column1 = somevalue
       and rownum = 1;--only if you dont care if theres 2+ rows. 
    return messaget;
  exception 
    when no_data_found
    then 
      return null; -- if you want to ignore no rows.
  end;