【问题标题】:Auto-populating a table with DateTime values in SQL server在 SQL Server 中使用 DateTime 值自动填充表
【发布时间】:2015-02-08 22:17:37
【问题描述】:

我有一个名为Appointment 的表,我想在其中显示从10am to 3pm 到Weekdays 到August 的预约空档(60min intervals)。

下面是我的表结构:

AppointmentID
Date
StartTime
EndTime

样本记录:

1
2015-2-2
11:45:34
12:45:34

我知道我可以手动执行 Insert 语句,但我想编写一个自动执行此操作的 SQL 查询,并感谢您的帮助。

【问题讨论】:

    标签: sql-server datetime


    【解决方案1】:

    您可能需要考虑在插入语句中使用DATEADD。

    【讨论】:

      【解决方案2】:

      试试这个:

      insert Appointment([Date],StartTime,EndTime)
      select cast(DATEADD(day, n, StartTime) as date)
      , cast(StartTime as time)
      , cast(dateadd(hour,1,DATEADD(day, n, StartTime)) as time) 
      from
      (
          select DATEADD(hour, n, '2015-01-01 10:00') StartTime
          from dbo.generate_series(0, DATEDIFF(hour,'2015-01-01 10:00','2015-01-01 15:00'), 1, 0)
      ) StartTimes
      cross join dbo.generate_series(0, DATEDIFF(day,'2015-01-01','2015-08-31'), 1, 0)
      order by AppointmentStart
      

      使用来自https://developer42.wordpress.com/2014/09/17/t-sql-generate-series-getting-a-list-of-numbers-in-a-given-range/的generate_series

      create function dbo.generate_series
      (
            @start bigint
          , @stop bigint
          , @step bigint = 1
          , @maxResults bigint = 0 --0 = unlimited
      )
      returns @results table(n bigint)
      as
      begin
      
          --avoid infinite loop (i.e. where we're stepping away from stop instead of towards it)
          if @step = 0 return
          if @start > @stop and @step > 0 return
          if @start < @stop and @step < 0 return
      
          --ensure we don't overshoot
          set @stop = @stop - @step
      
          --treat negatives as unlimited
          set @maxResults = case when @maxResults < 0 then 0 else @maxResults end
      
          --generate output
          ;with myCTE (n,i) as
          (
              --start at the beginning
              select @start
              , 1
              union all
              --increment in steps
              select n + @step
              , i + 1
              from myCTE 
              --ensure we've not overshot (accounting for direction of step)
              where (@maxResults=0 or i<@maxResults)
              and
              (
                     (@step > 0 and n <= @stop)
                  or (@step < 0 and n >= @stop)
              )  
          )
          insert @results
          select n 
          from myCTE
          option (maxrecursion 0) --sadly we can't use a variable for this; however checks above should mean that we have a finite number of recursions / @maxResults gives users the ability to manually limit this 
      
          --all good  
          return
      
      end
      

      【讨论】:

      • 您好约翰,感谢您的回答。我只是尝试使用插入语句,但它不起作用。
      • 我已经调整了代码以使用您的表结构(之前我刚刚创建了代码以提供日期/时间)。另外要确认的是,您的 AppointmentID 是否是一个身份字段(即 SQL 引擎将填充此字段,而不是您需要插入此列)。
      • 嗨,john,它说该表是无效对象
      • 哪个表;约定?您是否已经在数据库中创建了该表?您能在此处复制并粘贴完整的错误消息吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多