SQL Server automatically backs up the SP by adding the Database Name and path.

Source: Internet
Author: User
Create procedure sp_backdb @ dbname varchar ( 100 ), @ Path varchar ( 100 )
As


-- Create by Allen 2004 - 11 - 01   In Order to backup database and verify Database

 
Declare @ bakname varchar ( 100 )
-- Declare @ dbname varchar ( 100 )
Declare @ SQL varchar ( 8000 )
-- Declare @ sql1 varchar ( 8000 )
-- Declare @ path varchar ( 100 )
-- Set @ Path = ' C :\ '
-- Set @ Dbname = ' Northwind '
Set @ Bakname = @ Dbname + Cast (datepart (weekday, getdate ()) - 1   As Varchar ( 1 ))
-- The backup files from Sunday to Saturday are bak0 to bak6.



Set @ SQL = ' Backup database [ ' + @ Dbname + ' ] To disk = N ''' + @ Path + '' + @ Bakname + ' . Bak '' With init, nounload, noskip, stats = 10, noformat '
-- Print @ SQL

Set @ SQL = @ SQL + ' Declare @ I int
Select @ I = Position from MSDB .. backupset where database_name = ''' + @ Dbname + ''' And type ! = ''' + ' F ''' + '  
And backup_set_id = (Select max (backup_set_id) from MSDB .. backupset where database_name = ''' + @ Dbname + ''' )
Restore verifyonly from disk = N ''' + @ Path + '' + @ Bakname + ' . Bak ''' + ' With File = @ I '
-- Print @ SQL

Exec (@ SQL)

Back up the data to the specified directory and cycle from Sunday to Saturday (0---6)

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.