Search code examples
sqlfor-loopselectstored-proceduressap-ase

Running a `SELECT` for a range of dates to populate a table


I have a (somewhat elaborate) SELECT statement, which extracts for a specific date a subset of rows from a table, based on a specific time stamp. Naively, something like

SELECT id, runTime
  FROM ...
...
 WHERE runTime < @MyTime

where @MyTime is some date-time parameter. I need to run that SELECT for a long range of consecutive dates (from June 1, to November 1 of this year, say), storing the result in a MyOutTable which has already been created, with fields id, runTime, controlDate, where controlDate is the value of the date used to retrieve the id and runTime from my table.

Is there a short way to do this in SQL avoiding using Python? Say have some stored procedure which takes start and end date parameters and generates needed data, populating the output table?

Thank you very much


Solution

  • INSERT INTO MyOutTable ( ControlDate, id, runTime )
    SELECT '2017-10-30' as ControlDate, id, runTime
    FROM ...
    ...
    WHERE runTime < @MyTime
    

    Note there is no VALUES keyword, and the ControlDate value is a constant literal, though you could also use CURDATE() or similar here.