Answer the question
In order to leave comments, you need to log in
What causes sql script to loop?
[MS SQL Server]
Good afternoon, data gets into one [Contact Message In] table, there is a Processed sign, it is set to 0. I need to loop through all such records, somehow process and set Processed to 1. Here is my code:
declare @nullDate datetime = '1753-01-01'
, @exchangeCode varchar(20) = null;
select top 1 @exchangeCode=No_ from [dbo].[Contact Message In] where Processed=0;
while(@exchangeCode is not null)
begin
Print 'start loop ' + @exchangeCode
--код обработки, вырезал для краткости
--помечаем записи обработанными
update [Contact Message In] set Processed=1, [Received Message Date Time]=getdate() where [email protected] and Processed=0;
Print 'updated' --вот тут фактически не происходит обновление
--берём следующую строку
select top 1 @exchangeCode=No_ from [dbo].[Contact Message In] where Processed=0;
Print 'next loop for ' + @exchangeCode --выводит ту же самую запись, хотя должен брать следующую
end
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question