Consulta não está funcionando bem em loop
Eu tenho um loop While, onde estou tentando inserir.
<code> DECLARE @CurrentOffer int =121 DECLARE @OldestOffer int = 115 DECLARE @MinClubcardID bigint=0 DECLARE @MaxClubcardID bigint=1000 WHILE 1 = 1 BEGIN INSERT INTO Temp WITH (TABLOCK) SELECT top (100) clubcard from TempClub with (nolock) where ID between @MinClubcardand and @MaxClubcard declare @sql varchar (8000) while @OldestOffer <= @CurrentOffer begin print @CurrentOffer print @OldestOffer set @sql = 'delete from Temp where Clubcard in (select Clubcard from ClubTransaction_'+convert(varchar,@CurrentOffer)+' with (nolock))' print (@sql) exec (@sql) SET @CurrentOffer = @CurrentOffer-1 IF @OldestOffer = @CurrentOffer begin -- my logic end end end </code>
Minha tabela TempClub sempre verifica apenas com os primeiros 100 registros. Minha tabela TempClub tem 3000 registros. Eu preciso verificar todos os meus 3.000 registros do clubcard com a tabela ClubTransaction_121, ClubTransaction_120, ClubTransaction_119.