eg
Copy Code code as follows:
/*
Backing up a database with SQL statements
*/
BACKUP DATABASE MyDB
to DISK = ' C:\DBBACK\mydb. BAK '
--Here specify the path and file name of the database you want to back up, and note that the path's folder must have been created. File names can be marked with dates
/*
Restoring a database from an SQL statement
*/
Use master
RESTORE DATABASE MyDB
From disk= ' C:\DBBACK\mydb. 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
Copy Code code as follows:
--kill a connection that accesses 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
Copy Code code as follows:
sp_dboption @DBName, ' Single user ', ' true '