2015-12-21 88 views
1

我在我的SQL表中有一個日期時間字段。我創建了一個將count作爲變量併爲表生成記錄的過程。如果計數爲5會產生我想要的是,當我提供5作爲輸入參數表中的日期時間字段應該有這麼一個記錄插入每一個時間值基於輸入參數自動填充日期時間值的SQL表字段

12/20/2015 9:00 
12/20/2015 11:00 
12/20/2015 13:00 
12/20/2015 15:00 
12/20/2015 17:00 

自動填充5 records.The邏輯放入表格中,應該添加2小時的時間。

+0

我的不好。我使用MS SQL-Server –

+0

從...開始? –

+0

在任何時候您將添加的記錄數量是否有上限和下限?你也有一些你試過的代碼嗎? –

回答

0

遞歸CTEs是動態創建記錄的一種方式。這裏的關鍵是創建一個錨點(這是CTE中的第一個SELECT,這是您的出發點)。和一個退出檢查(這是WHERE子句)。

如果您想一次創建超過100條記錄,請閱讀MAXRECURSION。

DECLARE @RecordsRequired INT = 5; 
DECLARE @BaseDateTime  SMALLDATETIME = GETDATE(); 


WITH [Sample] AS 
    (
     /* This CTE uses recursion to create the required number of 
     * records. 
     */ 
      SELECT 
       1    AS RowNumber, 
       @BaseDateTime AS [DateTime] 

     UNION ALL 

      SELECT 
       RowNumber + 1     AS RowNumber, 
       DATEADD(HOUR, 2, [DateTime]) AS [DateTime] 
      FROM 
       [Sample] 
      WHERE 
       RowNumber < @RecordsRequired 
    ) 
SELECT 
    RowNumber, 
    [DateTime] 
FROM 
    [Sample] 
; 

你也可以考慮WHILE塊。

0

使用此代碼:

------------------ INPUT ------------------------ 
declare @start_date datetime = '01/01/2000 14:00' 
declare @loops int = 5 
------------------------------------------------- 

declare @i int = 0 
while (@i < @loops) begin 
    select dateadd(hour, @i * 2, @start_date) 
    set @i = @i + 1 
end 
0

試試這個沒有LOOP

Declare @count int = 5, 
     @incrementer int =2 -- in case if you want to change the incrementer 

SELECT Dateadd(hh, num * @incrementer, dates) 
FROM (SELECT Cast(CONVERT(VARCHAR(20), Dateadd(dd, 1, Getdate()), 111) 
        + ' 9:00 AM' AS DATETIME) AS Dates, 
       num 
     FROM (VALUES(0),(1),(2),(3),(4),(5)) TC (num)) A 
WHERE num <= @count - 1 
0
Create Table dates 
    (
    datetimefield datetime not null 
    ) 
    go 

Create Procedure FillDateTimeField 
@insertxrows int 
AS 
begin 
Declare @LastDateTimeInserted as datetime 
set @LastDateTimeInserted = (select isnull(max(datetimefield),getdate()) from Dates) 
;WITH norows AS (
    SELECT 1 as num, Dateadd(hour,2,@LastDateTimeInserted) as FirstRecord 
    UNION ALL 
    SELECT num + 1, dateadd(hour,2,firstrecord) FROM 
    norows 
    WHERE num < @insertxrows 
) 
insert into dates 
select firstrecord from norows 
end 
0

普萊斯e找到下面的示例代碼,它包含您需要的邏輯。希望能幫助到你!!

--Create a temp table for sample output 
CREATE TABLE #temp 
( 
CreatedDate datetime 
) 

--Declaring variables 
DECLARE @Count int 
DECLARE @TimeCounter int 
--intializing values 
SET @Count=5 
SET @TimeCounter=0 


WHILE(@Count>0) 
BEGIN 
--SELECT getdate()+1 
insert into #temp(#temp.CreatedDate) Select DATEADD(hour,@TimeCounter,getdate()) 
SET @[email protected]+2 
SET @[email protected] 
END 
--Final values 
SELECT * FROM #temp tmp 
--Dropping table 
DROP TABLE #temp 
0

這是用數字表格/函數最好解決的那些問題之一。與遞歸或循環相比,代碼少得多,通常對於任何不平凡且更可重用的更快的代碼。

你想要的核心代碼是

CREATE PROCEDURE usp_PopulateAppointments 
(
    @StartDateTime datetime2(3), 
    @Records  int, 
    @Interval  int = 120  --Time between appointment slots in minutes. Default to 2h if not manually specified. 
) 

INSERT INTO Appointments 
SELECT 
    DATEADD(m, @Interval * Number, @StartDateTime) 
FROM dbo.udfNumbers(0, @Recs) 

我認爲在這一個數字函數,它@StartAt和@NumberResults。我在http://sqlblog.com/blogs/adam_machanic/archive/2006/07/12/you-require-a-numbers-table.aspx的評論中使用了從Adam的最終代碼中派生出來的一個 - 根據我的經驗,它比實際的表更快,並且佔用更少的空間。

相關問題