question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Regression when running DDL (CREATE SCHEMA or CREATE VIEW) in PreparedStatement

See original GitHub issue

The following code:

try (Connection c = new com.microsoft.sqlserver.jdbc.SQLServerDriver().connect(url, properties)) {
    try (PreparedStatement s1 = c.prepareStatement("create schema x");
         PreparedStatement s2 = c.prepareStatement("drop schema x")) {
        System.out.println(s1.execute());
        System.out.println(s2.execute());
    }
}

… works with:

<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>6.1.0.jre8</version>
</dependency>

… but it no longer works with:

    <version>6.2.0.jre8</version>

The error I’m getting is this:

com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near the keyword 'schema'.
	at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:258)
	at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1547)
	at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:528)
	at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:461)
	at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7151)
	at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2689)
	at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:224)
	at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:204)
	at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.execute(SQLServerPreparedStatement.java:445)
	at SQLServer.main(SQLServer.java:69)

A workaround is to resort to static statements. The same is true for CREATE VIEW statements:

// Works on both versions
try (Statement s1 = c.createStatement();
     Statement s2 = c.createStatement()) {
    System.out.println(s1.execute("create view x as select 1 a"));
    System.out.println(s2.execute("drop view x"));
}

// Works only on version 6.1.0
try (PreparedStatement s1 = c.prepareStatement("create view x as select 1 a");
     PreparedStatement s2 = c.prepareStatement("drop view x")) {
    System.out.println(s1.execute());
    System.out.println(s2.execute());
}

I think that’s quite a significant regression. Other DDL statements might be affected as well.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
TobiasSQLcommented, Jul 7, 2017

Correctamundo, this is a bug. @v-afrafi I suggested a change in your fix (otherwise looks great!).

0reactions
lukasedercommented, Jul 19, 2017

Thank YOU for fixing it so quickly 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot call CREATE SCHEMA through JDBC in SQL Server
This seems to be a regression in version 6.2.0. It used to work in version 6.1.0. I've reported this issue to Microsoft: ...
Read more >
CREATE SCHEMA (Transact-SQL) - Microsoft Learn
Specifies a CREATE VIEW statement that creates a view within the schema. The principal executing this statement must have CREATE VIEW ...
Read more >
Chapter 12. SQL Statement Syntax - Oracle Help Center
CREATE INDEX is mapped to an ALTER TABLE statement to create indexes. See Section 12.1.7, “ ALTER TABLE Syntax”. CREATE INDEX cannot be...
Read more >
Changes in MySQL 8.0.29 (2022-04-26, General Availability)
Suppose we create tables t1 and t2 , and then the view v based on these two tables, using the SQL statements shown...
Read more >
Use quoted schema in an insert statement executed thorough ...
Are you connecting to the database using the credentials of "Quoted_User"? In this case I see no reason to specify the schema name...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found