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 '