Skip to content

Prepared statement with named parameters throws index out of range erroneously #1314

Closed
@tboemker

Description

@tboemker

Driver version

8.2.2

SQL Server version

Microsoft SQL Server 2017 (RTM-CU13-OD) (KB4483666) - 14.0.3049.1 (X64) 
	Dec 15 2018 11:16:42 
	Copyright (C) 2017 Microsoft Corporation
	Standard Edition (64-bit) on Windows Server 2016 Datacenter 10.0 <X64> (Build 14393: ) (Hypervisor)

Client Operating System

Microsoft Windows [Version 10.0.17134.1304]

JAVA/JVM version

openjdk version "1.8.0_181-1-ojdkbuild"
OpenJDK Runtime Environment (build 1.8.0_181-1-ojdkbuild-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)

Table schema

There are no tables. There is one stored procedure:

CREATE PROCEDURE demo_index_bug
  @x VARCHAR = NULL
, @y VARCHAR
AS
BEGIN
 SELECT 1
END

Problem description

  1. Expected behaviour:
    No exception.
  2. Actual behaviour:
    SQLException thrown.
  3. Error message/stack trace:
com.microsoft.sqlserver.jdbc.SQLServerException: The index 2 is out of range.
	at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:234)
	at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setterGetParam(SQLServerPreparedStatement.java:1116)
	at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setValue(SQLServerPreparedStatement.java:1130)
	at com.microsoft.sqlserver.jdbc.SQLServerCallableStatement.setString(SQLServerCallableStatement.java:1900)
	at Main.run(Main.java:35)
	at Main.main(Main.java:15)
  1. Any other details that can be helpful:

JDBC trace logs

Reproduction code

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;

public class Main {

	private final String host;
	private final String database;
	private final String user;
	private final String password;

	public static void main(String[] args) {
		try {
			new Main(args).run();
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public Main(String[] args) {
		this.host = args[0];
		this.database = args[1];
		this.user = args[2];
		this.password = args[3];
	}
	
	public void run()
	throws Exception {
		String connectionUrl = String.format("jdbc:sqlserver://%s;databaseName=%s", host, database);
        Connection conn = DriverManager.getConnection(connectionUrl, user, password);
		CallableStatement cs = conn.prepareCall("{call demo_index_bug(@y=?)}");
		cs.setString("y", "y");
		cs.executeQuery();
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions