Select 当键不是数字而是字母数字值时循环中的特定行数
Select a specific number of rows in a loop when the key is not number but an alphanumeric value
我试图从 SQL 服务器中的某些表中 select 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
?
谢谢你!
方法一:
如果您使用 SQL Server 2012 或更高版本,您可以使用 offset fetch
,如下所示:
SELECT *
FROM ANOTHERDB.[TableName]
ORDER BY Col1
OFFSET XX ROWS FETCH NEXT YY ROWS ONLY;
Col1 = 您要对数据进行排序的列
XX = 要跳过的行数
YY = 要获取的行数
方法二:
您可以使用Row_Number
功能(SQL Server 2008或更高版本),如下:
SELECT *
FROM ( SELECT * ,
ROW_NUMBER() OVER ( ORDER BY COL1) AS RN
FROM ANOTHERDB.[TableName]
) AS K
WHERE K.RN BETWEEN XX AND YY;
SQL table 没有 default order. So unless you can define one using the ORDER BY clause 没有前 1,000 条或后 1,000 条记录。
如果您尝试 return 将最后 1,000 条记录添加到 table,您将需要捕获每条记录的创建日期和时间。你不能依赖 identify column, as these can be reseeded or updated.
合并 TOP and ORDER BY 以限制 return 记录的数量。升序 return 是前 n 个结果。降序最后 n 个结果。
-- Limits results to the last 1,000 records.
SELECT TOP 1000
*
FROM
YourTable
ORDER BY
YourColumn DESC
;
根据需要替换 YourTable
和 YourColumn
。
我试图从 SQL 服务器中的某些表中 select 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
?
谢谢你!
方法一:
如果您使用 SQL Server 2012 或更高版本,您可以使用 offset fetch
,如下所示:
SELECT *
FROM ANOTHERDB.[TableName]
ORDER BY Col1
OFFSET XX ROWS FETCH NEXT YY ROWS ONLY;
Col1 = 您要对数据进行排序的列
XX = 要跳过的行数
YY = 要获取的行数
方法二:
您可以使用Row_Number
功能(SQL Server 2008或更高版本),如下:
SELECT *
FROM ( SELECT * ,
ROW_NUMBER() OVER ( ORDER BY COL1) AS RN
FROM ANOTHERDB.[TableName]
) AS K
WHERE K.RN BETWEEN XX AND YY;
SQL table 没有 default order. So unless you can define one using the ORDER BY clause 没有前 1,000 条或后 1,000 条记录。
如果您尝试 return 将最后 1,000 条记录添加到 table,您将需要捕获每条记录的创建日期和时间。你不能依赖 identify column, as these can be reseeded or updated.
合并 TOP and ORDER BY 以限制 return 记录的数量。升序 return 是前 n 个结果。降序最后 n 个结果。
-- Limits results to the last 1,000 records.
SELECT TOP 1000
*
FROM
YourTable
ORDER BY
YourColumn DESC
;
根据需要替换 YourTable
和 YourColumn
。