-
Notifications
You must be signed in to change notification settings - Fork 166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SYBASE: Issues getting insert row ID passing in parameters #337
Comments
Also, any debugging tips would be appreciated as well. |
For the debugging see http://www.freetds.org/userguide/logging.htm. For the problem I expect that either the row was not inserted (not sure what Python does in case of errors) or that the type of request done by FreeTDS does not update |
Thanks for the debugging tips 👍
This is not the case. I checked the database and the row is properly inserted.
Interesting 🤔. Is this something that could be changed? |
Also, the failing method uses parameters. This comment may provide insight as well: mkleehammer/pyodbc#757 (comment)
|
Here is the log from the first query that returned the correct row ID: Here is the log from the query that returned the row ID of 0 with parameters: I didn't notice anything strange in there. The logs show different methods were called in each scenario. So, one of the methods called in the logs in the one that returned 0 could be suspect. |
This is inherent to the way Sybase ASE deals with parameterized statements. These get transformed on the database side to a so called LightWeight Procedure or LWP. They're indeed basically stored procedures with a cached query plan stored entirely in memory (ASE's procedure/statement cache) with no artifacts on disk. If you have access to Sybase/SAP support, there's some more information in SAP Knowledge Base Article 2578627. These types of LWPs have their own context and the session-global To make your code work, you can embed the import pyodbc
connection = pyodbc.connect('DSN=DST_ST_2;UID=someusr;PWD=somepwd',
autocommit = True)
cursor = connection.cursor()
cursor.execute('insert tempdb.dbo.some_table (foo) values (?)' +
'\nselect @@identity', 'bar')
print(cursor.fetchone()[0])
The same issue can be encountered in Java (both with jTDS and the official jConnect JDBC driver): import java.sql.*;
import java.util.Properties;
public abstract class Identity {
public static void main(String[] args) throws SQLException {
Properties props = new Properties();
props.put("user", "someusr");
props.put("password", "somepwd");
Connection c =
DriverManager.getConnection("jdbc:jtds:sybase://dst_st_2:3000",
props);
PreparedStatement stmt =
c.prepareStatement("insert tempdb.dbo.some_table (foo) values (?)");
stmt.setString(1, "bar");
stmt.executeUpdate();
//stmt.close();
Statement stmt2 = c.createStatement();
ResultSet rs = stmt2.executeQuery("select @@identity");
while (rs.next()) {
System.out.println(rs.getInt(1));
}
}
}
Same fix applies: import java.sql.*;
import java.util.Properties;
public abstract class Identity2 {
public static void main(String[] args) throws SQLException {
Properties props = new Properties();
props.put("user", "someusr");
props.put("password", "somepwd");
Connection c =
DriverManager.getConnection("jdbc:jtds:sybase://dst_st_2:3000",
props);
PreparedStatement stmt =
c.prepareStatement("insert tempdb.dbo.some_table (foo) values (?)"
+ "\nselect @@identity");
stmt.setString(1, "bar");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
System.out.println(rs.getInt(1));
}
}
}
The jConnect JDBC driver has the |
Thanks @uzb-dev, that was very helpful 👍. Very nice response that will definitely help lead to a resolution. |
Forwarded from: mkleehammer/pyodbc#757
Environment
Issue
Forwarded from: sqlalchemy/sqlalchemy#5311
This works:
But this fails (returns 0):
Any thoughts on if this is a FreeTDS issue or a SYBASE issue?
The text was updated successfully, but these errors were encountered: