SQLSERVER_ATTACH_DB

Applies to: SQLFlex

Attachs an MS SQL mdf to the current server instance. If the passed database is already attached, no error is generated.

The SQLSERVER_ATTACH_DB command is intended for use with our new support of Microsoft LocalDB’s. When using LocalDB’s a common use case might be to attach to a customer DB, do some work on it, and then detach this DB and attach to a different DB. This was possible using stored procedures, but the code had some complexities that a developer need not worry about. When you attach a database, you must pass the database name, along with the path to an MDF file which contains the database. Any associated LDF’s (log files) and NDF’s (secondary data files) must be in the same folder.

If you call SQLSERVER_ATTACH_DB on a database that is already attached, no error will be generated. After you call SQLSERVER_ATTACH_DB, you should call SET_DATABASE_NAME if you wish to make the attached database the currently selected database.

SQLSERVER_ATTACH_DB will work on all supported version of SQL server.

Syntax

SQLSERVER_ATTACH_DB {database} on {filePath}

Parameter

Description

database

Name of the database

filePath

Path of the MDF file

Example

SQLSERVER_ATTACH_DB "MTSample" ON "C:\Mertech\Data\SampleDb.mdf"