0

我正在尝试使用 OUTPUT 获取新插入行的 ID。但是,我遇到了异常:关闭连接或空查询集上的 fetch()。

我正在使用adodbapi进行 SQL Server 连接。

代码:

import adodbapi
Query = '''
        DECLARE @revisions_id table(revisions_id bigint);
        INSERT INTO [dbo].[revisions]
                   ([submitted_date]
                   ,[submission_received_date]
                   ,[form_number]
                   ,[ProcessTryCount])
        OUTPUT INSERTED.revisions_id INTO @revisions_id     
             VALUES
                   (GETDATE()
                   ,GETDATE()
                   ,?
                   ,0 )

        SELECT revisions_id FROM @revisions_id 
    '''
values = [2] 
connsql = a.sql_connection()
cursor = connsql.cursor()
cursor.execute(Query, values)
revisionsID = cursor.fetchone()
connsql.commit()
cursor.close()
connsql.close()

例外:

  File "TMSWorkflow.py", line 43, in CreateRevisions
    revisionsID = cursor.fetchone()
  File "C:\Users\adminuser2\AppData\Local\Programs\Python\Python35\lib\site-packages\adodbapi\adodbapi.py", line 922, in fetchone
    result = self._fetch(1)
  File "C:\Users\adminuser2\AppData\Local\Programs\Python\Python35\lib\site-packages\adodbapi\adodbapi.py", line 898, in _fetch
    self._raiseCursorError(api.FetchFailedError, 'fetch() on closed connection or empty query set')
  File "C:\Users\adminuser2\AppData\Local\Programs\Python\Python35\lib\site-packages\adodbapi\adodbapi.py", line 562, in _raiseCursorError
    eh(self.connection, self, errorclass, errorvalue)
  File "C:\Users\adminuser2\AppData\Local\Programs\Python\Python35\lib\site-packages\adodbapi\apibase.py", line 57, in standardErrorHandler
    raise errorclass(errorvalue)
adodbapi.apibase.FetchFailedError: fetch() on closed connection or empty query set
4

0 回答 0