【高分悬赏】【难度大】【存储过程】【100分】【顶者都有分】
create proc dbo.OpreateTB
--@OpreateUser nvarchar(100),
--@IsRun nvarchar(10)
as
declare @i int
declare @start datetime
declare @sql nvarchar(max)
declare @error int
declare @ROWCOUNT int
set @i = 1;
while @i <= (select COUNT(*) from [TB_CMD])
begin
set @sql = (select sqlevent from [TB_CMD] where [OrderNumber] = @i)
set @start = GETDATE()
exec(@sql);
select @error = @@ERROR,
@ROWCOUNT = @@ROWCOUNT
update [TB_CMD]
set BetweenTime = datediff(ms,@start,GETDATE()),
RunLog = case when @error = 0 then '('+cast(@ROWCOUNT as varchar)+'行影响)'
else '回滚'
end,
RunStatus = case when @error = 0 then 1
when @error <> 1 then 0
end,
IsRun = 1
where [OrderNumber] = @i
set @i = @i + 1
end
go
--执行
exec OpreateTB
/*
(1 行受影响)
消息 208,级别 16,状态 0,第 1 行
对象名 '#tb' 无效。
(1 行受影响)
消息 208,级别 16,状态 0,第 1 行
对象名 '#tb' 无效。
(1 行受影响)
消息 208,级别 16,状态 0,第 1 行
对象名 '#tb' 无效。
(1 行受影响)
消息 105,级别 15,状态 1,第 1 行
字符串 '' 后的引号不完整。
(1 行受影响)
消息 208,级别 16,状态 0,第 1 行
对象名 '#tb' 无效。
(1 行受影响)
消息 208,级别 16,状态 0,第 1 行
对象名 '#tb' 无效。
(1 行受影响)
消息 3701,级别 11,状态 5,第 1 行
无法对 表 '#tb' 执行 删除,因为它不存在,或者您没有所需的权限。
(1 行受影响)
*/
--查询
select *
from [TB_CMD]
create proc dbo.OpreateTB
--@OpreateUser nvarchar(100),
--@IsRun nvarchar(10)
as
declare @i int
declare @start datetime
declare @sql nvarchar(max)
declare @error int
declare @ROWCOUNT int
set @i = 1;
while @i <= (select COUNT(*) from [TB_CMD])
begin
--按照[OrderNumber]进行了排序,每次取出1条
;with t
as
(
select *,
ROW_NUMBER() over(order by [OrderNumber]) rownum
from [TB_CMD]
)
select @sql = [SqlEvent]
from t
where rownum = @i
set @start = GETDATE()
exec(@sql);
select @error = @@ERROR,
@ROWCOUNT = @@ROWCOUNT
update [TB_CMD]
set BetweenTime = datediff(ms,@start,GETDATE()),
RunLog = case when @error = 0 then '('+cast(@ROWCOUNT as varchar)+'行影响)'
else '回滚'
end,
RunStatus = case when @error = 0 then 1
when @error <> 1 then 0
end,
IsRun = 1
where [OrderNumber] = @i
set @i = @i + 1
end
go
create proc dbo.OpreateTB
--@OpreateUser nvarchar(100),
--@IsRun nvarchar(10)
as
declare @i int
declare @start datetime
declare @sql nvarchar(max)
declare @OrderNumber int
declare @error int
declare @ROWCOUNT int
set @i = 1;
while @i <= (select COUNT(*) from [TB_CMD])
begin
--按照[OrderNumber]进行了排序,每次取出1条
;with t
as
(
select *,
ROW_NUMBER() over(order by [OrderNumber]) rownum
from [TB_CMD]
)
select @sql = [SqlEvent],
@OrderNumber = [OrderNumber]
from t
where rownum = @i
set @start = GETDATE()
exec(@sql);
select @error = @@ERROR,
@ROWCOUNT = @@ROWCOUNT
update [TB_CMD]
set BetweenTime = datediff(ms,@start,GETDATE()),
RunLog = case when @error = 0 then '('+cast(@ROWCOUNT as varchar)+'行影响)'
else '回滚'
end,
RunStatus = case when @error = 0 then 1
when @error <> 1 then 0
end,
IsRun = 1
where [OrderNumber] = @OrderNumber --这里也修改了
set @i = @i + 1
end
go