我可以在SELECT查询中使用函数吗?

时间:2014-07-28 15:26:06

标签: sql oracle plsql etl

我可以在select语句中使用这样的函数吗?

select
myseq.nextval,
getDoctor(dbNumber,C.code,DOCCODE), 
getDoctor(dbNumber,C.code,RFDOCCODE), 
...
from charges c
where type='R';

这里getdoctor指向一个检查两个表并返回一个值的函数:

create or replace function getDoctor(dbNumber IN NUMBER,code IN VARCHAR2(10),doccode IN VARCHAR2(10), OUT DOCID VARCHAR2(10))  
is 
begin
 DOCID:= nvl((select MDOCID from NEW_PROVIDERS NP where dbNumber=NP.dbNumber and code=NP.code and doccode=NP.doccode),
    select MDOCID from ProvidersProxy P where P.originalDB=dbNumber and code=P.originalGroup and doccode=P.originalDocCode);
return DOCID;
end getDoctor;

2 个答案:

答案 0 :(得分:0)

写这个(不要忘记“p_”前缀):

create or replace function getDoctor(
    p_dbNumber IN NUMBER,
    p_code IN VARCHAR2(10),
    p_doccode IN VARCHAR2(10)
) return varchar2
is
  l_DOCID VARCHAR2(10);
begin
  begin
    select MDOCID
      into l_DOCID 
      from NEW_PROVIDERS NP 
     where p_dbNumber=NP.dbNumber 
       and p_code=NP.code 
       and p_doccode=NP.doccode
    ;
  exception
    when no_data_found then
      select MDOCID 
        into l_DOCID
        from ProvidersProxy P 
       where P.originalDB=p_dbNumber 
         and p_cpcode=P.originalGroup 
         and p_doccode=P.originalDocCode
      ;
  end;
  return l_DOCID;
end getDoctor;

答案 1 :(得分:0)

Oracle SQL可用函数的基本签名(在各处都有详细记录)的格式如下:

FUNCTION my_function (param1 datatype, param2 datatype)
RETURN datatype
IS
local_variable1 datatype;
local_variable2 datatype;
BEGIN
-- Do something awesome
return local_variable;
/* Optional Exceptions Block */
END;

所以在你的情况下(对NVL()进行一些重写)然后:

create or replace function getDoctor(p_dbNumber IN NUMBER, p_code IN VARCHAR2, p_doccode IN VARCHAR2)
return varchar2
is 
DOCID VARCHAR2(10);
begin

  select MDOCID into DOCID 
  from NEW_PROVIDERS NP 
  where p_dbNumber=NP.dbNumber 
  and p_code=NP.code 
  and p_doccode=NP.doccode;

return DOCID;
EXCEPTION
 when no_data_found then
    select MDOCID INTO DOCID 
    from ProvidersProxy P 
    where P.originalDB=p_dbNumber 
    and p_code=P.originalGroup 
    and p_doccode=P.originalDocCode;

    return DOCID;

end getDoctor;