Search code examples
stored-procedurestypesplsqloracle11gcursor

ORA-06502: PL/SQL: numeric or value error


I am really sorry for this. I am new oracle and I have created following block which is resulting output and then error. First cursor is generating output and then error and so second cursor is not generating output.

Please help for error free output .

thanks and regards

Error:

 END;
Error report:
ORA-06502: PL/SQL: numeric or value error
ORA-06512: at line 36
06502. 00000 -  "PL/SQL: numeric or value error%s"
*Cause:    
*Action:

Block:

  DECLARE 
      c_dbuser SYS_REFCURSOR;
      c_dbuser1 SYS_REFCURSOR;
      temp_dbuser FIT_SCHEMA.fxf_inspt_insp_main%ROWTYPE;

      year_date varchar2(20):='2014';
      typeOfGraph varchar2(50):='InspectionByInspection';
      division varchar2(20):='Division';
      subDiv varchar2(20):='ALL';
      emp varchar2(20):='';


      TYPE t_name IS RECORD(            --Error is at this line :( 
       inspectiondate varchar2(20),
        totalcount number(38),
       inspectionDesc varchar2(20)

      );
      r_name   t_name; -- name record

     TYPE t_monthwise is record (
        month_number varchar2(20),
         inspcount number(38),
      inspectionDesc varchar2(20)


     );

     t_month t_monthwise;
    BEGIN

     fit_schema.My_manager(typeOfGraph,year_date,division,subDiv,emp,c_dbuser,c_dbuser1);

      LOOP

        FETCH c_dbuser INTO  r_name ;

         EXIT WHEN c_dbuser%NOTFOUND;
       dbms_output.put_line( r_name.inspectiondate ||' '||r_name.totalcount||' '||r_name.inspectionDesc);

     END LOOP;  
      LOOP
        FETCH c_dbuser1 INTO  t_month ;


           EXIT WHEN c_dbuser1%NOTFOUND;
        dbms_output.put_line( t_month.month_number ||' '||t_month.inspcount||' '||t_month.inspectionDesc);

    END LOOP;  
       CLOSE c_dbuser;
      CLOSE c_dbuser1;


END;

Output :

13-DEC-2014 1 3#CPLD Only
13-DEC-2014 4 0#Class Only
14-DEC-2014 1 0#Class Only
15-DEC-2014 2 0#Class Only
16-DEC-2014 1 0#Class Only
17-DEC-2014 1 7#Negative Class
17-DEC-2014 9 0#Class Only
19-DEC-2014 15 0#Class Only
22-DEC-2014 1 11#65% Rule
23-DEC-2014 1 8#XLGH & Class
30-DEC-2014 1 0#Class Only
31-DEC-2014 1 10#Mixed Articles
31-DEC-2014 3 0#Class Only
02-JAN-2015 2 0#Class Only
05-JAN-2015 2 0#Class Only
07-JAN-2015 2 9#XLGH Only
07-JAN-2015 3 1#Class & Reweigh
07-JAN-2015 1 10#Mixed Articles
07-JAN-2015 4 0#Class Only
07-JAN-2015 2 11#65% Rule
08-JAN-2015 5 0#Class Only
08-JAN-2015 1 9#XLGH Only
09-JAN-2015 1 3#CPLD Only
09-JAN-2015 1 11#65% Rule
09-JAN-2015 4 0#Class Only
09-JAN-2015 1 1#Class & Reweigh
12-JAN-2015 1 5#CCD Only
12-JAN-2015 1 3#CPLD Only
19-JAN-2015 1 11#65% Rule
20-JAN-2015 1 0#Class Only
21-JAN-2015 4 0#Class Only
23-JAN-2015 1 10#Mixed Articles
26-JAN-2015 1 7#Negative Class
26-JAN-2015 2 0#Class Only
27-JAN-2015 1 3#CPLD Only
27-JAN-2015 3 0#Class Only
27-JAN-2015 1 6#CCD & Class
28-JAN-2015 6 0#Class Only
29-JAN-2015 3 0#Class Only
29-JAN-2015 1 5#CCD Only
30-JAN-2015 1 3#CPLD Only
30-JAN-2015 1 1#Class & Reweigh
01-FEB-2015 3 0#Class Only
02-FEB-2015 1 4#CPLD & Class
02-FEB-2015 1 8#XLGH & Class
04-FEB-2015 1 0#Class Only

Queries from Procedure :-

SELECT TO_CHAR(MAINTABLE.INSP_CREATED_TMSTP,'dd-MON-yyyy') inspectiondate,  --varchar2
    COUNT(MAINTABLE.insp_id) AS totalinspection, --count is number
    MAINTABLE.insp_type_id ||'#'||ISNPTYPETABLE.INSP_TYPE_DESC AS INSPECTIONDESCRIPTION-- varchar2
  FROM FIT_SCHEMA.fxf_inspt_insp_main MAINTABLE
  JOIN FIT_SCHEMA.fxf_inspt_emp_detail EMPTABLE
  ON(MAINTABLE.inspector_emp_nbr=EMPTABLE.inspector_emp_nbr)
  JOIN FIT_SCHEMA.fxf_inspt_drop_down DROPDOWNTABLE
  ON (emptable.division_id = DROPDOWNTABLE.drop_down_id)
  JOIN FIT_SCHEMA.fxf_inspt_insp_type ISNPTYPETABLE
  ON(MAINTABLE.insp_type_id                =isnptypetable.insp_type_id)
  WHERE dropdowntable.is_active_flg          = 1
  AND MAINTABLE.STATUS_ID                  = 8
  AND MAINTABLE.insp_type_id              IN (0,1,3,4,5,6,7,8,9,10,11,12) 
  AND TRUNC(MAINTABLE.insp_created_tmstp) between TRUNC(EMPTABLE.EFFECTIVE_FROM_TMSTP)
  And Nvl(Trunc(Emptable.Effective_To_Tmstp),Sysdate)  AND UPPER(dropdowntable.drop_down_grp) =UPPER('division') AND  TRUNC(MAINTABLE.insp_created_tmstp) between '01-JUN-14' AND '31-MAY-15' GROUP BY TO_CHAR(MAINTABLE.INSP_CREATED_TMSTP,'dd-MON-yyyy')
       , TRUNC(MAINTABLE.INSP_CREATED_TMSTP), MAINTABLE.insp_type_id  ||'#'|| ISNPTYPETABLE.INSP_TYPE_DESC ORDER BY TRUNC(MAINTABLE.INSP_CREATED_TMSTP) 


SELECT TO_CHAR(MAINTABLE.insp_created_tmstp,'MM') AS MONTHNUMBER,
    COUNT(MAINTABLE.INSP_ID)                         AS INSPCOUNT ,
    MAINTABLE.insp_type_id ||'#'
    ||ISNPTYPETABLE.INSP_TYPE_DESC AS INSPECTIONDESCRIPTION
    FROM FIT_SCHEMA.fxf_inspt_insp_main MAINTABLE
  JOIN FIT_SCHEMA.fxf_inspt_emp_detail EMPTABLE
  ON(MAINTABLE.inspector_emp_nbr=EMPTABLE.inspector_emp_nbr)
  JOIN FIT_SCHEMA.fxf_inspt_drop_down DROPDOWNTABLE
  ON (emptable.division_id = DROPDOWNTABLE.drop_down_id)
  JOIN FIT_SCHEMA.fxf_inspt_insp_type ISNPTYPETABLE
  ON(MAINTABLE.insp_type_id                       =isnptypetable.insp_type_id)
  WHERE dropdowntable.is_active_flg          = 1
  AND MAINTABLE.insp_type_id     IN (0,1,3,4,5,6,7,8,9,10,11,12) 
  AND dropdowntable.is_active_flg = 1
  AND MAINTABLE.STATUS_ID         = 8
  AND TRUNC(MAINTABLE.insp_created_tmstp)    between TRUNC(EMPTABLE.EFFECTIVE_FROM_TMSTP)
  And Nvl(Trunc(Emptable.Effective_To_Tmstp),Sysdate) AND UPPER(dropdowntable.drop_down_grp) =UPPER('division')
  AND  TRUNC(MAINTABLE.insp_created_tmstp) between '01-JUN-14' 
  AND '31-MAY-15' GROUP BY TO_CHAR(MAINTABLE.insp_created_tmstp,'MM'), MAINTABLE.insp_type_id ||'#'||ISNPTYPETABLE.INSP_TYPE_DESC ORDER BY TO_CHAR(MAINTABLE.insp_created_tmstp,'MM')

Solution

  • Line 36 is:

    FETCH c_dbuser INTO  r_name ;
    

    So one of the values being returned in your c_dbuser ref cursor is too big. Your date format mask will only generate string 11 characters long so inspectiondate is (more than) adequate, and the count seems unlikely to exceed number(38) so totalcount also seems fine. Which suggests that inspectionDesc is too small for some of the values in the data.

    You're generating that from:

    MAINTABLE.insp_type_id ||'#'||ISNPTYPETABLE.INSP_TYPE_DESC
    

    If the total length of that concatenated string exceeds 20 characters then you'll get an error on fetch.

    If INSP_TYPE_DESC is defined as varchar2(20) then you aren't allowing any room for the rest; you need one extra character for the fixed #, and at least two for the insp_type_id - more if the highest ID can be three digits or more. If it isn't constrained then you'll need to decide the highest number you expect to ever see and allow for that. So if you expect up to three digits for an ID, define inspectionDesc as varchar2(24), etc.