欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用CTE生成辅助表(数字或时间)等

程序员文章站 2022-05-25 20:26:38
数字辅助表: DECLARE @start_digital INT = 0, @end_digital INT = 9 ;WITH Digital_Rangs(Digital) AS ( SELECT @start_digital UNION ALL SELECT [Digital] = [Digi ......

数字辅助表:

 

declare @start_digital int = 0,
        @end_digital int = 9

;with digital_rangs(digital) 
as
(
    select  @start_digital 
    union all
    select [digital] = [digital] + 1
    from [digital_rangs]
    where [digital] < @end_digital
)

select [digital] from [digital_rangs] option (maxrecursion 0);

 

时间辅助表:

 

 

declare @start_date date = '2019-05-01',
        @end_date date = '2019-05-20'

;with [date_rangs]([date]) 
as
(
    select @start_date 
    union all
    select [date] = dateadd(day,1,[date])
    from [date_rangs]
    where [date] < @end_date
)

select [date] from [date_rangs] option (maxrecursion 0);