How can I rollback an UPDATE query in SQL server 2005?

后端 未结 10 2369
北海茫月
北海茫月 2020-12-14 09:15

How can I rollback an UPDATE query in SQL server 2005?

I need to do this in SQL, not through code.

相关标签:
10条回答
  • 2020-12-14 09:39

    As already stated there is nothing you can do except restore from a backup. At least now you will have learned to always wrap statements in a transaction to see what happens before you decide to commit. Also, if you don't have a backup of your database this will also teach you to make regular backups of your database.

    While we haven't been much help for your imediate problem...hopefully these answers will ensure you don't run into this problem again in the future.

    0 讨论(0)
  • 2020-12-14 09:40
    begin transaction
    
    // execute SQL code here
    
    rollback transaction
    

    If you've already executed the query and want to roll it back, unfortunately your only real option is to restore a database backup. If you're using Full backups, then you should be able to restore the database to a specific point in time.

    0 讨论(0)
  • 2020-12-14 09:40

    From the information you have specified, your best chance of recovery is through a database backup. I don't think you're going to be able to rollback any of those changes you pushed through since you were apparently not using transactions at the time.

    0 讨论(0)
  • 2020-12-14 09:41

    You can use implicit transactions for this

    SET IMPLICIT_TRANSACTIONS ON
    
    update Staff set staff_Name='jas' where staff_id=7
    
    ROLLBACK
    

    As you request-- You can SET this setting ( SET IMPLICIT_TRANSACTIONS ON) from a stored procedure by setting that stored procedure as the start up procedure.

    But SET IMPLICIT TRANSACTION ON command is connection specific. So any connection other than the one which running the start up stored procedure will not benefit from the setting you set.

    0 讨论(0)
提交回复
热议问题