sql statement attach detach database

When attaching a database using the sp_attach_db system stored procedure --
Tag: When attaching a database using the sp_attach_db system stored procedure
//Attach a database
sp_attach_db
When attaching a database using the sp_attach_db system stored procedure.

sp_attach_db: Attach the database to the server.
Syntax
sp_attach_db [ @dbname = ] 'dbname' , [ @filename1 = ] 'filename_n' [ ,...16 ]

Parameters
[@dbname =] 'dbname'
The name of the database to attach to the server. The name must be unique. The data type of dbname is sysname, and the default value is NULL.

[@filename1 =] 'filename_n'

The physical name of the database file, including the path. The data type of filename_n is nvarchar(260), and the default value is NULL. Up to 16 file names can be specified.
Parameter names start with @filename1 and increment to @filename16. The list of filenames must at least include the main file, which contains system tables that point to other files in the database.
The list must also include all files that were moved after the database was detached.

Return code value: 0 (success) or 1 (failure)


eg: The following example appends two files from pubs to the current server.

EXEC sp_attach_db @dbname = N'pubs',
@filename1 = N'c:\Program Files\Microsoft SQL Server\MSSQL\Data\pubs.mdf',
@filename2 = N'c:\Program Files\Microsoft SQL Server\MSSQL\Data\pubs_log.ldf'

EXEC sp_attach_db @ dbname = N'Ty20051029101451aaa',
@filename1 = N'd:\5screen\Ty20051029101451aaa_data.mdf',
@filename2 = N'd:\5screen\Ty20051029101451aaa_log.ldf'
---solved the problem

//delete database
DROP DATABASE
Delete one or more databases from Microsoft® SQL Server®. Deleting a database deletes the database files and disk files used by the database.

Syntax: DROP DATABASE database_name [ ,...n ]

Parameters: database_name Specifies the name of the database to be dropped. Execute sp_helpdb from the master database to see a list of databases.
eg: exec sp_helpdb database_name

exec Drpo DataBase [Ty20051029101451aaa]

//Separate database
The database can be detached using the sp_detach_db stored procedure and then reattached using CREATE DATABASE with the FOR ATTACH clause.
sp_detach_db Archive
GO
CREATE DATABASE Archive
ON PRIMARY (FILENAME = 'c:\program files\microsoft sqlserver\mssql\data\archdat1.mdf')
FOR ATTACH
GO

//Display current database information
--select * from Master..sysDatabases

//
CREATE DATABASE TestOA ALTER
DATABASE TestOA SET OFFLINE WITH ROLLBACK IMMEDIATE
RESTORE DATABASE TestOA From disk='C:\Documents and Settings\Administrator\Desktop\Account Set\data\Template.Dat'
ALTER DATABASE TestOA SET OFFLINE WITH ROLLBACK IMMEDIATE

exec sp_detach_db Km20051030011601 -- Detach the database
exec sp_attach_single_file_db km20051030011601,'D:\TestDatabase\Km20051030011601.mdf'--only attach the .mdf file

Follow popular foreign website
facebook:http://www.fb-on.com
facebook official website:http://www.facebookzh.com
facebook: http://www.cn-face-book.com
youtube: http://www.youtubezh.com
twitter: http://www.twitterzh.com

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326395423&siteId=291194637