简体   繁体   English

SQLalchemy:query 返回一个非结果

[英]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.我在获取由 sqlalchemy 调用的存储过程返回的结果时遇到问题,我看到即使存储过程返回一个整数 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: Python错误跟踪:

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'回溯(最近一次调用):文件“/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.在处理上述异常的过程中,又出现了一个异常:...... 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”,第 1089 行,在 _non_result “此结果对象不返回行。” sqlalchemy.exc.ResourceClosedError:此结果对象不返回行。 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.这会导致另一个问题 - 当存储过程返回非零值(使用RETURN语句)时,这表示失败。

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.您需要更改存储过程,并且您至少有两个选择:1) 返回结果集或 2) 使用输出参数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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