數(shù)字輔助表是一個連續(xù)整數(shù)的數(shù)列,通常用來實(shí)現(xiàn)多種不同的查詢?nèi)蝿?wù)。大多分兩類:足夠大物理數(shù)字表和表函數(shù),前者可以稱為靜態(tài)的,后者可以稱為動態(tài)且按需生產(chǎn)。
物理數(shù)字表
物理數(shù)字表通常存在一個物理表,表記錄相對足夠大,相關(guān)的T-SQL代碼如下:
IF OBJECT_ID(N'dbo.Nums', 'U') IS NOT NULL
BEGIN
DROP TABLE dbo.Nums;
END
GO
CREATE TABLE dbo.Nums
(
Num INT NOT NULL,
CONSTRAINT PK_U_CL_Nums_Num PRIMARY KEY CLUSTERED
(
Num ASC
)
);
GO
INSERT INTO dbo.Nums (Num)
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS RowNum
FROM master.dbo.spt_values;
GO
注意:如何填充物理數(shù)字表的方法很多,為了演示作用使用了一種。
測試的T-SQL代碼如下:
1 SELECT Num
2 FROM dbo.Nums;
3 GO
執(zhí)行后的查詢結(jié)果如下:
表函數(shù)
表函數(shù)實(shí)現(xiàn)使用交叉連接和CTE,SQL Server 2005和以上版本的T-SQL代碼如下:
IF OBJECT_ID(N'dbo.ufn_GetNums', N'IF') IS NOT NULL
BEGIN
DROP TABLE dbo.ufn_GetNums;
END
GO
--==================================
-- 功能: 獲取指定范圍的數(shù)字?jǐn)?shù)列
-- 說明: 交叉最后層級的CTE得到的數(shù)據(jù)行:在L級(從0開始計數(shù))得到的行的總數(shù)為2^2^L。
-- 例如:在5級就會得到4 294 967 596行。5級的CTE提供了超過40億的行。
-- 作者: XXX
-- 創(chuàng)建: yyyy-MM-dd
-- 修改: yyyy-MM-dd XXX 修改內(nèi)容描述
--==================================
CREATE FUNCTION dbo.ufn_GetNums
(
@bintLow BIGINT,
@bintHigh BIGINT
) RETURNS TABLE
AS
RETURN
WITH
L0 AS (SELECT c FROM (VALUES(1), (1)) AS LO(c)),
L1 AS (SELECT 1 AS c FROM L0 AS T CROSS JOIN L0 AS T2),
L2 AS (SELECT 1 AS c FROM L1 AS T CROSS JOIN L1 AS T2),
L3 AS (SELECT 1 AS c FROM L2 AS T CROSS JOIN L2 AS T2),
L4 AS (SELECT 1 AS c FROM L3 AS T CROSS JOIN L3 AS T2),
L5 AS (SELECT 1 AS c FROM L4 AS T CROSS JOIN L4 AS T2),
Nums AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS RowNum FROM L5)
SELECT TOP (@bintHigh - @bintLow + 1) @bintLow + RowNum - 1 AS Num
FROM Nums
ORDER BY RowNum ASC;
GO
SQL Server 2012增加了有關(guān)分頁的新特性,相關(guān)的T-SQL代碼如下:
IF OBJECT_ID(N'dbo.ufn_GetNums2', N'IF') IS NOT NULL
BEGIN
DROP TABLE dbo.ufn_GetNums2;
END
GO
--==================================
-- 功能: 獲取指定范圍的數(shù)字?jǐn)?shù)列
-- 說明: 交叉最后層級的CTE得到的數(shù)據(jù)行:在L級(從0開始計數(shù))得到的行的總數(shù)為2^2^L。
-- 例如:在5級就會得到4 294 967 596行。5級的CTE提供了超過40億的行。
-- 作者: XXX
-- 創(chuàng)建: yyyy-MM-dd
-- 修改: yyyy-MM-dd XXX 修改內(nèi)容描述
--==================================
CREATE FUNCTION dbo.ufn_GetNums2
(
@bintLow BIGINT,
@bintHigh BIGINT
) RETURNS TABLE
AS
RETURN
WITH
L0 AS (SELECT c FROM (VALUES(1), (1)) AS LO(c)),
L1 AS (SELECT 1 AS c FROM L0 AS T CROSS JOIN L0 AS T2),
L2 AS (SELECT 1 AS c FROM L1 AS T CROSS JOIN L1 AS T2),
L3 AS (SELECT 1 AS c FROM L2 AS T CROSS JOIN L2 AS T2),
L4 AS (SELECT 1 AS c FROM L3 AS T CROSS JOIN L3 AS T2),
L5 AS (SELECT 1 AS c FROM L4 AS T CROSS JOIN L4 AS T2),
Nums AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS RowNum FROM L5)
SELECT @bintLow + RowNum - 1 AS Num
FROM Nums
ORDER BY RowNum ASC
OFFSET 0 ROWS FETCH FIRST @bintHigh - @bintLow + 1 ROWS ONLY;
GO
以函數(shù)ufn_GetNums為例,演示相關(guān)的效果。獲取指定范圍的數(shù)字序列的T-SQL代碼如下:
SELECT Num
FROM dbo.ufn_GetNums(11, 20);
GO
執(zhí)行后的查詢結(jié)果如下:
博友如有其他更好的解決方案,也請不吝賜教,萬分感謝。
參考清單列表
1、《Microsoft SQL Server 2012 High-Performance T-SQL Using Window Functions》 作者 Itzik Ben-Gan(美國)(SQL Server Inside 有關(guān)書籍的作者)
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
您可能感興趣的文章:- C#訪問SQLServer增刪改查代碼實(shí)例
- SqlServer查詢和Kill進(jìn)程死鎖的語句
- SQLServer批量更新兩個關(guān)聯(lián)表數(shù)據(jù)的方法
- sqlserver進(jìn)程死鎖關(guān)閉的方法
- 獲取SqlServer存儲過程定義的三種方法
- SqlServer存儲過程實(shí)現(xiàn)及拼接sql的注意點(diǎn)
- win2008 r2 服務(wù)器php+mysql+sqlserver2008運(yùn)行環(huán)境配置(從安裝、優(yōu)化、安全等)
- SqlServer2008誤操作數(shù)據(jù)(delete或者update)后恢復(fù)數(shù)據(jù)的方法
- Windows2012配置SQLServer2014AlwaysOn的圖解