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

SQL Server 完成數字輔佐表實例代碼

編輯:MSSQL

SQL Server 完成數字輔佐表實例代碼。本站提示廣大學習愛好者:(SQL Server 完成數字輔佐表實例代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是SQL Server 完成數字輔佐表實例代碼正文


數字輔佐表是一個延續整數的數列,通常用來完成多種不同的查詢義務。大多分兩類:足夠大物理數字表和表函數,前者可以稱為靜態的,後者可以稱為靜態且按需消費。

物理數字表

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