基于输入参数自动填充日期时间值的SQL表字段
问题描述:
我在我的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小时的时间。
答
递归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块。
答
使用此代码:
------------------ 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
答
试试这个没有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
答
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
答
普莱斯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
答
这是用数字表格/函数最好解决的那些问题之一。与递归或循环相比,代码少得多,通常对于任何不平凡且更可重用的更快的代码。
你想要的核心代码是
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的最终代码中派生出来的一个 - 根据我的经验,它比实际的表更快,并且占用更少的空间。
我的不好。我使用MS SQL-Server –
从...开始? –
在任何时候您将添加的记录数量是否有上限和下限?你也有一些你试过的代码吗? –