这是正常现象还是我缺少什么?这些语句不会产生回滚
BEGIN TRAN
BEGIN TRY
CREATE TABLE dbo."aCTIONS" ("ID" int NOT NULL IDENTITY(1,1) )
ALTER TABLE "dbo"."aCTIONS"
ADD "Date" datetime
ALTER TABLE "dbo"."aCTIONS"
ADD "Name" nvarchar(50) COLLATE Latin1_General_CI_AS
ALTER TABLE "dbo"."aCTIONS"
ADD "ActionID" int;
ALTER TABLE "dbo"."aCTIONS"
ADD CONSTRAINT PK_aCTIONS_ActionID
PRIMARY KEY NONCLUSTERED ("ActionID")
ALTER TABLE "dbo"."aCTIONS"
ADD "Notes" nvarchar(255) COLLATE Latin1_General_CI_AS;
ALTER TABLE "dbo"."aCTIONS"
ADD CONSTRAINT DF_aCTIONS_Notes DEFAULT (N'MISLIM') FOR "Notes"
ALTER TABLE "dbo"."aCTIONS"
ADD "Consequence_Paid" bit;
ALTER TABLE "dbo"."aCTIONS"
ADD CONSTRAINT DF_aCTIONS_Consequence_Paid DEFAULT ((1)) FOR "Consequence_Paid"
ALTER TABLE "dbo"."aCTIONS"
ADD "Reward_Paid" bit
ALTER TABLE "dbo"."aCTIONS"
ADD "username" int
ALTER TABLE "dbo"."aCTIONS"
ADD "time" time(0)
ALTER TABLE "dbo"."aCTIONS"
ADD "DateOnly" date;
ALTER TABLE "dbo"."aCTIONS"
ADD CONSTRAINT DF_aCTIONS_DateOnly DEFAULT (getdate()) FOR "DateOnly"
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
ROLLBACK TRAN
END CATCH
IF @@TRANCOUNT > 0
COMMIT TRAN
请注意,... ADD“ ActionID” int之后的大空间应该有NOT NULL,并且在该语句之后,下一条语句将尝试在其上创建PK约束。因此错误将在那里发生,这是可以的。但是不知何故,我在SSMS中显示了错误,之前回滚了,事务仍然处于活动状态,但是如何?为什么?
请注意,这只是我正在构建的脚本过程的输出,并试图确保如果存在错误将永远不会部分创建表。
谢谢, 德扬
编辑1
好的,好的,但是这只动物是由SSMS本身生成的:
/* Toprevent any potential data loss issues, you should review this script in detail before running it outside the context of the database designer.*/
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Stavke ADD
dsad nchar(10) NULL
GO
ALTER TABLE dbo.Stavke SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
?
除此之外,还需要做更多的事情,最后,我正在寻找一种解决方案,以将其全部放入交易中。
答案 0 :(得分:0)
有一个解决方案:
https://dba.stackexchange.com/questions/29544/rollback-group-of-ddl-statements
以及更多信息:
这使我想到了这个解决方案:
SET XACT_ABORT ON;
BEGIN TRAN
BEGIN TRY
CREATE TABLE dbo."aCTIONS" ("ID" int NOT NULL IDENTITY(1,1) )
ALTER TABLE "dbo"."aCTIONS"
ADD "Date" datetime
ALTER TABLE "dbo"."aCTIONS"
ADD "Name" nvarchar(50) COLLATE Latin1_General_CI_AS
ALTER TABLE "dbo"."aCTIONS"
ADD "ActionID" int;
ALTER TABLE "dbo"."aCTIONS"
ADD CONSTRAINT PK_aCTIONS_ActionID
PRIMARY KEY NONCLUSTERED ("ActionID")
ALTER TABLE "dbo"."aCTIONS"
ADD "Notes" nvarchar(255) COLLATE Latin1_General_CI_AS;
ALTER TABLE "dbo"."aCTIONS"
ADD CONSTRAINT DF_aCTIONS_Notes DEFAULT (N'MISLIM') FOR "Notes"
ALTER TABLE "dbo"."aCTIONS"
ADD "Consequence_Paid" bit;
ALTER TABLE "dbo"."aCTIONS"
ADD CONSTRAINT DF_aCTIONS_Consequence_Paid DEFAULT ((1)) FOR "Consequence_Paid"
ALTER TABLE "dbo"."aCTIONS"
ADD "Reward_Paid" bit
ALTER TABLE "dbo"."aCTIONS"
ADD "username" int
ALTER TABLE "dbo"."aCTIONS"
ADD "time" time(0)
ALTER TABLE "dbo"."aCTIONS"
ADD "DateOnly" date;
ALTER TABLE "dbo"."aCTIONS"
ADD CONSTRAINT DF_aCTIONS_DateOnly DEFAULT (getdate()) FOR "DateOnly"
END TRY
BEGIN CATCH
IF (XACT_STATE()) = -1
BEGIN
ROLLBACK TRAN
THROW
END
END CATCH
IF (XACT_STATE()) = 1
COMMIT TRAN
,并且由于错误而未创建表。很酷,不是吗?