O'Reilly logo

SQL Server 2012 with PowerShell V3 Cookbook by Donabel Santos

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Attaching a database

In this recipe, we will programmatically attach a database with a primary data file (.mdf), log file (.ldf), and multiple secondary data files (.ndf).

Getting ready

Before we can attach a database, we must have the data files and optional log files attached. If you have not completed the Detaching a database recipe, perform the following steps:

  1. When we attach the database, we will set QUERYWORKS\jraynor as the owner. This principal has been created with our development VM. Feel free to replace the appropriate code with a login available with your system.
  2. We will create a database called TestDB. Open up SQL Server Management Studio and run the following code:
    IF DB_ID('TestDB') IS NOT NULL DROP DATABASE TestDB GO CREATE DATABASE ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required