在一次交易中与和进行许多更改 -这不是梦。有可能的。rollback
commit
这是脚本的脚手架(遵循MS准则进行了改进):
BEGIN TRANSACTION
BEGIN TRY
-- place your script in this TRY block
-- your DDL instructions:
ALTER TABLE1...
ALTER TABLE2...
-- data modifications:
EXEC('
UPDATE A
SET c1 = 23,
c2 = ''ZZXX'';
');
-- another DDL instruction:
ALTER TABLE2...
-- end of your script
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION;
-- If you want reraise an exception (to determine the reason of the exception)
-- just uncomment block with appropriate version:
-- SQL SERVER > 2012
/*
THROW;
*/
-- SQL SERVER < 2012 (tested against 2008 R2)
/*
DECLARE @ErrorMessage VARCHAR(MAX);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;
SELECT
@ErrorMessage = ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE();
RAISERROR (
@ErrorMessage, -- Message text.
@ErrorSeverity, -- Severity.
@ErrorState -- State.
);
*/
END CATCH;
IF @@TRANCOUNT > 0
COMMIT TRANSACTION;
GO
请注意,THROW
仅适用于SQL SERVER版本>2012。在这里,您可以将版本从semver转换为年份表示法:http : //sqlserverbuilds.blogspot.ru(不知道.ru
域,有英文版本)