I have to insert a specific number of rows into a SQL Server table.
DECLARE @val AS INT = 20,
@val2 AS VARCHAR(50),
@Date AS DATETIME = CONVERT(DATETIME,'02-05-2016'),
@i AS INT = 0
SET @val2 = 'abc'
DECLARE @tbl TABLE
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[val2] VARCHAR(50) NULL,
[datum] [datetime] NULL
)
--INSERT INTO @tbl
SELECT @val2, DATEADD(DAY, @i, @Date)
UNION ALL
SELECT @val2, DATEADD(DAY, @i, @Date)
In this query, I have to insert dates starting from a given date till the number of value assigned to the variable '@val'. So, in this case, 20 rows need to be inserted into the table starting from '02-05-2016' and then date increasing 1 day for each row.
How can I do it in a single statement without any looping or multiple insert statements?