Advertisement

How to Attach and DeAttach Database in Sql Server |SSMS|

How to Attach and DeAttach Database in Sql Server |SSMS| Attach a Database
Prerequisites
The database must first be detached. Attempting to attach a database that has not been detached will return an error. For more information, see Detach a Database.
When you attach a database, all data files (MDF and LDF files) must be available. If any data file has a different path from when the database was first created or last attached, you must specify the current path of the file.
When you attach a database, if MDF and LDF files are located in different directories and one of the paths includes \\?\GlobalRoot, the operation will fail.
Is Attach the best choice?
We recommend that you move databases by using the ALTER DATABASE planned relocation procedure instead of using detach and attach, when moving database files within the same instance. For more information, see Move User Databases.

We don't recommend using detach and attach for Backup and Recovery. There are no transaction log backups, and it's possible to accidently delete files.
Security
File access permissions are set during a number of database operations, including detaching or attaching a database. For information about file permissions that are set whenever a database is detached and attached, see Securing Data and Log Files from SQL Server 2008 R2 Books Online (Still a valid read!)
We recommend that you do not attach or restore databases from unknown or untrusted sources. Such databases could contain malicious code that might execute unintended Transact-SQL code or cause errors by modifying the schema or the physical database structure. Before you use a database from an unknown or untrusted source, run DBCC CHECKDB on the database on a nonproduction server and also examine the code, such as stored procedures or other user-defined code, in the database. For more information about attaching databases and information about changes that are made to metadata when you attach a database, see Database Detach and Attach (SQL Server).

Detach a Database
This topic describes how to detach a database in SQL Server 2019 (15.x) by using SQL Server Management Studio or Transact-SQL. The detached files.
remain and can be reattached by using CREATE DATABASE with the FOR ATTACH or FOR ATTACH_REBUILD_LOG option. The files can be moved to another server and attached there.

To detach a database
In SQL Server Management Studio Object Explorer, connect to the instance of the SQL Server Database Engine and then expand the instance.

Expand Databases, and select the name of the user database you want to detach.

Right-click the database name, point to Tasks, and then click Detach. The Detach Database dialog box appears.

Databases to detach
Lists the databases to detach.

Database Name
Displays the name of the database to be detached.

Drop Connections
Disconnect connections to the specified database.

Update Statistics
By default, the detach operation retains any out-of-date optimization statistics when detaching the database; to update the existing optimization statistics, click this check box.

Keep Full-Text Catalogs
By default, the detach operation keeps any full-text catalogs that are associated with the database. To remove them, clear the Keep Full-Text Catalogs check box. This option appears only when you are upgrading a database from SQL Server 2005 (9.x).

Status
Displays one of the following states: Ready or Not ready.

Message
The Message column may display information about the database, as follows:

When a database is involved with replication, the Status is Not ready and the Message column displays Database replicated.

When a database has one or more active connections, the Status is Not ready and the Message column displays number_of_active_connections Active connection(s) - for example: 1 Active connection(s). Before you can detach the database, you need to disconnect any active connections by selecting Drop Connections.

To obtain more information about a message, click the hyperlinked text to open Activity Monitor.

When you are ready to detach the database, click OK.

SSMS,SqlServer Management studio,Attachment and Deattachment database file in SSMS,Attachment of database,Deattachment of database,Sql Server Attachment,Sql Server Deattachment,

Post a Comment

0 Comments