how to find bad data causing ORA-01722

572 Views Asked by At

I'm using ojdbc8 to run a straight-forward query that throws java.sql.SQLSyntaxErrorException: ORA-01722: invalid number for certain results.

This happens in SQL Developer and via java API (name is a varchar here):

Statement sel = conn.createStatement();
ResultSet rs = sel.executeQuery("select * from t1 order by name");
while(rs.next())

Through debugging, I've isolated several rows that cause the error but when I select against those rows only (rather than select *), I don't experience the error, which leads me to believe that this may some kind of driver/performance issue?

I've also tried finding bad data with code I found here on SO but it returns without error (PL/SQL procedure successfully completed):

set serveroutput on
DECLARE
   exc EXCEPTION;
   PRAGMA EXCEPTION_INIT(exc, -1722);
   l_message VARCHAR2(4000);
BEGIN
   EXECUTE IMMEDIATE 
   'select * from t1 order by name';
EXCEPTION
   WHEN exc THEN
      l_message := regexp_replace(sqlerrm, '^[A-Z]{3}-[0-9]{5}: "([^"]+)".*$', '\1');

      dbms_output.put_line ( 'bad data '''||l_message||'''');
      dbms_output.put_line ( 'exception: sqlcode='||SQLCODE||', sqlerrm='''||sqlerrm||'''');

   WHEN OTHERS THEN
      RAISE;
END;
0

There are 0 best solutions below