当我按SQL Server Management Studio(SSMS)上的新查询时,根据以下图片:
我想在新标签中添加以下脚本。这有可能吗?
USE AdventureWorks2012;
GO
-- SET XACT_ABORT ON will render the transaction uncommittable
-- when the constraint violation occurs.
SET XACT_ABORT ON;
BEGIN TRY
BEGIN TRANSACTION;
------------------------------------------------------
------------------------------------------------------
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
-- Test XACT_STATE for 0, 1, or -1.
-- If 1, the transaction is committable.
-- If -1, the transaction is uncommittable and should
-- be rolled back.
-- XACT_STATE = 0 means there is no transaction and
-- a commit or rollback operation would generate an error.
-- Test whether the transaction is uncommittable.
IF (XACT_STATE()) = -1
BEGIN
PRINT 'The transaction is in an uncommittable state.' +
' Rolling back transaction.'
ROLLBACK TRANSACTION;
END;
-- Test whether the transaction is active and valid.
IF (XACT_STATE()) = 1
BEGIN
PRINT 'The transaction is committable.' +
' Committing transaction.'
COMMIT TRANSACTION;
END;
END CATCH;
GO
在下面的链接中有一个非常相似的问题,但它对我不起作用。 SSMS 2014。
MSSQL Server Management Studio (SSMS) 2005 New Query Template
我不知道这里是否可行,但我使用代码片段来做这样的事情。您可以使用免费的生成器,例如https://snippetsgen.codeplex.com/,并将它们保存在Code Snippets Manager(或Ctr + K,ctr + B)下。然后通过快捷方式访问它们(对我来说,它是Ctr + K,Ctr + X)。我在这里扔了很多经常被查询的东西。
对于SSMS版本14,您可以在C:\ Program Files(x86)\ Microsoft SQL Server \ 140 \ Tools \ Binn \ ManagementStudio \ SqlWorkbenchProjectItems \ Sql \ SQLFile.sql中修改模板文件