Link to home
Start Free TrialLog in
Avatar of GordonPrince
GordonPrinceFlag for United States of America

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.
Avatar of JestersGrind
JestersGrind
Flag of United States of America image

Probably the only way to do it is via dynamic SQL.  Build the string and then execute it.

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)
ASKER CERTIFIED SOLUTION
Avatar of JestersGrind
JestersGrind
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial