SQL Server backup and Restoration

Source: Internet
Author: User

1 :/*

2. Back up a database using SQL statements
   3: */
   4: BACKUP DATABASE mydb
   5: TO DISK ='C:\DBBACK\mydb.BAK' 
6: -- specify the path and file name of the database to be backed up. Note: The Path folder must have been created. The file name can be identified by date.
   7:  
   8: /*
9: restore a database using SQL statements
  10: */
  11: USE master
  12: RESTORE DATABASE mydb
  13: FROM DISK='C:\DBBACK\mydb.BAK'
  14: WITH REPLACE

Note: in many cases, the data cannot be restored directly because it is not exclusively opened. The following process may be used.

1: -- kill the connection to a database

   2: CREATE PROC KillSpid(@DBName varchar)

 

   3: AS

 

   4: BEGIN

 

   5:     DECLARE @SQL varchar

 

   6:     DECLARE @SPID int

 

   7:     SET @SQL='DECLARE CurrentID CURSOR FOR 

 

   8:     SELECT spid FROM sysprocesses WHERE dbid=db_id('''+@DBName+''') '

 

   9:     FETCH NEXT FROM CurrentID INTO @SPID

 

  10:     WHILE @@FETCH_STATUS <>-1

 

  11:     BEGIN

 

  12:         exec('KILL '+@SPID)

 

  13:         FETCH NEXT FROM  CurrentID INTO @SPID

 

  14:     END

 

  15:     CLOSE CurrentID

 

  16:     DEALLOCATE CurrentID

 

  17: END

 

It is best to use a single user to operate the database after kill the user

 

SP_DBOPTION @DBName,'single user','true'

 

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.