Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Upgraded from SQL 2000 to SQL 2005 and switched to Microsoft SQL Server 2005 JDBC Driver version 1.2. I got the error "The statement did not return a result" when executing an Insert followed by SELECT SCOPE_IDENTITY()".I solved the issue using executeUpdate() and getGeneratedKeys instead of executeQuery. Here is the before and after code. </p> <p><em>Note: The connection used in this example is java.sql.connection not the com.microsoft.sqlserver.jdbc.SqlServerConnection.</em></p> <p><strong>SQL 2000 code</strong></p> <pre><code>String dbURL = "jdbc:sqlserver" + "://" + dbServer + ":" + dbServerPort + ";SelectedMethod=cursor;databaseName=" + dbName + ";user=xxx;password=xxx"; Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); java.sql.Connection connection = DriverManager.getConnection(dbURL); sql = "insert into Contact (name) values ('ABC'); SELECT SCOPE_IDENTITY()"; PreparedStatement ps = connection.prepareStatement(sql); ResultSet rs = ps.executeQuery(); if (rs.next()) { long id = rs.getLong(1); System.out.println("Id=" + id); } </code></pre> <p><strong>SQL 2005 code</strong></p> <pre><code>String dbURL = "jdbc:sqlserver" + "://" + dbServer + ":" + dbServerPort + ";SelectedMethod=cursor;databaseName=" + dbName + ";user=xxx;password=xxx"; Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); java.sql.Connection connection = DriverManager.getConnection(dbURL); sql = "insert into Contact (name) values ('ABC'); SELECT SCOPE_IDENTITY()"; PreparedStatement ps = connection.prepareStatement(sql); ps.executeUpdate(); // do not use execute() here otherwise you may get the error // The statement must be executed before // any results can be obtained on the next // getGeneratedKeys statement. ResultSet rs = ps.getGeneratedKeys(); if (rs.next()) { long id = rs.getLong(1); System.out.println("Id=" + id); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload