Create a record for each field stored in a temporary table

0

I have the following query where in a temporary table I keep every Monday between a range of dates.

DECLARE @TEMP TABLE(REGISTRO DATE)
DECLARE @startdate datetime = '2017-01-19'
DECLARE @enddate datetime = '2017-02-19' ;with cte(col)
AS
(
SELECT @startdate
UNION ALL
SELECT col + 1
FROM cte
WHERE col <= @enddate
)
INSERT INTO @TEMP
SELECT *
FROM cte
WHERE DATEDIFF(dd,2,col)% 7 = 0

I get a table with the results:

2017-01-25 
2017-02-01 
2017-02-08
2017-02-15

Now in a real table in my database I need to create a record for each result obtained in this case it would be INSERT for the visit table

INSERT INTO Visita (fecha) VALUES('2017-01-25')
INSERT INTO Visita (fecha) VALUES('2017-02-01')
INSERT INTO Visita (fecha) VALUES('2017-02-08')
INSERT INTO Visita (fecha) VALUES('2017-02-15')
    
asked by Danitza Ayala 19.01.2017 в 23:52
source

1 answer

1

Why not insert the data directly into your Visita table without going through a temporary table?

Modifying your script a bit:

DECLARE @startdate datetime = '2017-01-19'
DECLARE @enddate datetime = '2017-02-19'

;with cte(col)
AS
(
SELECT @startdate
UNION ALL
SELECT col + 1
FROM cte
WHERE col <= @enddate
)
INSERT INTO Visita (fecha) -- directo a la tabla Visita
SELECT *
FROM cte
WHERE DATEDIFF(dd,2,col)% 7 = 0
    
answered by 20.01.2017 в 00:09