这篇文章主要为大家详细介绍了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 有关书籍的作者)
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
本文来自:http://www.q1010.com/179/9301-0.html
注:关于SQL Server数字辅助表的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:SQL SERVER
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。