将记录插入存储过程的表中

时间:2014-08-18 21:41:10

标签: sql sql-server-2012

我正在使用SQL Server 2012

我需要在PRDateudEmpPRCo的{​​{1}}表格中插入Employee。我有两个参数Job@BegDate,例如表中的原始记录,如

@EndDate

enter image description here

我需要一些代码才能得到像这样的结果

enter image description here

@BegDate '07/27/2014' @EndDate '08/10/2014' select Co, Job, Employee from udEmp 将从PRDate开始,并在BegDate结束。我认为光标可以做到,我怎么也想不通。请帮忙。感谢

1 个答案:

答案 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