4

When I used JDBC to execute a prepared statement as following:

select count(1) from TableName where col1 = 9 and col2 = ?

it ocurred a DB2 SQL Error:

SQLCODE=-514, SQLSTATE=26501, SQLERRMC=SQL_CURSH200C1, DRIVER=3.64.104.

what's more, this problem doesn't come out everytime,sometimes it can query successfully.

Could anyone tell me the reason for the problem, thanks very much!

PS:DB2 version is 9.5

2
  • Show the code how you prepare and execute the statement Commented Jul 31, 2013 at 10:41
  • Same problem with: statement.executeQuery("SELECT COUNT(*) FROM MY_SCHEMA.MY_TABLE;").getLong(1) Commented Nov 8, 2024 at 12:34

2 Answers 2

3

Here's a link to the DB2 error codes, so you can look up your next error yourself.

DB2 thinks that your select is using a cursor. Try

select count(*) from TableName where col1 = 9 and col2 = ?

and see if the error goes away.

Here's the full explanation of the error from the IBM DB2 Error code manual.

-514

THE CURSOR cursor-name IS NOT IN A PREPARED STATE

Explanation

The application program has tried to use a cursor, 'cursor-name,' that is not in a prepared state. The cursor is associated with a statement that:

  1. Was never prepared.
  2. Was invalidated by a commit or rollback operations

System action

The statement cannot be processed. Programmer response

For case 1, ensure that you prepare the statement that is named in the DECLARE CURSOR statement for 'cursor-name' before you try to open the cursor.

For case 2, take one of the following actions:

  • Use the WITH HOLD option of DECLARE CURSOR.
  • Do not execute a commit or rollback operation until you are finished using the cursor.
  • Prepare the statement again after the commit or rollback.
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks. I have read this DB2 error code explanation before asked the question, but I still don't know the relation between the explanation and the problem. I do not declare a cursor for the statement, and do not execute a commit or rollback operation either. Would you please explain more details to me , many thanks.
@kingson: Change your prepared statement so it looks like the one I gave you in my answer.
Does using count(*) instead of count(1) really confuse the parser?
@a_horse_with_no_name: This isn't a common DB2 error, so I really don't know. The error may be triggered by some other part of the code. I guess kingson can tell us if my response worked or not.
@kingson: Then we have to see all of your DB2 statements in your code.
|
0

This issue may occur when you have not prepared your statement correctly before you run the executeQuery(). Check the code if you have prepared the query correctly and you have correctly set the values.

If you not able to get the error, place the complete code snippet which you use to prepare the statement and execute.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.