Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

sql server - Add date to SQL database backup filename

I'm using the below to backup a db from a SQL job. Can someone tell me how to add the current date to the output filename? Preferably in YYYYMMDD format.

BACKUP DATABASE [myDB] TO? DISK = N'\myPathmyDB.bak' WITH NOFORMAT, INIT,? NAME = N'myDB', SKIP, REWIND, NOUNLOAD,? STATS = 10
GO

Thanks!

question from:https://stackoverflow.com/questions/2410674/add-date-to-sql-database-backup-filename

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
DECLARE @MyFileName varchar(1000)

SELECT @MyFileName = (SELECT '\ServerToSavePathMyDB_' + convert(varchar(500),GetDate(),112) + '.bak') 

BACKUP DATABASE [myDB] TO DISK=@MyFileName ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...