SQL SERVER - Attach a Database with T-SQL - SQL Authority with Pinal Dave
source link: https://blog.sqlauthority.com/2020/12/18/sql-server-attach-a-database-with-t-sql/?utm_campaign=sql-server-attach-a-database-with-t-sql
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
SQL SERVER – Attach a Database with T-SQL
Yesterday one of my clients of Comprehensive Database Performance Health Check reached out to me asking for help in attaching the database files to the server. Let us see today we can attach a database with T-SQL.
There are two different methods to attach the database. Let us see both of them.
Method 1: Create a Database with T-SQL
This is my preferred method and I use it all the time.
USE [master]
GO
CREATE
DATABASE
[AdventureWorks]
ON
( FILENAME = N
'D:\Data\AdventureWorks.mdf'
),
( FILENAME = N
'E:\Log\AdventureWorks_log.ldf'
)
FOR
ATTACH
GO
Method 2: sp_attach_db
This is used to be my preferred method before I started to use Method 1.
USE [master]
GO
EXEC
sp_attach_db @dbname = N
'AdventureWorks'
,
@filename1 =
'D:\Data\AdventureWorks.mdf'
,
@filename2 =
'E:\Log\AdventureWorks_log.ldf'
GO
Well, that’s it. It is pretty simple to attach databases with the T-SQL script. Here is a few associated blog post on the same topic, which you may find useful.
If you have any such questions, you can reach me via Twitter.
Reference: Pinal Dave (https://blog.sqlauthority.com)
Pinal Dave is a SQL Server Performance Tuning Expert and an independent consultant. He has authored 12 SQL Server database books, 35 Pluralsight courses and has written over 5200 articles on the database technology on his blog at a https://blog.sqlauthority.com. Along with 17+ years of hands-on experience, he holds a Masters of Science degree and a number of database certifications.
For any SQL Server Performance Tuning Issue send an email at [email protected] .
Pinal is also a CrossFit Level 1 Trainer (CF-L1) and CrossFit Level 2 Trainer (CF-L2).
Nupur Dave is a social media enthusiast and an independent consultant. She primarily focuses on the database domain, helping clients build short and long term multi-channel campaigns to drive leads for their sales pipeline.
Exclusive Newsletter
Download SQL Server 2019 for FREE
Install SQL Server 2019 in Sixty Seconds
Is your SQL Server running slow and you want to speed it up without sharing server credentials? In my Comprehensive Database Performance Health Check, we can work together remotely and resolve your biggest performance troublemakers in less than 4 hours.
Once you learn my business secrets, you will fix the majority of problems in the future.
Have you ever opened any PowerPoint deck when you face SQL Server Performance Tuning emergencies? SQL Server Performance Tuning Practical Workshop is my MOST popular training with no PowerPoint presentations and 100% practical demonstrations.
Essentially I share my business secrets to optimize SQL Server performance.
© 2006 – 2020 All rights reserved. pinal @ SQLAuthority.com
3 FREE Scripts to Instantly
Improve SQL Server Performance
Email Address *
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK