I have written a stored procedure in a loop in ms sql server 2008 something like this
BEGIN TRANSACTION
WHILE(@first <= @last)
BEGIN
Select @LineOfAuthorityNameSubString = TempLineOfAuthority from #tbTempLineOfAuthority;
Select @tbLineOfAuthorityId = LineOfAuthority
from tbLineOfAuthority where LineOfAuthorityX = @LineOfAuthorityNameSubString;
INSERT INTO tbProductLineOfAuthority(ProductId, LineOfAuthortyId)
VALUES(@tbProductId, @tbLineOfAuthorityId);
SET @first += @step
END
COMMIT TRANSACTION
Now the issue is in this line of code
Select @tbLineOfAuthorityId = LineOfAuthority
from tbLineOfAuthority where LineOfAuthorityX = @LineOfAuthorityNameSubString;
The variable @tbLineOfAuthorityId is getting the same value at all the time in loop. Please help me!!!
@tbProductIdand@tbLindeOfAuthorityIdfor each loop of your loop, is this the intended behaviour?@first,@lastand@stepare not used in the body of the loop, they can only make the body run repeatedly. Nothing in the loop body effects the value@tbLineOfAuthorityIdwill be be set to, so it will always be the same value.