sp_detach_db

Transact-SQL Reference

Transact-SQL Reference

sp_detach_db

Detaches a database from a server and, optionally, runs UPDATE STATISTICS on all tables before detaching.

Syntax

sp_detach_db [ @dbname = ] 'dbname'
    
[ , [ @skipchecks = ] 'skipchecks' ]

Arguments

[@dbname =] 'dbname'

Is the name of the database to be detached. dbname is sysname, with a default value of NULL.

[@skipchecks =] 'skipchecks'

skipchecks is nvarchar(10), with a default value of NULL. If true, UPDATE STATISTICS is skipped. If false, UPDATE STATISTICS is run. This option is useful for databases that are to be moved to read-only media.

Return Code Values

0 (success) or 1 (failure)

Result Sets

None

Remarks

The detached files remain and can be reattached using sp_attach_db or sp_attach_single_file_db. The files can also be moved to another server and attached.

Permissions

Only members of the sysadmin fixed server role can execute sp_detach_db.

Examples

This example detaches the pubs database with skipchecks set to true.

EXEC sp_detach_db 'pubs', 'true'

See Also

sp_attach_db

sp_attach_single_file_db