I have a "copy" stored procedure for a database. During the copy, I backup the database with something like:
exec('backup database [' + @source + '] ' +
'to disk = N''' + @backupdir + '\copybackup'' ' +
'with noformat, noinit, name=N''copybackup-full'', ' +
'SKIP, NOREWIND, NOUNLOAD, STATS = 10;');
And then create and empty database, and restore to it:
exec('create database [' + @dest + '];');
exec('restore database [' + @dest + '] ' +
'from disk = N''' + @backupdir + '\copybackup'' ' +
'with file = 1, ' +
'move N''' + @source + ''' to N''' + @dbdir + '\' + @dest +
'.mdf'', ' +
'move N''' + @source + '_log'' to N''' + @dbdir + '\' + @dest +
'_log.ldf'', ' +
'NOUNLOAD, REPLACE, STATS = 10;');
So all is well and good. Except, now I'm left with a file at @backupdir\copybackup that I really don't want. How do I get rid of it?
Since this is a stored procedure, I really don't want to have to wrap it all in a batch file or some other hokey thing on the server itself. I'd like to take care of it from T-SQL right here when I create the mess in the first place. I've grepped through the MS Docs, but no joy. (SQL Server 2005, please)
Ideas?