Transact-SQL example-example of backing up a database using a script

Source: Internet
Author: User
Tags sql 2008

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.