Free Newsletters:
DatabaseJournal  
DBANews
Search Database Journal:
 
HOME News MS SQL Oracle DB2 Access MySQL PostgreSQL PHP SQL Etc Scripts Links Discussion
internet.com

» HOME
» NEWS
» FEATURES
» SERIES
MS SQL
Oracle
MS Access
MySQL
DB2
» RESOURCES
Products
Scripts
Links
» DISCUSSION
» TECH JOBS

Marketplace Partners
Be a Marketplace Partner




internet.commerce
Be a Commerce Partner
Car Donations
Online Education
Dental Insurance
PDA Phones & Cases
Memory Upgrades
Televisions
Memory
Boat Donations
Cell Phones
Web Hosting Directory
GPS
Hurricane Shutters
Compare Prices
Data Center Solutions




MySpace Joins eBay, Yahoo in Open Profile Push

News Corp. Unit Under Fire for Ties to Hacker

Are Non-PC Devices Hurting 'Net Innovation?

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Linked Data Planet Conference & Expo

CA ERwin® Data Modeler Proven database design and modeling. Efficiently analyze, design and deploy effective database solutions. Whitepaper: Manage SQL Server Deployments
Try it free: CA ERwin® Data Modeler

WHITEPAPER:
The New Information Agenda. Do You Have One?

WHITEPAPER:
The Outsourcing Decision for a Globally Integrated Enterprise--from Commodity Outsourcing to Value Creation

GLOBAL CIO LEADERSHIP SURVEY:
How are other CIOs driving growth?

WHITEPAPER:
How CIOs Can Drive Growth, Business Flexibility and Innovation in a Flex-Pon-Sive* Company


Solaris 8 Migration Assistant
Rapidly move your Solaris 8 application environments to new systems running Solaris 10 with the Solaris 8 Migration Assistant. Reduce migration risk while taking advantage of increased performance, reliability and security of the latest SPARC hardware platforms and Solaris 10 OS. »

 
Sun Eco Innovation: Good for Business, Good for the Environment
A complete solution to help you optimize and refresh your datacenter while properly recycling equipment and eliminating eWaste, including money-saving promotions to lower hardware acquisition costs. »

 
Sun Eco Innovation: Power Calculators
Power consumption has increasingly become a priority in customer's minds when purchasing new systems or storage. Sun's Power Calculators provide data on power consumption of Sun products allowing IT managers to better plan the power requirements in the datacenter to achieve better energy and cost savings. »

 
Optimize the Web Tier: Consolidate to Get More Performance in Less Space and Lower Power Consumption
Expansion in the Web tier is generally accomplished by adding more servers whenever extra capacity is needed. As the pool of servers grows larger, however, the complexity of the environment can grow exponentially. »

Production Manager (hands on)
Aquent
US-MA-Cambridge

Justtechjobs.com Post A Job | Post A Resume
MS SQL
June 25, 2003
Attaching and Detaching Databases on SQL Server
By Alexander Chigrik

Introduction

If you need to move a database, or database file to another server or disk, and you do not want to backup the database, copy the backup on another server, and then re-create the database from the backup, you can use the sp_detach_db and sp_attach_db system stored procedures to detach the database and then attach it again. Detaching and attaching databases can be used when you want to move the database files to a different physical disk from the disk that has run out of disk space and you want to expand the existing file rather than add a new file to the database on another disk.

To move a database using detach and attach, you should make the following steps:

  • Detach the database.
  • Move the database file(s) to the desired location on another server or disk.
  • Attach the database specifying the new location of the moved file(s).

After detaching, the database will be removed from SQL Server but will be intact within the data and transaction log files that compose the database. You can use these data and transaction log files to attach the database to any instance of SQL Server, including the server from which the database was detached. After attaching, the database will be available in exactly the same state it was in when it was detached.

Detaching a Database

To detach an SQL Server 2000 database, you can use the sp_detach_db system stored procedure. This stored procedure can also run UPDATE STATISTICS on all tables before detaching. The syntax for sp_detach_db system stored procedure is as follows:

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

