我有一个存储过程构建更新查询,并且每次都在循环中存储在临时表中。
总查询内置UNKNOWN(2到4)
如何在事务中放置多个查询?
例如,临时表在列EXPSQL
(nvarchar
)
id EXPSQL
1 Update tableA SET Name = 'Test' WHERE id=1
2 Update tableB SET Name = 'Test2' WHERE id=10
如何开始循环执行以上查询的事务?或者还有其他方式吗?
while @id < total
begin
set @id = @id +1
select @SQL = EXPSQL FROM #TEMPTABLE WHERE id=@id
EXEC (@SQL)
end
由于
答案 0 :(得分:0)
如果你想一个一个地执行:
begin transaction
while @id < total
begin
set @id = @id +1
select @SQL = EXPSQL FROM #TEMPTABLE WHERE id=@id
EXEC (@SQL)
-- if a error occurs go back to the original state
if(@@error <> 0)
rollback transaction
end
commit transaction
如果你想分别执行每一个:
while @id < total
begin
set @id = @id +1
select @SQL = EXPSQL FROM #TEMPTABLE WHERE id=@id
begin transaction
EXEC (@SQL)
-- if a error occurs go back to the original state
if(@@error <> 0)
rollback transaction
else
commit transaction
end