0
我有下面的存储过程,动态调用存储过程的列表。它沿用了数个月,一直运行良好(不幸的是我的服务器是相当限制,访问不能管理这个任何其他方式)TSQL事务行为与断开连接的查询 - 动态SQL
Alter Proc [Process].[UspLoad_LoadController]
(
@HoursBetweenEachRun Int
)
As
Begin
--find all procedures that need to be updated
Create Table [#ProcsToRun]
(
[PID] Int Identity(1 , 1)
, [SchemaName] Varchar(150)
, [ProcName] Varchar(150)
);
Insert [#ProcsToRun]
([SchemaName]
, [ProcName]
)
Select [s].[name]
, [p].[name]
From [sys].[procedures] [p]
Left Join [sys].[schemas] [s]
On [s].[schema_id] = [p].[schema_id]
Where [s].[name] = 'Process'
And [p].[name] Like 'UspUpdate%';
Declare @MaxProcs Int
, @CurrentProc Int = 1;
Select @MaxProcs = Max([PID])
From [#ProcsToRun];
Declare @SQL Varchar(Max)
, @SchemaName sysname
, @ProcName sysname;
--run through each procedure, not caring if the count changes and only updating if there have been more than 23 hours since the last run
While @CurrentProc <= @MaxProcs
Begin
Select @SchemaName = [SchemaName]
, @ProcName = [ProcName]
From [#ProcsToRun]
Where [PID] = @CurrentProc;
Select @SQL = @SchemaName + '.' + @ProcName
+ ' @PrevCheck = 0,@HoursBetweenUpdates = '
+ Cast(@HoursBetweenEachRun As Varchar(5));
Exec (@SQL);
Set @CurrentProc = @CurrentProc + 1;
End;
End;
Go
然而,环境这是在运行偶尔遭受来自通信错误,查询在执行过程中被取消。
我的问题是 - 我可以用事务语句包装整个过程,并且如果我可以在查询被提前终止的情况下发生什么?
BEGIN Tran Test
Exec [Process].[UspLoad_LoadController] @HoursBetweenEachRun = 1;
COMMIT TRANSACTION Test
我想要发生的事情将是事务回滚 - 这会迎合这一点吗?