[Practical] SQL Server backs up database Stored Procedure scripts and supports complete and differential backup

Source: Internet
Author: User

SQL Server backs up database Stored Procedure scripts and supports complete and differential backup

 1   --  ========================================================== =====  2   --  Author: Shu mu  3   --  Create Date: 2011-02-15  4   --  Description: SQL Server backup database  5   -- ========================================================== =====  6   Alter   Procedure  Usp_sys_backupdb  7     @ Dbname   Varchar ( 50 ) =  Null , --  Database to be backed up  8     @ Backuptype  Varchar ( 50 ) =  '  Full  '   --  Full full backup diff differential backup  9   As  10   Begin  11       12       --  Declare @ dbname varchar (50) 13       --  Declare @ backuptype varchar (50)  14       15       --  Set @ dbname = db_name ()  16       --  Set @ backuptype = 'full'  17       18       Declare   @ Filepath   Varchar ( 100 )--  Backup storage path  19       Declare   @ Filename   Varchar ( 100 ) --  Full name of the backup file  20   21       Set   @ Dbname  =   Isnull ( @ Dbname ,Db_name  ())  22       Set   @ Filepath  =  '  D: \ data_bak \ full  '  23       24       If   @ Backuptype  =  '  Diff  '  25      Begin  26           Set   @ Filepath  =  Replace ( @ Filepath , '  Full  ' , '  Diff  '  )  27       End  28      29       Set   @ Filename  =  @ Filepath  +  '  \  '  30                    +  @ Dbname  31                    +  Convert ( Varchar ( 100 ),Getdate (), 112  )  32                    +  '  _  '  33                    +  @ Backuptype      34   35       --  Full backup  36       If  @ Backuptype  =  '  Full  '  37       Begin  38           Set   @ Filename  =  @ Filename  39                         +  '  _  '  40                        +  Ltrim ( Str ( Datepart (Wk, Getdate ()) %  2  ))  41                         +  Str ( Rand () *  10000 , 5 )  42                         +  '  . Bak  '  43           Backup   Database   @ Dbname   To   Disk   =   @ Filename   With Init, retaindays =  6 44       End  45   46       --  Differential backup  47       If   @ Backuptype  =  '  Diff  '  48       Begin  49           Set   @ Filename =  @ Filename  50                         +  '  _  '                      51                         +  Ltrim ( Str ( Datepart (DW, Getdate ()) -  1  )) 52                         +  Str ( Rand () *  10000 , 5  )  53                         +  '  . Bak  '  54           Backup   Database  @ Dbname   To   Disk   =   @ Filename   With Init, differential, retaindays =  6  55       End      56   End 

 

Related Article

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.