Oracle PL / SQL代码返回null

时间:2017-01-19 11:38:26

标签: sql oracle plsql

我在oracle PL / SQL中写这个函数,根据他们的性别和拼贴国籍,性别,等级和资格等,返回大学员工的柜台。

FUNCTION GET_EMP_COUNT_GENERIC (
   P_ORGANIZATION_ID         VARCHAR2,
   P_GRADE                   VARCHAR2,
   P_SEX                     VARCHAR2,
   P_NAT_TYPE                VARCHAR2,
   P_DATE                    DATE,
   P_EMP_TYPE                VARCHAR2,
   P_QUALIFIC_CAT            VARCHAR2 )    
RETURN NUMBER IS 
   V_COUNT   NUMBER;
BEGIN
     SELECT   COUNT (EMP.PERSON_ID)  INTO   V_COUNT 
     FROM   PER_ALL_ASSIGNMENTS_F ASS,
            PER_ALL_PEOPLE_F EMP,
            HR_ALL_ORGANIZATION_UNITS ORG,
            PAY_PEOPLE_GROUPS PPG,
            PER_GRADES GRD             
     WHERE  ASS.PERSON_ID = EMP.PERSON_ID
            AND ASS.ORGANIZATION_ID = ORG.ORGANIZATION_ID
            AND ASS.PEOPLE_GROUP_ID = PPG.PEOPLE_GROUP_ID
            AND ASS.GRADE_ID = GRD.GRADE_ID
            AND EMP.PERSON_TYPE_ID = 1145        
            AND (ASS.ORGANIZATION_ID = P_ORGANIZATION_ID OR P_ORGANIZATION_ID IS NULL)
            AND (GRD.NAME = P_GRADE                      OR P_GRADE IS NULL)
            AND (EMP.SEX = P_SEX                         OR P_SEX IS NULL)
            AND (PPG.SEGMENT2 = P_NAT_TYPE               OR P_NAT_TYPE IS NULL)
            AND (PPG.SEGMENT1 =P_EMP_TYPE                OR P_EMP_TYPE IS NULL)
            AND (XXPNU_HCM_PKG.GET_QUALIFICATION_NAME(EMP.PERSON_ID,EMP.BUSINESS_GROUP_ID) 
                              = P_QUALIFIC_CAT   OR P_QUALIFIC_CAT  IS NULL)
            AND P_DATE BETWEEN ASS.EFFECTIVE_START_DATE
            AND  ASS.EFFECTIVE_END_DATE
            AND P_DATE BETWEEN EMP.EFFECTIVE_START_DATE
            AND  EMP.EFFECTIVE_END_DATE;                                 
   RETURN V_COUNT;    
END GET_EMP_COUNT_GENERIC;

但是当我在查询中调用它时,它返回null或零值? 我不知道我的错误是什么

        SELECT
XXPNU_HCM_PKG.GET_EMP_COUNT_GENERIC (null,null,'M',null,:P_DATE,'FACULTY',null) as FACULTY_M,
XXPNU_HCM_PKG.GET_EMP_COUNT_GENERIC (null,null,'F',null,:P_DATE,'FACULTY',null) as FACULTY_F
--------------------------------TABLES------------------------------------------

  FROM                PER_ALL_ASSIGNMENTS_F ASS,
                      PER_ALL_PEOPLE_F EMP,
                      HR_ALL_ORGANIZATION_UNITS ORG,
                      PAY_PEOPLE_GROUPS PPG,
                      PER_GRADES GRD

--------------------------------- CONDITIONS -----------------------------------
  WHERE                 
                      ASS.PERSON_ID = EMP.PERSON_ID
                      AND ASS.ORGANIZATION_ID = ORG.ORGANIZATION_ID
                      AND ASS.PEOPLE_GROUP_ID = PPG.PEOPLE_GROUP_ID
                      AND ASS.GRADE_ID = GRD.GRADE_ID
                      AND EMP.PERSON_TYPE_ID = 1145

--------------------------------THIS YEAR DATE ---------------------------------

                     AND :P_DATE BETWEEN ASS.EFFECTIVE_START_DATE
                         AND  ASS.EFFECTIVE_END_DATE
                     AND :P_DATE BETWEEN EMP.EFFECTIVE_START_DATE
                         AND  EMP.EFFECTIVE_END_DATE ;

1 个答案:

答案 0 :(得分:-1)

当函数找不到与函数中的谓词匹配的行时,它将返回0。

因为你在select语句的select-section中使用了这个函数,所以当函数内发生异常时它将返回NULL。 尝试使用返回-1的exceptio处理程序。然后你知道发生了一个例外。

remenber,在select语句中调用函数时不会处理异常(在所有DML中我会说,但我肯定不知道)