Insert multiple dates for the same id - SQL Server 2014

0

I need to enter a full month for each "ID" field in order to verify if a hotel room is available on that date. It would be something like this:

And so on for each of the 500 rooms of a hotel. I'm using SQLServer Express 2014.

    
asked by Pablo Matias 21.06.2017 в 01:01
source

2 answers

1
DECLARE @habId INT = 1, @contador INT = 0, @fechaInital as datetime;
WHILE @habId < 5
BEGIN

    SET @fechaInital = '20170601'
    WHILE @contador < 30
    BEGIN
       print '' + convert(nvarchar(2), @habId) +', ' + convert(nvarchar(10), @fechaInital)

       SET @fechaInital = DATEADD(day, 1, @fechaInital)
       SET @contador = @contador + 1;
    END;

    SET @habId = @habId + 1;
    SET @contador = 0
END;

Resulting image:

This function will print X date, from June 1, until 30 days later. In your case, instead of printing, you should do a INSERT and I guess you have to give your own format, etc.

You can use two nested for loops (one that first has 500 rooms and one with 30 days) and that's it.

    
answered by 21.06.2017 / 01:54
source
0

You need to make a table with the following structure

Id int, // Id de la nueva tabla
Habitacion_Id int,  // Foranea con tu tabla, Id de la fecha en la que quieres poner muchas fechas
Fecha datetime // La fecha que quieres colocar

If you want to put more information modify that table

    
answered by 21.06.2017 в 01:17