程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SqlServer數據庫 >> 關於SqlServer >> SQL Server 實現數字輔助表實例代碼

SQL Server 實現數字輔助表實例代碼

編輯:關於SqlServer

數字輔助表是一個連續整數的數列,通常用來實現多種不同的查詢任務。大多分兩類:足夠大物理數字表和表函數,前者可以稱為靜態的,後者可以稱為動態且按需生產。

物理數字表

    物理數字表通常存在一個物理表,表記錄相對足夠大,相關的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

注意:如何填充物理數字表的方法很多,為了演示作用使用了一種。

測試的T-SQL代碼如下:

1 SELECT Num
2 FROM dbo.Nums;
3 GO

執行後的查詢結果如下:

 表函數

    表函數實現使用交叉連接和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
 
--==================================
-- 功能: 獲取指定范圍的數字數列
-- 說明: 交叉最後層級的CTE得到的數據行:在L級(從0開始計數)得到的行的總數為2^2^L。
--    例如:在5級就會得到4 294 967 596行。5級的CTE提供了超過40億的行。
-- 作者: XXX
-- 創建: yyyy-MM-dd
-- 修改: yyyy-MM-dd XXX 修改內容描述
--==================================
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增加了有關分頁的新特性,相關的T-SQL代碼如下:

IF OBJECT_ID(N'dbo.ufn_GetNums2', N'IF') IS NOT NULL
BEGIN
  DROP TABLE dbo.ufn_GetNums2;
END
GO
 
--==================================
-- 功能: 獲取指定范圍的數字數列
-- 說明: 交叉最後層級的CTE得到的數據行:在L級(從0開始計數)得到的行的總數為2^2^L。
--    例如:在5級就會得到4 294 967 596行。5級的CTE提供了超過40億的行。 
-- 作者: XXX
-- 創建: yyyy-MM-dd
-- 修改: yyyy-MM-dd XXX 修改內容描述
--==================================
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

以函數ufn_GetNums為例,演示相關的效果。獲取指定范圍的數字序列的T-SQL代碼如下:

SELECT Num
FROM dbo.ufn_GetNums(11, 20);
GO

執行後的查詢結果如下:


 

博友如有其他更好的解決方案,也請不吝賜教,萬分感謝。

參考清單列表

1、《Microsoft SQL Server 2012 High-Performance T-SQL Using Window Functions》 作者 Itzik Ben-Gan(美國)(SQL Server Inside 有關書籍的作者)

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved