-
Notifications
You must be signed in to change notification settings - Fork 564
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
Executing Stored Procedure in SQL Server without a full accounting of Errors and Queries #935
Comments
Could you post an ODBC trace of the two cases? |
ODBC Trace Attached. First File Contains trace for the first instance where we hit the Divide by Zero immediately. Second File Contains trace where we initially receive the 'SELECT 0;' |
This issue happens because inside cursor.nextset() there is a call to SQLMoreResults and if it returns an error, pyODBC will free the statement handle and getting more results becomes impossible. The problem is that there is no obvious way to tell if SQLMoreResults returned an error because it itself failed (in which case the statement should be freed), or because the result that it got contains an error (in which case it shouldn't). I'm going to try to think of a solution to his, but if anyone else has suggestions feel free to share. |
I am able to obtain the output when running SQLCMD on the same procedure (same driver etc.) so have attached trace of this executing the 'initial error' stored procedure. There appears to me to be some overlap of the API calls at critical points but then a fair amount of difference in what happens in between. This could mean that it is too different in structure to be of use even as a starting point but it is as close as I have to a suggestion |
sqlcmd is designed specifically for msodbcsql so it can make assumptions on how SQLMoreResults behaves; not so with pyODBC which is generic and has to work with other ODBC drivers too. |
try this, it's built on top of ideas from @gordthompson
|
How bad would it be to assume the SQLMoreResults call did not fail? What if we assume it indicates that there is an error to retrieve? In the super rare case that is wrong, the next API call should fail also. Is this a driver bug? Should it really be returning an error status? Not that it helps any. |
I think my suggestion above might be the way to go:
Since it is documented to return SQL_ERROR, do not assume the call itself failed. |
Environment
To diagnose, we usually need to know the following, including version numbers. On Windows, be
sure to specify 32-bit Python or 64-bit:
Issue
I have the following stored procedure which I am trying to execute. The commented 'SELECT 0;' is somewhat important as adding or removing it has what I think is a relevant change to the output but I wanted to leave it out initially.
Stored Procedure
Procedure Call in Python
This has the output
I expected the 'nextstep' function to continue moving through subsequent items with output for the SELECT statements and errors down until 'SELECT 6' where it would exit. Unfortunately this is not the case. I believe it may be associated with the 'SQLMoreResults' error that comes with the second Divide by Zero error.
If the original 'SELECT 0' is uncommented and the procedure updated then even less information is returned as shown in the output below.
It appears that there is a difference in an immediate execution error and an error thrown after a successful SELECT statement. I'm not sure if this is relevant to the underlying issue.
I am not sure why the SQLMoreResults error is thrown on the first encounter here however it exits immediately without moving on to the 'SELECT 1;', 'SELECT 2;' and then exiting on the second encounter of Divide by Zero as it did previously.
Essentially I am looking for the full output of all steps both successful statements and errors so if multiple errors are encountered then they can all be logged at the Python level rather than needing to move between Python and SQL for debugging.
I think 'SET NOCOUNT ON;' has been suggested for similar errors which I have tried with no success.
The text was updated successfully, but these errors were encountered: