简体   繁体   English

如何在SQL Server 2008中使用带触发器的备份?

[英]How to use backup with triggers in SQL Server 2008?

I have a table (Cycle) and I created a trigger 我有一个表格(周期),并创建了一个触发器

alter trigger AnyName on Cycles
for insert,update,delete
AS
BACKUP DATABASE medrepcrm TO  DISK = N'C:\medrepcrm.bak' WITH NOFORMAT, INIT,  NAME = N'pcrm-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10
GO

But when I insert, update, or delete in the Cycle table that leads to an error. 但是,当我在Cycle表中插入,更新或删除时,会导致错误。

Msg 3021, Level 16, State 0, Procedure AnyName, Line 8
Cannot perform a backup or restore operation within a transaction.
Msg 3013, Level 16, State 1, Procedure AnyName, Line 8
BACKUP DATABASE is terminating abnormally.
The statement has been terminated.

What can I do to fix this error? 我该如何解决该错误?

BACKUP DATABASE on MSDN says MSDN上的备份数据库

The BACKUP statement is not allowed in an explicit or implicit transaction. 显式或隐式事务中均不允许使用BACKUP语句。

A trigger is always in a transactions 触发器始终在交易中

If you work it through, a backup which is transactionally consistent snapshot of a database: it doesn't make sense for it to be in it's own transaction. 如果您进行了此工作,那么备份将是数据库的事务一致性快照:将它包含在自己的事务中是没有意义的。

Not least, on RESTORE the roll forward/roll back mechanism would have to rollback the transaction containing the backup... which defeats the purpose of the backup in the first place... 尤其重要的是,在RESTORE上,前滚/后退机制必须回滚包含备份的事务...这首先使备份的目的无效...

You could start a job sp_start_job . 您可以开始工作sp_start_job Within the job you execute a stored procedure where you execute the backup statement. 在作业中,您将执行存储过程,并在其中执行备份语句。 I've done this already with a server trigger which is fired when a new database is created. 我已经通过创建新数据库时触发的服务器触发器完成了此操作。

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

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