In the process of general database development and maintenance, it is often necessary to backup the database, the most basic way is to use the Database Backup Wizard provided by the SSMs graphical interface to back up the step-by-step operation, although it is simple and fast but the days will feel repetitive and cumbersome. Here is an easy-to-use SQL script backup method, or a slightly improved ability to be a scheduled backup of the database within a scheduled task.
In the example below, parameter @enable_compression is a feature that sql2008 Enterprise Edition provides, which compresses backup files backed up by the database.
Declare @database_name nvarchar( -)Declare @backup_folder nvarchar( -)Declare @enable_compression bitSet @database_name =N'Yourdbname'Set @backup_folder =N'F:\DbBackups\'Set @enable_compression = 1 --backup file compression functionality provided by SQL 2008 Enterprise EditionDeclare @backup_sql nvarchar(4000)Set @backup_sql =N'BACKUP DATABASE [' + @database_name + '] to disk =' +N'N" " + @backup_folder + @database_name +N'_full_' + Replace(Replace(Replace(Convert(nvarchar( +),getdate(), -),'-',"'),' ','_'),':',"')+N'.Bak"'With Format,init'if @enable_compression = 1 Set @backup_sql = @backup_sql +N', Compression'execDbo.sp_executesql@backup_sql
This article has ended, because the content of a single solid relatively short.