Getting an exception ORA-00942: table or view does not exist - when inserting into an existing table

拜拜、爱过 提交于 2019-11-26 22:57:56

Oracle will also report this error if the table exists, but you don't have any privileges on it. So if you are sure that the table is there, check the grants.

There seems to be some issue with setCLOB() that causes an ORA-00942 under some circumstances when the target table does exist and is correctly privileged. I'm having this exact issue now, I can make the ORA-00942 go away by simply not binding the CLOB into the same table.

I've tried setClob() with a java.sql.Clob and setCLOB() with an oracle.jdbc.CLOB but with the same result.

As you say, if you bind as a string the problem goes away - but this then limits your data size to 4k.

From testing it seems to be triggered when a transaction is open on the session prior to binding the CLOB. I'll feed back when I've solved this...checking Oracle support.

There was no problem with my database connection properties or with my table or view name. The solution to the problem was very strange. One of the columns that I was trying insert was of Clob type. As I had a lot of trouble handling clob data in oracle db before, gave a try by replacing the clob setter with a temporary string setter and the same code executed with out any problems and all the rows were correctly inserted!!!.

ie. peparedstatement.setClob(columnIndex, clob)

was replaced with

peparedstatement.setString(columnIndex, "String")

@unbeli is right. Not having appropriate grants on a table will result in this error. For what it's worth, I recently experienced this. I was experiencing the exact problem that you described, I could execute insert statements through sql developer but would fail when using hibernate. I finally realized that my code was doing more than the obvious insert. Inserting into other tables that did not have appropriate grants. Adjusting grant privileges solved this for me.

Note: Don't have reputation to comment, otherwise this may have been a comment.

I found how to solve this problem without using JDBC's setString() method which limits the data to 4K.

What you need to do is to use preparedStatement.setClob(int parameterIndex, Reader reader). At least this is what that worked for me. Thought Oracle drivers converts data to character stream to insert, seems like not. Or something specific causing an error.

Using a characterStream seems to work for me. I am reading tables from one db and writing to another one using jdbc. And i was getting table not found error just like it is mentioned above. So this is how i solved the problem:

case Types.CLOB:   //Using a switch statement for all columns, this is for CLOB columns
        Clob clobData = resultSet.getClob(columnIndex); // The source db
        if (clobData != null) {
            preparedStatement.setClob(columnIndex, clobData.getCharacterStream());
        } else {
            preparedStatement.setClob(columnIndex, clobData);
        }
        clobData = null;
        return;

All good now.

We experienced this issue on a BLOB column. Just in case anyone else lands on this question when encountering this error, here is how we resolved the issue:

We started out with this:

            preparedStatement.setBlob(parameterIndex, resultSet.getBlob(columnName)); break;

We resolved the issue by changing that line to this:

        java.sql.Blob blob = resultSet.getBlob(columnName);
        if (blob != null) {
            java.io.InputStream blobData =  blob.getBinaryStream();
            preparedStatement.setBinaryStream(parameterIndex, blobData);
        } else {
            preparedStatement.setBinaryStream(parameterIndex, null);
        }

Is your script providing the schema name, or do you rely on the user logged into the database to select the default schema?

It might be that you do not name the schema and that you perform your batch with a system user instead of the schema user resulting in the wrong execution context for a script that would work fine if executed by the user that has the target schema set as default schema. Your best action would be to include the schema name in the insert statements:

INSERT INTO myschema.mytable (mycolums) VALUES ('myvalue')

update: Do you try to bind the table name as bound value in your prepared statement? That won't work.

It works for me:

Clob clob1;
while (rs.next()) {
   rs.setString(1, rs.getString("FIELD_1"));

   clob1 = rs.getClob("CLOB1");
   if (clob1 != null) {
      sta.setClob(2, clob1.getCharacterStream());
   } else {
      sta.setClob(2, clob1);
   }
   clob1 = null;

   sta.setString(3, rs.getString("FIELD_3"));
}
user8951209

Here I got the solution for the question. The problem is on glass fish if you are using it. When you create JNDI name make sure pool name is correct and pool name is the name of connection pool name that you are created.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!