0
I have a select to bring all the records that have been entered and I need to do a while to go through this select and give an Insert in another temporary table. Only that the while is always bringing the last record
What could be wrong?
DECLARE @CONTADOR INT
DECLARE @CONTADORLOOP INT = 1
SELECT @CONTADOR = COUNT(*) FROM #tableConta
SELECT * FROM #tableConta
WHILE(@CONTADORLOOP IS NOT NULL AND @CONTADORLOOP <= @CONTADOR)
BEGIN
SELECT @EMP_VLR = EMPCOD, @PLANO_COD_RED = PLANOCODRED FROM #tableConta where PLANOCODRED = @P_PLANO_CTA_COD_RED
PRINT @EMP_VLR
--INSERT INTO #tableDebCred (TOTAL2, EMPCOD2, PLANOCODRED) VALUES (@VLRTOTAL2, @EMP_VLR, @PLANO_COD_RED)
SET @CONTADORLOOP = @CONTADORLOOP + 1
END
END
What’s in
@P_PLANO_CTA_COD_RED
?– 8biT
it takes the value declared in a variable there was 18906 as you can see in the image
– Felipe Michael da Fonseca
inside your while you recover all the records of
#tableConta
since yourPLANOCODRED
is the same for all lines. Is this what you want to do ? What’s more, theInsert into
withselect
does not serve you in this case ?– 8biT
what would that be like? There are some examples?
– Felipe Michael da Fonseca
See if it answers. Example
– 8biT