Cannot perform a backup or restore operation within a transaction

前端 未结 3 1510
一整个雨季
一整个雨季 2020-12-19 15:12

I am using PyODBC to back up my database, using following code:

SQL_command = """
                BACKUP DATABASE [MyDatabase]
                         


        
3条回答
  •  清酒与你
    2020-12-19 15:31

    The other answers are correct. You do need to set autocommit. However, the transaction will complete but the backup won't actually be taken because of a quirk in SQL Server and the way it returns status messages for backup and restore operations.

    To work around this you need to loop through these return messages until none remain:

    SQL_command = """
                    BACKUP DATABASE [MyDatabase]
                    TO DISK = N'D:\MSSQL\BACKUP\MyDatabase_20141212.bak' WITH
                          NOFORMAT
                    ,     NOINIT
                    ,     NAME = N'MyDatabase_20141212'
                    ,     SKIP
                    ,     REWIND
                    ,     NOUNLOAD
                    ,     STATS = 10
                  """
    
    conn.cursor.execute(SQL_command)
    while conn.cursor.nextset():
         pass 
    
    conn.cursor.close()
    

提交回复
热议问题