【发布时间】:2016-08-23 04:01:04
【问题描述】:
我有以下存储过程,它动态调用存储过程列表。它已经存在了几个月并且运行良好(不幸的是,我对服务器的访问受到很大限制,因此无法以任何其他方式进行管理)
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
我想要回滚事务 - 这会满足这个要求吗?
【问题讨论】:
标签: sql-server tsql sql-server-2012 dynamic-sql