我试图从SQL Server中的某些表中选择1000行,并且在某些情况下很容易,因为我确实有一个密钥,它是BIGINT。因此,我店,我已经取了关键的最后一个数字,然后我给它添加100见下文:当密钥不是数字,而是字母数字值时,选择循环中的特定行数
--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)
但是如何做到这一点的时候,关键是字母数字值?
什么是
WHERE [Tables_ID] BETWEEN @StartIndex AND @StartIndex + 1000
相当于如果@StartIndex
是nvarchar
,例如123g7_56y7f
?
感谢yoU!
是你的'[Tables_ID]'可以包含NVARCHAR数据吗?我不这么认为 –
在我感兴趣的第二种情况下,Table_ID(即一个虚拟名称)是varchar(64) – aragorn