sql server使用公用表表达式CTE通过递归方式编写通用函数自动生成连续数字和日期

作者:张工502219048 时间:2024-01-24 15:34:17 

问题:

在数据库脚本开发中,有时需要生成一堆连续数字或者日期,例如yearly report就需要连续数字做年份,例如daily report就需要生成一定时间范围内的每一天日期。

而自带的系统表master..spt_values存在一定的局限性,只是从0到2047(验证脚本:select * from master..spt_values b where b.type = 'P'),也不能直接生成连续日期。

可能大部分人会想到一个笨办法,通过while循环去逐条插入数据到临时表,每次数字加1或者日期加1天,但这样和数据库服务器的交互就太频繁了。如果生成1W个连续数字,那就要跟数据库服务器交互1W次,可怕!如果是有1000个客户端都需要调用这个while循环,那就是1000W次!可怕!

解决方案:

可以使用公用表表达式CTE通过递归方式实现,并编写为一个通用表值函数方便调用,封装起来简化使用,返回表格式数据。

CTE是在内存中准备好数据,而不是每次一条往返服务器和客户端一次。如果需要再插入到临时表的话就是全部数据一次性插入。

如果传入参数为数字,则生成连续数字;如果传入参数为日期,则生成连续日期。
是不是觉得很方便呢?

函数脚本:


if object_id('dbo.fun_ConcatStringsToTable') is not null drop function dbo.fun_ConcatStringsToTable
go
/*
 功能:连续字符串(数字或日期)以table形式返回
 作者:zhang502219048 2018-12-10
 脚本来源:https://www.cnblogs.com/zhang502219048/p/11108991.html
-- 示例1(数字):
 select * from dbo.fun_ConcatStringsToTable(1, 10000)
-- 示例2(数字文本):
 select * from dbo.fun_ConcatStringsToTable('1', '10000')
-- 示例3(日期):
 declare @dateBegin datetime = '2009-1-1', @dateEnd datetime = '2018-12-31'
 select * from dbo.fun_ConcatStringsToTable(@dateBegin, @dateEnd)
-- 示例4(日期文本):
 select * from dbo.fun_ConcatStringsToTable('2009-1-1', '2018-12-31')
**/
create function [dbo].[fun_ConcatStringsToTable]
(
 @strBegin as nvarchar(100),
 @strEnd as nvarchar(100)
)
returns @tempResult table (vid nvarchar(100))
as
begin
 --数字
 if isnumeric(@strBegin) = 1 and isnumeric(@strEnd) = 1
 begin
   --使用CTE递归批量插入数字数据
   ;with cte_table(id) as
   (
     select cast(@strBegin as int)
     union all
     select id + 1
     from cte_table
     where id < @strEnd
   )
   insert into @tempResult
   select cast(id as nvarchar(100))
   from cte_table
   option (maxrecursion 0)
 end
 --日期
 else if isdate(@strBegin) = 1 and isdate(@strEnd) = 1
 begin
   --使用CTE递归批量插入日期数据
   ;with cte_table(CreatedDate) as
   (
     select cast(@strBegin as datetime)
     union all
     select dateadd(day, 1, CreatedDate)
     from cte_table
     where CreatedDate < @strEnd
   )
   insert into @tempResult
   select convert(varchar(10), CreatedDate, 120)
   from cte_table
   option (maxrecursion 0)
 end

return;
end
go

调用函数示例:


-- 示例1(数字):
 select * from dbo.fun_ConcatStringsToTable(1, 10000)
-- 示例2(数字文本):
 select * from dbo.fun_ConcatStringsToTable('1', '10000')
-- 示例3(日期):
 declare @dateBegin datetime = '2009-1-1', @dateEnd datetime = '2018-12-31'
 select * from dbo.fun_ConcatStringsToTable(@dateBegin, @dateEnd)
-- 示例4(日期文本):
 select * from dbo.fun_ConcatStringsToTable('2009-1-1', '2018-12-31')

脚本运行结果:

sql server使用公用表表达式CTE通过递归方式编写通用函数自动生成连续数字和日期
sql server使用公用表表达式CTE通过递归方式编写通用函数自动生成连续数字和日期 sql server使用公用表表达式CTE通过递归方式编写通用函数自动生成连续数字和日期

  结论:

从上面几个图可以看到,通过简单调用fun_ConcatStringsToTable这个自定义表值函数,指定起止数字或日期,就达到了生成连续数字和日期的目的。

扩展:

如果想生成连续月份呢?博主在这里也帮大家写了一下脚本,如果需要可以在此基础上再自行做成表值函数:


with cte_table(CreatedDate) as
(
 select cast('2017-12-1' as datetime)
 union all
 select dateadd(month, 1, CreatedDate)
 from cte_table
 where CreatedDate < '2018-04-01'
)
select convert(varchar(7), CreatedDate, 120) as YearMonth
from cte_table
option (maxrecursion 0)

总结

以上所述是小编给大家介绍的sql server使用公用表表达式CTE通过递归方式编写通用函数自动生成连续数字和日期 ,网站的支持!如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

来源:https://www.cnblogs.com/zhang502219048/archive/2019/06/30/11108991.html

标签:sqlserver,递归,生成
0
投稿

猜你喜欢

  • python暴力解压rar加密文件过程详解

    2023-11-20 06:28:38
  • Python 爬虫学习笔记之多线程爬虫

    2022-10-03 15:10:37
  • asp.net iis 无法显示网页的解决方法分析

    2023-07-21 23:34:53
  • python3.7调试的实例方法

    2022-09-22 17:16:24
  • VUE-ElementUI 时间区间选择器的使用

    2024-05-09 15:12:21
  • 浅谈Python由__dict__和dir()引发的一些思考

    2021-10-06 06:14:10
  • oracle日志操作模式(归档模式和非归档模式的利与弊)

    2024-01-24 18:01:36
  • 基于Python自制一个文件解压缩小工具

    2021-10-15 14:28:00
  • MySQL 8.0 驱动与阿里druid版本兼容问题解决

    2024-01-17 06:25:49
  • python多重继承新算法C3介绍

    2022-05-28 12:27:04
  • 使用httplib模块来制作Python下HTTP客户端的方法

    2021-03-10 09:27:37
  • Pandas中常用的七个时间戳处理函数使用总结

    2023-05-28 21:41:33
  • 将matplotlib绘图嵌入pyqt的方法示例

    2022-11-10 06:59:14
  • 浅析Python 条件控制语句

    2023-08-31 02:59:46
  • python list使用示例 list中找连续的数字

    2022-10-16 19:49:46
  • vue中的mixins混入使用方法

    2023-07-02 17:01:47
  • MySQL七大JOIN的具体使用

    2024-01-28 04:11:31
  • MySQL的存储引擎InnoDB和MyISAM

    2024-01-26 02:19:30
  • Python 自动化表单提交实例代码

    2022-12-20 06:16:14
  • JavaScript+canvas实现七色板效果实例

    2023-08-09 09:48:10
  • asp之家 网络编程 m.aspxhome.com