Backing up with SQL statements, restoring databases

Source: Internet
Author: User
Keywords Network programming MSSQL tutorials
Tags backup created database files date disk file files names

The only way to do that is to back up the database files to disk and restore them.

eg:




/*


back up the database through SQL statements


*/


BACKUP DATABASE mydb


to DISK = ' C:dbbackmydb.bak '


--Here Specify the path and filename that you want to back up the database, and note that the path's folder must have been created. The file name can be marked with a date


/*


restore the database through SQL statements


*/


Use master


RESTORE DATABASE mydb


from disk= ' C:dbbackmydb.bak '


with REPLACE


 


Note: Most of the time you cannot restore directly because the data is not open exclusively. You might use the following procedure




--kill the connection to access a database


CREATE PROC killspid (@DBName varchar)


as


BEGIN


DECLARE @SQL varchar


DECLARE @SPID int


SET @SQL = ' DECLARE currentid CURSOR for


SELECT spid from sysprocesses WHERE dbid=db_id (' + @DBName + ') '


FETCH NEXT from CurrentID into @SPID


While @ @FETCH_STATUS <>-1


BEGIN


exec (' KILL ' + @SPID)


FETCH NEXT from CurrentID into @SPID


End


Close CurrentID


deallocate CurrentID


End


 


Single user action database is best used when killing users




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.