如何在 SQL Server 2005 中回滚 UPDATE 查询?

发布于 2024-07-16 15:42:53 字数 76 浏览 4 评论 0原文

如何在 SQL Server 2005 中回滚 UPDATE 查询?

我需要用 SQL 来完成此操作,而不是通过代码。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

Oo萌小芽oO 2024-07-23 15:42:53
begin transaction

// execute SQL code here

rollback transaction

如果您已经执行了查询并想要将其回滚,不幸的是,您唯一真正的选择是恢复数据库备份。 如果您使用完整备份,那么您应该能够将数据库恢复到特定时间点。

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.

悲念泪 2024-07-23 15:42:53

你需要这个工具,你可以找到交易并将其逆转。

ApexSQL 日志

You need this tool and you can find the transaction and reverse it.

ApexSQL Log

池予 2024-07-23 15:42:53

您可以为此使用隐式事务

SET IMPLICIT_TRANSACTIONS ON

update Staff set staff_Name='jas' where staff_id=7

ROLLBACK

根据您的要求 - 您可以通过将存储过程设置为启动过程来从存储过程设置此设置 (SET IMPLICIT_TRANSACTIONS ON) 。

但是 SET IMPLICIT TRANSACTION ON 命令是特定于连接的。 因此,除了运行启动存储过程的连接之外的任何连接都不会从您设置的设置中受益。

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.

時窥 2024-07-23 15:42:53

您可以回滚在事务中执行的语句。
不提交事务,而是回滚事务。

如果您更新了某些内容并想要回滚这些更新,并且您尚未在(尚未提交的)事务中执行此操作,那么我认为这只是运气......

(手动修复或恢复备份)

You can rollback the statements you've executed within a transaction.
Instead of commiting the transaction, rollback the transaction.

If you have updated something and want to rollback those updates, and you haven't done this inside a (not-yet-commited) transaction, then I think it's though luck ...

(Manually repair, or, restore backups)

×纯※雪 2024-07-23 15:42:53

一旦提交更新,您就无法仅回滚单个更新。 最好的办法是回滚到以前的数据库备份。

Once an update is committed you can't rollback just the single update. Your best bet is to roll back to a previous backup of the database.

梦回梦里 2024-07-23 15:42:53

根据您指定的信息,恢复的最佳机会是通过数据库备份。 我认为您将无法回滚您所推动的任何更改,因为您当时显然没有使用事务。

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.

似狗非友 2024-07-23 15:42:53

简单易行:

标题代码...

Set objMyConn = New ADODB.Connection

Set objMyCmd = New ADODB.Command Set

objMyRecordset = New ADODB.Recordset

On Error GoTo ERRORHAND 

工作代码...

objMyConn.ConnectionString = ConnStr

objMyConn.Open 

代码...

“从 Excel 复制数据”

objMyConn.BeginTrans <-- define transactions to possible be rolled back 

For NewRows = 2 To Rows

objMyRecordset.AddNew 

For NewColumns = 0 To Columns - 1

objMyRecordset.Fields(NewColumns).Value = ActiveSheet.Cells(NewRows, NewColumns + 1)

Next NewColumns objMyRecordset.Update Next NewRows

objMyConn.CommitTrans <- if success, commit them to DB

objMyConn.Close

错误:

Success = False 

objMyConn.RollbackTrans <-- here we roll back if error encountered somewhere

LogMessage = "ERROR writing database: " & Err.Description

...

Simple to do:

header code...

Set objMyConn = New ADODB.Connection

Set objMyCmd = New ADODB.Command Set

objMyRecordset = New ADODB.Recordset

On Error GoTo ERRORHAND 

Working code...

objMyConn.ConnectionString = ConnStr

objMyConn.Open 

code....

'Copy Data FROM Excel'

objMyConn.BeginTrans <-- define transactions to possible be rolled back 

For NewRows = 2 To Rows

objMyRecordset.AddNew 

For NewColumns = 0 To Columns - 1

objMyRecordset.Fields(NewColumns).Value = ActiveSheet.Cells(NewRows, NewColumns + 1)

Next NewColumns objMyRecordset.Update Next NewRows

objMyConn.CommitTrans <- if success, commit them to DB

objMyConn.Close

ERRORHAND:

Success = False 

objMyConn.RollbackTrans <-- here we roll back if error encountered somewhere

LogMessage = "ERROR writing database: " & Err.Description

...

彩扇题诗 2024-07-23 15:42:53

如前所述,除了从备份恢复之外,您无能为力。 至少现在您将学会始终将语句包装在事务中,以便在决定提交之前查看会发生什么。 另外,如果您没有数据库备份,这也会教您定期备份数据库。

虽然我们对您当前的问题没有提供太多帮助...但愿这些答案将确保您将来不会再次遇到此问题。

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.

-残月青衣踏尘吟 2024-07-23 15:42:53

在此示例中,我们运行 2 行插入查询,如果所有这些都为 true,则运行,但如果不是,则不运行任何内容并回滚

DECLARE @rowcount int  set @rowcount = 0 ; 
BEGIN TRANSACTION [Tran1]
BEGIN TRY 
 insert into [database].[dbo].[tbl1] (fld1) values('1') ;
    set @rowcount = (@rowcount + @@ROWCOUNT); 
 insert into [database].[dbo].[tbl2] (fld1) values('2') ;
    set @rowcount = (@rowcount + @@ROWCOUNT); 

IF @rowcount =  2
  COMMIT TRANSACTION[Tran1]
ELSE
  ROLLBACK TRANSACTION[Tran1]
END TRY
  BEGIN CATCH
  ROLLBACK TRANSACTION[Tran1]
END CATCH

in this example we run 2 line insert into query and if all of them true it run but if not no run anything and ROLLBACK

DECLARE @rowcount int  set @rowcount = 0 ; 
BEGIN TRANSACTION [Tran1]
BEGIN TRY 
 insert into [database].[dbo].[tbl1] (fld1) values('1') ;
    set @rowcount = (@rowcount + @@ROWCOUNT); 
 insert into [database].[dbo].[tbl2] (fld1) values('2') ;
    set @rowcount = (@rowcount + @@ROWCOUNT); 

IF @rowcount =  2
  COMMIT TRANSACTION[Tran1]
ELSE
  ROLLBACK TRANSACTION[Tran1]
END TRY
  BEGIN CATCH
  ROLLBACK TRANSACTION[Tran1]
END CATCH
城歌 2024-07-23 15:42:53

尝试

ROLLBACK WORK;

一下通常会有效

Try

ROLLBACK WORK;

It usually works

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文