欢迎来到站长教程网!

mssql

当前位置:主页 > 数据库 > mssql >

SQL Server 实现数字帮助表实例代码

时间:2021-05-05|栏目:mssql|点击:

数字帮助表是一个持续整数的数列,凡是用来实现多种差异的查询任务。大多分两类:足够大物理数字表和表函数,前者可以称为静态的,后者可以称为动态且按需出产。

物理数字表

    物理数字表凡是存在一个物理表,表记录相对足够大,相关的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

执行后的查询功效如下:

SQL Server 实现数字辅佐表实例代码

 表函数

    表函数实现利用交错毗连和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

执行后的查询功效如下:

SQL Server 实现数字辅佐表实例代码
 

博友如有其他更好的办理方案,也请不惜见教,万分感激。

参考清单列表

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

感激阅读,但愿能辅佐到各人,感谢各人对本站的支持!

您大概感乐趣的文章:

上一篇:sql server 自界说支解月成果详解及实现代码

栏    目:mssql

下一篇:数据库 要害字一览表

本文标题:SQL Server 实现数字帮助表实例代码

本文地址:www.jiaocheng88.com.cn/mssql/62001.html

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:584415406 | 邮箱:584415406#qq.com(#换成@)

Copyright © 2015-2020 小白站长教程网 版权所有 苏ICP备20040415号-3