简体   繁体   中英

SQLalchemy:query returns a non-result

I am having trouble in getting the results returned by the stored procedure which is called by sqlalchemy, I am seeing that even though stored procedure is returning an integer sql alcehmy. Result set is set to None, which is causing some issues, can someone tell me whats going on?

CREATE PROCEDURE [SYSDATA].[getCertificateInstallStatus] 
@DVSystemName [varchar](max)
AS
BEGIN
declare @system_state int = (SELECT state FROM SYSDATA.certificate where DVSystemName = @DVSystemName)
if @system_state is not NULL
    Return @system_state
Return 0
END

Now executing this stored procedure:

DECLARE @return_value int

EXEC    @return_value = [SYSDATA].[getCertificateInstallStatus]
        @DVSystemName = N'UATDSG'

SELECT  'Return Value' = @return_value

GO

Calling this procedure returns:

Return Value
0

Python error trace:

Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/SQLAlchemy-1.2.7-py3.6-macosx-10.9-x86_64.egg/sqlalchemy/engine/result.py", line 1067, in _fetchone_impl return self.cursor.fetchone() AttributeError: 'NoneType' object has no attribute 'fetchone'

During handling of the above exception, another exception occurred: ...... File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/SQLAlchemy-1.2.7-py3.6-macosx-10.9-x86_64.egg/sqlalchemy/engine/result.py", line 1089, in _non_result "This result object does not return rows. " sqlalchemy.exc.ResourceClosedError: This result object does not return rows. It has been closed automatically.

The explanation for the unexpected results is that the statement in the stored procedure doesn't return a result set, it returns an integer value. This leads to another issue - when a stored procedure returns a non-zero value (using RETURN statement) this indicates failure.

You need to change the stored procedure and you have at least two options: 1) return a result set or 2) use an ouptut parameter.

Stored procedure returning a result set:

CREATE PROCEDURE [SYSDATA].[getCertificateInstallStatus] 
    @DVSystemName [varchar](max)
AS
BEGIN
    SET NOCOUNT ON

    SELECT COALESCE([state], 0) AS [SystemState]
    FROM SYSDATA.certificate 
    WHERE DVSystemName = @DVSystemName)

    RETURN 0
END

Stored procedure with an output parameter:

CREATE PROCEDURE [SYSDATA].[getCertificateInstallStatus] 
    @DVSystemName [varchar](max),
    @SystemState int OUTPUT
AS
BEGIN
    SET NOCOUNT ON

    SELECT @SystemState = COALESCE([state], 0) 
    FROM SYSDATA.certificate 
    WHERE DVSystemName = @DVSystemName

    RETURN 0
END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM