Question : Counting the Character Length of a Long Field - Error

Hi, I am trying to count the length of fields that are of a long data type.  I wrote a function called find_lenght2 and I call it from another procedure.  It works, but eventually errors after 500,000 plus rows and says "ORA-06502: PL/SQL: numeric or value error".  Please help.

I am enclosing a copy of the find_lenth function and the procedure I am using to call it below.

Thanks much.

Find Length Function:
CREATE or REPLACE function Find_Length2
   ( geo_log_id number)
RETURN number
IS
    long_var LONG;
BEGIN
    SELECT long_desc INTO long_var
    FROM logs
    WHERE log_id = geo_log_id;
    return length(long_var);
END;
/
Procdure to call the Function:
CREATE OR REPLACE PROCEDURE GEO_COUNT_DB_LONG_SIZE
is
cursor log_id_cursor is
  select log_id from logs
        where  sti_casetype in (
                                      select sti_casetype
                                            from ford_acs_casetypes
                                            where transition_phase = 'DB'
                                            and ford_contact_type_cd <> 'B'
                                            );
  local_log_id number;
  /* long_desc_length number(20,0); */
  long_desc_length number;
begin
  open log_id_cursor;
  fetch log_id_cursor into local_log_id;
          while log_id_cursor%found
           loop
  select find_length2(local_log_id) into long_desc_length from logs WHERE LOG_ID = local_log_id;
          insert into geo_field_count2 (case_no, field_size) values (local_log_id, long_desc_length);
       commit;
      fetch log_id_cursor into local_log_id;
   end loop;
 end;
/

Error Encountered after being successful on 500,000 plus rows:

SQL> truncate table geo_field_count2;

Table truncated.

SQL> exec geo_count_db_long_size;
BEGIN geo_count_db_long_size; END;

*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error
ORA-06512: at "SCOPUS1_APP.FIND_LENGTH2", line 7
ORA-06512: at "SCOPUS1_APP.GEO_COUNT_DB_LONG_SIZE", line 19
ORA-06512: at line 1


SQL>



Answer : Counting the Character Length of a Long Field - Error

yes we can definitely do that but before that let us add debug/log messages to the function code as well to unerstand which line is giving the error.. Use the below code an perform the test again and give me the log message output what you are getting on the screen.

CREATE OR REPLACE PROCEDURE GEO_COUNT_DB_LONG_SIZE
is
cursor log_id_cursor is
  select to_char(log_id) from logs
        where  sti_casetype in (
                                      select sti_casetype
                                            from ford_acs_casetypes
                                            where transition_phase = 'DB'
                                            );
  local_log_id number;
  /* long_desc_length number(20,0); */
  long_desc_length number;
  my_err varchar2(100);
begin
  open log_id_cursor;
  fetch log_id_cursor into local_log_id;
   my_err :='after fetch';
          while log_id_cursor%found
           loop
      my_err:='before function call to get length';
  select find_length3(local_log_id) into long_desc_length from logs WHERE LOG_ID = local_log_id;
      my_err:='after function call to get length';
          insert into geo_field_count2 (case_no, field_size) values (local_log_id, long_desc_length);
      my_err:='after insert  to other table';
       commit;
      fetch log_id_cursor into local_log_id;
   end loop;
exception when others then
dbms_output.put_line('i am in exception:'|| my_err);
dbms_output.put_line('error:'||sqlerrm);
dbms_output.put_line('error code:' || sqlcode);
end;
/

create or replace function Find_Length3 (geo_log_id number)
  return number
is
  x clob;
  y long;
  my_err varchar2(100);
begin
  dbms_lob.createtemporary(x, false);
my_err:='FUNCTION:after createtemporary';
  select long_desc into y from logs where log_id = geo_log_id;
my_err:='FUNCTION:after select';
   x:= to_clob(y);
my_err:='FUNCTION:after to_clob';
  return dbms_lob.getlength(x);
my_err:='FUNCTION:after getlength';
 dbms_lob.freetemporary(x); --> note, this will not get executed as
-- this is after return statement.

exception
  when others then
dbms_output.put_line('inside function exception:'|| my_err);
dbms_output.put_line('inside function error:'||sqlerrm);
dbms_output.put_line('inside function error code:' || sqlcode);
end;
/
Random Solutions  
 
programming4us programming4us