Sql server SQL根据日期范围插入多行

Sql server SQL根据日期范围插入多行,sql-server,date,Sql Server,Date,我需要为基于日期范围的多个插入编写sql。例如,我的成员12的起始日期为2014年1月1日至2014年12月31日。我需要每个月在表中插入一行12个。我需要将此信息与已经以这种方式展开的表进行比较。任何帮助都将不胜感激。 示例输入 client amount start end numpymtmths 12 $3000 01/01/2014 03/31/2014 03 13 $700

我需要为基于日期范围的多个插入编写sql。例如,我的成员12的起始日期为2014年1月1日至2014年12月31日。我需要每个月在表中插入一行12个。我需要将此信息与已经以这种方式展开的表进行比较。任何帮助都将不胜感激。 示例输入

client    amount       start           end         numpymtmths

12      $3000      01/01/2014     03/31/2014     03

13      $700       06/01/2014     12/31/2014     07
需要将其扩展为:

client  amount     paydate     
12      $1000      201401    
12      $1000      201402  
12      $1000      201403    
13      $100       201406  
13      $100       201407    
13      $100       201408  
13      $100       201409  
13      $100       201410  
13      $100       201411  
13      $100       201412 

要分割数据,您需要有一个数字表或月份表,或者动态创建它,例如使用CTE。CTE解决方案可能如下所示:

with n1(n) as (
  select 1 union all select 1 union all 
  select 1 union all select 1 union all 
  select 1 union all select 1 union all 
  select 1 union all select 1 union all 
  select 1 union all select 1
),
n(n) as (
  select row_number() over (order by (select null)) - 1 
  from n1 cross join n1 as n2
)

select client, amount / numpymtmths, dateadd(month, n.n, start) as month 
from payment
join n on n.n < numpymtmths
order by client, month
现在,加入CTE的10 x 10个数字的有效期为100个月

SQL Fiddle: