Я использую SQL Server 2012. Пожалуйста, ознакомьтесь с T SQL ниже, чтобы подготовиться к вопросу:
create table PersonBatchTable (ID int identity not null, name varchar(30), complete bit)
GO
insert into PersonBatchTable ([name],complete) values ('Kevin',0)
insert into PersonBatchTable ([name],complete) values ('Adam',0)
insert into PersonBatchTable ([name],complete) values ('Marie',0)
insert into PersonBatchTable ([name],complete) values ('Craig',0)
--lots more inserts
go
create procedure StoredProcedureIHaveNoControlOver
as
begin
--Much more complex TSQL here
--Ends with the line below
update PersonBatchTable set complete=1
end
и T SQL У меня есть вопрос о:
create Procedure MyProcedure
as
begin
begin try
if object_id('tempdb.dbo.#PersonBatchTableBackup', 'U') is not null drop table #PersonBatchTableBackup
select * into #PersonBatchTableBackup from PersonBatchTable
delete from PersonBatchTable where ID not in (select top 1 id from PersonBatchTable where complete=0) --in reality this would say more like top 100
exec StoredProcedureIHaveNoControlOver
SET IDENTITY_INSERT PersonBatchTable ON
insert into PersonBatchTable (ID,[name],complete)
select ID,[name],complete from #PersonBatchTableBackup where ID not in (select ID from PersonBatchTable)
SET IDENTITY_INSERT PersonBatchTable OFF
end try
begin catch
SET IDENTITY_INSERT PersonBatchTable ON
insert into PersonBatchTable (ID,[name],complete)
select ID,[name],complete from #PersonBatchTableBackup where ID not in (select ID from PersonBatchTable)
SET IDENTITY_INSERT PersonBatchTable OFF
end catch
end
В его нынешнем виде все записи PersonBatch будут завершены после того, как хранимая процедура будет запущена четыре раза - следовательно, через четыре часа, если она будет выполняться каждый час.
Могу ли я быть уверен, что таблица PersonBatch будет всегда быть заселенным? Выполнение внутренней хранимой процедуры занимает десять минут и может вызывать блокировки. Следовательно, я наблюдаю множество ошибок "жертвы взаимоблокировки", распространяющихся от внутренней хранимой процедуры к внешней хранимой процедуре.