I have a problem where I need to copy data from one database into another, I am never guaranteed to have the data available in realtime, and sometimes I might need to process 5 days' of data in one go etc. The processing day should operate over a period from 7am one day to 6:59am the next day, for example, between 2017-08-01 07:00 and 2017-08-02 06:59.
I would like to use SQL but without any looping constructs is possible, but I am do not know how to convert the psuedo code below into the required SQL statements, thus any help would be appreciated:
declare @lastDate datetime
set @lastDate = (select max(dc) from dmt)
do
if gettime() => 07:00 then
if @lastDate < getdate() then
insert id, dc into dmt where dc between @lastdate and @lastdate + 1 day from othertable
end if
end if
set @lastDate = (select max(dc) from dmt)
while @lastDate < getdate()
I am using SQL Express 2012. The id is an int and dc is a datetime. dmt is the table where I want to store the copied information and othertable is where I want to copy from.
Thanks.