从带有case语句ORACLE 11g的函数返回CHAR

时间:2014-05-21 20:27:15

标签: oracle function case

我试图创建一个返回角色的oracle SQL函数。下面的代码(经过一些修改)作为SQL语句运行,但是当我尝试创建一个函数(包体)时,它不会编译:

FUNCTION  GetSomeInfo(v_Memkey IN NUMBER, v_ValKey IN NUMBER,
v_ValVerKey IN NUMBER, v_ValDate IN DATE) return char is
    l_CompIndicator char(1) := 'Y';
BEGIN
    Select 
    (
      case 
        when eah.ncokey != 44799 then l_CompIndicator:='Y'
        when count (0)<=5 then l_CompIndicator:='H'
        when count(0)=6 then l_CompIndicator:='C'
     end
    ) as screenstat
   from HGUSER.detailtable ead right join hguser.answertable eah
   on ead.Memkey=eah.Memkey and ead.telheaddate = eah.telheaddate 
   and to_char(ead.telheaddate,'mm/dd/yy hh24:mi:ss') = to_char (v_ValDate,'mm/dd/yy     hh24:mi:ss')  and ead.sansdata is NOT NULL
   where eah.Memkey=v_Memkey
   and eah.ValKey=v_ValKey
   and eah.ValVerKey=v_ValVerKey
   group by eah.ncokey;
  return l_CompIndicator; 
END GetSomeInfo;

2 个答案:

答案 0 :(得分:0)

你需要像这样使用select:

FUNCTION GetSomeInfo(v_Memkey IN NUMBER, v_ValKey IN NUMBER, v_ValVerKey IN NUMBER, v_ValDate IN DATE) return char is l_CompIndicator char(1) := 'Y'; 
BEGIN 
  Select ( case when eah.ncokey != 44799 then 'Y' when count (0)<=5 then 'H' when count(0)=6 then 'C' end ) as screenstat 
    into l_CompIndicator 
    from HGUSER.detailtable ead right join hguser.answertable eah on ead.Memkey=eah.Memkey and ead.telheaddate = eah.telheaddate and to_char(ead.telheaddate,'mm/dd/yy hh24:mi:ss') = to_char (v_ValDate,'mm/dd/yy hh24:mi:ss') and ead.sansdata is NOT NULL where eah.Memkey=v_Memkey and eah.ValKey=v_ValKey and eah.ValVerKey=v_ValVerKey group by eah.ncokey;
    return l_CompIndicator;
END GetSomeInfo;

通常你需要在之前声明变量。

答案 1 :(得分:0)

输出应选择INTO变量如下:

FUNCTION  GetSomeInfo(v_Memkey IN NUMBER, v_ValKey IN NUMBER,
v_ValVerKey IN NUMBER, v_ValDate IN DATE) return char is
    l_CompIndicator char(1) := 'Y';
BEGIN
    Select 
    (
      case 
        when eah.ncokey != 44799 then 'Y'
        when count (0)<=5 then 'H'
        when count(0)=6 then 'C'
     end
    ) as screenstat
   into l_CompIndicator  --this assignment is necessary
   from HGUSER.detailtable ead right join hguser.answertable eah
   on ead.Memkey=eah.Memkey and ead.telheaddate = eah.telheaddate 
   and to_char(ead.telheaddate,'mm/dd/yy hh24:mi:ss') = to_char (v_ValDate,'mm/dd/yy     hh24:mi:ss')  and ead.sansdata is NOT NULL
   where eah.Memkey=v_Memkey
   and eah.ValKey=v_ValKey
   and eah.ValVerKey=v_ValVerKey
   group by eah.ncokey;
  return l_CompIndicator; 
END GetSomeInfo;

<强>参考

SELECT INTO Statement on PL/SQL Language Reference