GordonPrince
asked on
how to create nightly SQL backup copy-only
Does anyone have an example of a SQL statement I could use to create a nightly backup of one SQL database, make it a copy-only backup, write it to a file with the date/time as the backup filename?
I see the example
BACKUP DATABASE database_name TO <backup_device> … WITH COPY_ONLY …
in the SQL 2005 help. But I don't know how to combine that with automatically putting the date/time in the TSQL.
I see the example
BACKUP DATABASE database_name TO <backup_device> … WITH COPY_ONLY …
in the SQL 2005 help. But I don't know how to combine that with automatically putting the date/time in the TSQL.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
DECLARE @SQL VARCHAR(1000),
@DatabaseName VARCHAR(100)
SET @DatabaseName = 'MyDatabase'
SET @SQL = 'BACKUP DATABASE ' + @DatabaseName + ' TO ' + @DatabaseName + CONVERT(VARCHAR(25), GetDate(), 127) + '.BAK WITH COPY_ONLY'
EXECUTE (@SQL)