That because when no more id is found (or a gap in the id sequence is found), @intRowCount becomes Zero and never increments any more. Use a code like this instead.DECLARE @CurrentID INT, @MaxID INT, @FileName VARCHAR(100), @FormatFile VARCHAR(100), @bcpCommand VARCHAR(2000)SELECT @CurrentID = MIN(Prov_No), @MaxID = MAX(Prov_No)FROM EY_TestWHILE @CurrentID <= @MaxID BEGIN ---run bcp command SELECT @CurrentID = MIN(Prov_No) FROM EY_Test WHERE Prov_No > @CurrentID END
It starts with the minimum id and ends with maximum id and in between, it never goes into a gap or missing id.Peter LarssonHelsingborg, Sweden