Additional database sp_attach_db
Attach the database to the server.
Syntax
Sp_attach_db[@ Dbname =]'Dbname'
,[@ Filename1 =]'Filename_n'[,...16]
Parameters
[@ Dbname =]'Dbname'
Name of the database to be appended to the server. The name must be unique.DbnameThe data type of isSysnameThe default value is null.
[@ Filename1 =]'Filename_n'
The physical name of the database file, including the path.Filename_nThe data type of isNvarchar (260)The default value is null. A maximum of 16 file names can be specified. The parameter name is@ Filename1Starts and increments@ Filename16. The file name list must contain at least the primary file. The primary file contains the system tables pointing to other files in the database. The list must also contain all the files that are moved after the database is detached.
Only explicitSp_detach_dbPerform operations on databases separated from the Database ServerSp_attach_db. If you must specify more than 16 files, use the create database with the for attach clause.
If the server to which the database is appended is not the server from which the database is detached and the detached database is enabled for replication, runSp_removedbreplicationDelete a copy from a database.
Permission
OnlySysAdminAndDbcreatorOnly members with fixed server roles can perform this process.
Example
In the following examplePubsTo 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'
Detaching a database sp_detach_db
Detach the database from the server, and you can choose to run update statistics on all tables before the separation.
Syntax
Sp_detach_db[@ Dbname =]'Dbname'
[,[@ Skipchecks =]'Skipchecks']
Parameters
[@ Dbname =]'Dbname'
Name of the database to be detached.DbnameThe data type of isSysnameThe default value is null.
[@ Skipchecks =]'Skipchecks'
SkipchecksThe data type of isNvarchar (10)The default value is null. IfTrue, The update statistics is skipped. IfFalseTo run update statistics. This option is useful for databases to be moved to read-only media.
Note
UseSp_attach_dbOrSp_attach_single_file_dbRetain and re-append the detached file. Files can also be moved and appended to other servers.
Permission
OnlySysAdminMembers with fixed server roles can only executeSp_detach_db.
Example
The following example separatesPubsDatabase, andSkipchecksSetTrue.
Exec sp_detach_db 'pubs', 'true'