I have a client that takes a backup everytime they run payroll. as a result in their Microsoft SQL 2008 server they get a new database every time it is run.
ie original db is payroll backup db is payroll.20200421
every quarter I have to go in, detach the database, and archive the db files to another drive.
I have gotten to the point where I can save a list of the databases that need to be archived in a table, but cannot figure out how to put that into the sp_detach_db function.
the original script is here.
DECLARE @CutOffDate DateTime
SELECT @CutOffDate = DATEADD(MONTH, 3, GETDATE()) --set list to anything older than 3 months old
SELECT name, database_id, create_date
INTO TempDatabaseList --create temporary table
FROM sys.databases
WHERE sys.databases.create_date < @CutOffDate -- anything created older than cutoff date
AND RIGHT(name,6) not like '%[^0-9]%' --and the last 6 characters in name are...< @CutOffDate -- anything created older than cutoff date