In the above, the parameters to the stored procedure are as follows:

  • [@dbname =] 'dbname' is the database name. 'dbname'is nvarchar(128), a default value is NULL.
  • [@skipchecks =] 'skipchecks' The 'skipchecks' parameter indicates will be can UPDATE STATISTICS run or skipped. The 'skipchecks' is nvarchar(10), a default value is NULL. If 'skipchecks' is true, UPDATE STATISTICS is skipped. If 'skipchecks' is false, UPDATE STATISTICS is run.

The following example detaches the pubs database and runs UPDATE STATISTICS on all tables before detaching:

EXEC sp_detach_db 'pubs', 'false'

Attaching a Database

When you attach a database, you must specify at least the name and physical location of the primary data file. If one or more of the database files have changed location since the database was detached, you must specify the name and physical location of these files in addition to the primary file.

To attach SQL Server 2000 database, you can use the sp_attach_db system stored procedure. The syntax for sp_attach_db system stored procedure is as follows:

sp_attach_db [ @dbname = ] 'dbname',
             [ @filename1 = ] 'filename_n' [ ,...16 ] 

In the above command:

  • [@dbname =] 'dbname' is the database name. dbname is nvarchar(128), a default value is NULL.
  • [@filename1 =] 'filename_n' is the database file name. filename_n is nvarchar(260), a default value is NULL. There can be up to 16 file names specified.

This is the example to attach the pubs database which contain two files pubs.mdf and pubs_log.ldf from the C:\MSSQL\Data directory:

EXEC sp_attach_db @dbname = 'pubs', 
     @filename1 = 'C:\MSSQL\Data\pubs.mdf', 
     @filename2 = 'C:\MSSQL\Data\pubs_log.ldf'

Attaching a Single-File Database

A single-file database is a database that has only one data file. When a database comprises only one data file , the database can be attached to an instance of SQL Server 2000 without using the transaction log file. When the data file will be attached, SQL Server will create a new transaction log file automatically.

To attach a single-file database, you can use the sp_attach_single_file_db system stored procedure. The syntax for sp_attach_single_file_db system stored procedure is as follows:

sp_attach_single_file_db [ @dbname = ] 'dbname' 
    , [ @physname = ] 'physical_name'
  • [@dbname =] 'dbname' is the database name. 'dbname' is nvarchar(128), a default value is NULL.
  • [@physname =] 'phsyical_name' is the database file name. 'phsyical_name' is nvarchar(260), a default value is NULL.

This is the example to attach only one data file of the pubs database from the C:\MSSQL\Data directory:

EXEC sp_attach_single_file_db @dbname = 'pubs',
     @physname = 'C:\MSSQL\Data\pubs.mdf'

Troubleshooting Lost Users

When you move a database to another server and there are existing users in the detached database, you can lose users after attaching the database on the new server. For example, if you move the Sales database from the Product server to the Test server (for the test purposes) and the user Alex exists in the Sales database, you should manually link the relationship between the Alex user and the appropriate login on the Test server.

You can use the sp_change_users_login system stored procedure to link the specified user in the current database to the appropriate login. The following example links the user Alex in the current database to the Alex login:

EXEC sp_change_users_login 'Update_One', 'Alex', 'Alex'

See SQL Server Books Online to get more information about the sp_change_users_login stored procedure.

» See All Articles by Columnist Alexander Chigrik

Tools:
Add databasejournal.com to your favorites
Add databasejournal.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

MS SQL Archives

IT in 2018: Download Free eBook By The Author Of "Does IT Matter?" Simple Registration Is Required.
Data Sheet: IBM Information Server Blade
Download: SQL Backup & DBA Best Practices eBook
HP eBook: Using Business Service Management (BSM) to Manage Your Business Applications
Webcast: Five Virtualization Trends to Watch. Produced for HP, Citrix, and Intel.


Latest Forum Threads
MS SQL Forum
Topic By Replies Updated
Mssql Equivalent Of Mysql "merge" Storage Engine dbnewbie 1 May 13th, 10:42 AM
How To Transfer Access Data Records To SQL ?? ankurdjariwala 1 May 8th, 12:24 PM
problem with federated server linking majidkhan 1 April 29th, 10:00 AM
"SELECT rowguidcol" from tables on linked servers? brentbordelon 1 April 25th, 04:12 PM







JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES