我正在使用SQL Server 2012
我需要在PRDate
,udEmp
和PRCo
的{{1}}表格中插入Employee
。我有两个参数Job
和@BegDate
,例如表中的原始记录,如
@EndDate
我需要一些代码才能得到像这样的结果
@BegDate '07/27/2014'
@EndDate '08/10/2014'
select Co, Job, Employee
from udEmp
将从PRDate
开始,并在BegDate
结束。我认为光标可以做到,我怎么也想不通。请帮忙。感谢
答案 0 :(得分:1)
您不需要光标。一个简单的递归CTE解决了这个问题:
with dates as (
select cast(@BegDate as date) as thedate
union all
select dateadd(day, 7, thedate)
from dates
where thedate < @EndDate
)
select e.Co, e.Job, e.Employee, dates.thedate as prdate
from udemp e cross join
dates;
如果你真正需要的话,这很容易适应insert
。