Db2 SQLCODE

SQLCODE -313: Host variables do not match parameter markers

SQLCODE -313 (SQLSTATE 07001) means the number of host variables supplied on EXECUTE or OPEN doesn't equal the number of ? parameter markers in the prepared statement. Db2 runs nothing until the two counts match.

Other names for this code

Also written as SQL -313, SQLCODE=-313, SQLCODE = -313, db2 -313, sqlstate 07001, SQL0313N, db2 number of host variables parameter markers, SQL0313.

What it means

A dynamic statement uses ? parameter markers where values will go. After the PREPARE, the program supplies one value per marker, either as a list of host variables in USING :A, :B or through an SQLDA in USING DESCRIPTOR. Db2 matches them by position, the first value to the first marker and so on.

If the count is wrong, there's no safe way to match them, so Db2 rejects the EXECUTE or OPEN with -313. The prepared statement stays prepared. Only that one EXECUTE or OPEN fails.

A ? inside a quoted literal is just a character, not a marker. Db2 counts only the real markers, which isn't always the same as counting question marks in the string.

Common causes

  • The statement is built at run time with optional predicates, such as search screens where each filled-in field adds AND COL = ?, but the USING list is fixed.
  • With an SQLDA, SQLD doesn't match the number of entries the program filled in, or the program reused an SQLDA from another statement.
  • The program counted a question mark inside a literal as a marker, or missed a marker added in a later change.
  • The same statement name was prepared again with different text, and an OPEN or EXECUTE later in the code still passes the old list.
  • A product generated the statement wrongly. IBM APAR PH48017 fixed a case on Db2 12 and 13 where a query offloaded to Db2 Analytics Accelerator with a host variable in a row value expression got -313.

How to find the problem

  1. Display the final statement text just before the PREPARE and count the markers outside quoted literals.
  2. Count the host variables in the USING clause, or display SQLD and the filled entries of the SQLDA.
  3. Run DESCRIBE INPUT after the PREPARE. It fills an SQLDA with one entry per marker, so SQLD tells you how many values Db2 expects.
  4. If the SQL comes from a tool or an accelerated query, note CURRENT QUERY ACCELERATION and search IBM support for open APARs.

How to fix it

  • Build the USING list in the same code that adds each marker. With a variable number of markers, use an SQLDA, add one entry each time you add a marker, and set SQLD at the end.
  • Use DESCRIBE INPUT to size and type the input SQLDA instead of hard-coding it.
  • Prepare each distinct statement under its own statement name, or keep the USING list next to the PREPARE that it belongs to.
  • For the accelerator case, apply the PTF for PH48017 (UI82064 for Db2 12, UI82065 for Db2 13).

What it looks like

DSNT408I SQLCODE = -313, ERROR:  THE NUMBER OF HOST VARIABLES SPECIFIED IS NOT EQUAL TO THE
         NUMBER OF PARAMETER MARKERS
DSNT418I SQLSTATE   = 07001 SQLSTATE RETURN CODE

IBM documentation

IBM documentation: -313. Use the documentation for your installed release.

Report a correction to WND Software.

Questions

Do JDBC and ODBC programs get -313?

Rarely. The drivers usually check that every marker has a value before sending the statement and report their own error. A -313 from Db2 is more common in COBOL, PL/I and C programs that use dynamic SQL directly.

Is a ? inside quotes a parameter marker?

No. Inside a string literal it's an ordinary character. Only a ? outside literals is a marker.

What does Db2 for LUW report?

SQL0313N, with SQLSTATE 07001, or 07004 when the statement needs input values and none were given. IBM i reports SQL0313.