Our agency has more than 200 fire trucks and 50 ambulances. Each of these apparatus has a gps on board that registers data into a SQL Server 2012 database. Each month it is millions of records of data. Each month the name of the table changes. For example; This month (16 December 2015), the name of the table is GPS.dbo.History201512
and it will hold gps data only for the month of December in 2015. Next month (January 2016), the name shall be GPS.dbo.History201601
and it will maintain data for january 2016. So the pattern is GPS.dbo.HistoryYYYYMM
.
A simple query would be:
SELECT DateTimeStamp, Longitude, Latitude, Apparatus
FROM GPS.dbo.History201512
WHERE Apparatus = 'Ambulance32'
So this is a moving target. The table changes names on a predictable pattern.
What can I do so I can program this query to query the current month. If today's date is 4 July 2016 then query the table GPS.dbo.History201607
. If today's date is 9 November 2017 then query the table GPS.dbo.History201711
.
How do you program the query such that it picks up on the system calendar date and then queries the appropriate monthly table?