I at trying to select 1000 rows from some tables in SQL Server and in some cases is easy because I do have a key which is a bigint. Thus, I store the last number of a key that I have fetched and then I add to it 100. See below:
--Get the last loaded record
DECLARE @StartIndex BIGINT
SET @StartIndex = 1 + (SELECT [StoredIndex]
FROM [DB].[dbo].[MasterData]
WHERE [Status] = 'LastLoad')
--Declare and set the last record that was loaded
DECLARE @EndIndex BIGINT
--Retrieve the next @Step+1 or less records and store them in a temporary table
SELECT T1.*
INTO #TempResults
FROM
--Get the next @Step+1 or less records
(SELECT *
FROM ANOTHERDB.[TableName]
WHERE [Tables_ID] BETWEEN @StartIndex AND @StartIndex + 1000) T1
--Set the index of the last record inserted
SET @EndIndex = (SELECT MAX([Tables_ID])--The next record fetched with the largest ID
FROM #TempResults)
However how to do that when the key is an alphanumeric value?
What is the equivalent for
WHERE [Tables_ID] BETWEEN @StartIndex AND @StartIndex + 1000
If the @StartIndex
is a nvarchar
, for example 123g7_56y7f
?
Thank yoU!