Insert multiple rows from a date range
Solution 1:
CREATE TABLE [#Booked] (
[BookedId] [bigint] IDENTITY (1, 1) NOT NULL,
[BookedFor] [nvarchar] (50) NOT NULL,
[BookedWhen] [smalldatetime] NOT NULL,
[DeskName] [varchar] (10) NOT NULL
)
Declare
@StartDate smalldatetime = '2022-01-01'
, @EndDate smalldatetime = '2022-01-06'
, @BookedFor nvarchar(50) = 'Mr Smith'
, @DeskName varchar(10) = '3'
;WITH Dates(day) AS
(
SELECT CAST(@StartDate as Date) as day
UNION ALL
SELECT CAST(DATEADD(day, 1, day) as Date) as day
FROM Dates
WHERE CAST(DATEADD(day, 1, day) as Date) < @EndDate
)
INSERT INTO #Booked
SELECT @BookedFor, [day], @DeskName FROM dates;
SELECT * FROM #Booked
Create days between two dates