Only one method is used here. Back up the database file to the disk and restore it.
Only one method is used here. Back up the database file to the disk and restore it.
Eg:
The Code is as follows:
/*
Back up a database using SQL statements
*/
Backup database mydb
To disk = 'C: \ DBBACK \ mydb. Bak'
-- 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.
/*
Restore a database using SQL statements
*/
USE master
Restore database mydb
From disk = 'C: \ DBBACK \ mydb. Bak'
WITH REPLACE
Note: in many cases, the data cannot be restored directly because it is not exclusively opened. The following process may be used.
The Code is as follows:
-- Kill the connection to a database
Create proc KillSpid (@ DBName varchar)
AS
BEGIN
DECLARE @ SQL varchar
DECLARE @ SPID int
SET @ SQL = 'Clare CurrentID CURSOR
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
It is best to use a single user to operate the database after kill the user
The Code is as follows:
SP_DBOPTION @ DBName, 'single user', 'true'