2010-05-22 54 views
0

基本上我想保持事务非常简单,但如果在后面的部分发生任何错误,我应该能够回滚。 喜欢的东西:在sql 2008中删除一组表的最佳做法

BEGIN TRANSACTION 

    DELETE SET 1(this will delete first set of table) 
    COMMIT 

    DELETE SET 2 (will delete second set of table) 

如果任何错误发生在删除设置2我应该能够回滚设置1个交易为好。让我知道,如果我们有任何选择这样做。感谢您的帮助。

回答

2

如果在删除设置 2我应该能够回滚设置1 交易为well.Let我知道,如果我们 有任何选项来执行这样出现任何错误。 感谢您的帮助。

那么,你为什么不这样做呢?

BEGIN TRY 
    BEGIN TRANSACTION -- Start the transaction 

    DELETE SET 1(this will delete first set of table) 
    DELETE SET 2 (will delete second set of table) 

    -- If we reach here, success! 
    COMMIT 
END TRY 
BEGIN CATCH 
    -- Whoops, there was an error 
    IF @@TRANCOUNT > 0 
    ROLLBACK 

    -- Raise an error with the details of the exception 
    DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int 
    SELECT @ErrMsg = ERROR_MESSAGE(), 
     @ErrSeverity = ERROR_SEVERITY() 

    RAISERROR(@ErrMsg, @ErrSeverity, 1) 
END CATCH 

阅读here的完整说明。

-2

如果您的意思是删除DELETE TABLE中的表格(即表格,而不是内容),则表示您运气不佳 - DDL未处理。

+2

DDL(DROP/CREATE)*是*交易安全...如果您曾经使用过红门SQL比较,您会看到它 – gbn 2010-05-22 15:47:35

相关问题