Use the ALTER DATABASE command to migrate DATABASE in SQL Server

Technically, you can use the ALTER DATABASE syntax to move any system or user database files that define, initialize, except for the Resource database. To move these data files, you need:

  1. Specify the name and path of the file.
  2. Full path of new data file.

Note that if you do it this way, you can only move one file at a time. ALTER DATABASE's general structure includes:

1. Find the database name and log file with the command:

USE master SELECT name, physical_name FROM sys.master_files WHERE database_id = DB_ID ("Personnel");

2. Set to OFFLINE mode:

ALTER DATABASE Personnel SET offline GO

3. Move the file to a new location using the command:

ALTER DATABASE Personnel MODIFY FILE (NAME = Personnel_Data, FILENAME = "C: DataPersonnel_Data.mdf") GO

4. Repeat the above commands with other data and log files.

5. After finishing, set ONLINE mode with the command:

ALTER DATABASE Personnel SET online GO

And finally, check the database changes:

USE master SELECT name, physical_name FROM sys.master_files WHERE database_id = DB_ID ("Personnel");

Or besides, if you want to move data by full-text path, just declare the new path instead of the new path + the file name as usual. The full steps are as follows:

1. Set OFFLINE mode for database by command:

ALTER DATABASE database_name SET offline GO

2. Move each file one by one:

ALTER DATABASE database_name MODIFY FILE (NAME = logical_name, FILENAME = "new_path". GO

3. Do the same with other catalog files.

4. Set the database to ONLINE with the command:

ALTER DATABASE database_name SET online GO

Some notes to know when using the ALTER DATABASE command, please refer here.Good luck!

4.5 ★ | 2 Vote

May be interested

  • SQL Server setup is always availableSQL Server setup is always available
    database mirroring solution helps build a high-availability database management system in sql server which is quite simple and suitable for medium-sized and lower-level databases.
  • SYSTEM_USER function in SQL ServerSYSTEM_USER function in SQL Server
    the system_user statement returns the username of the current user in the sql server database.
  • What is MS SQL Server?What is MS SQL Server?
    this article will give you a general introduction to sql server, demonstrating the benefits and benefits of using sql server as well as existing versions.
  • Rational DA Data Architecture and DB2 9: Build an SQL commandRational DA Data Architecture and DB2 9: Build an SQL command
    are you familiar with database explorer? whether or not, please read the following article to learn about some of the functions and components of this type of data architecture, more specifically about the ability to build sql commands encountered in the database (database ) yours.
  • GOTO command in SQL ServerGOTO command in SQL Server
    the goto command is a simple jump command, which allows an unconditional jump program from goto to a location in the program that has a label (laber) command in the same function.
  • Clean command in WindowsClean command in Windows
    the clean command deletes all partitions or formats the volume from the current drive. the command applies to windows server (semi-annual channel), windows server 2019, windows server 2016, windows server 2012 r2, windows server 2012.
  • A serious vulnerability on phpMyAdmin allows an attacker to destroy the databaseA serious vulnerability on phpMyAdmin allows an attacker to destroy the database
    a serious security vulnerability on phpmyadmin - one of the most popular mysql database management software will harm the database when the tricked administrator clicks on the link.
  • Network basics: Part 3 - DNS ServerNetwork basics: Part 3 - DNS Server
    a dns server is a server that contains a database of public ip addresses and hostnames associated with them. in most cases, the dns server is used to resolve or translate those common names into ip addresses as required.
  • How to Set Up a Microsoft SQL Server Database in Docker on LinuxHow to Set Up a Microsoft SQL Server Database in Docker on Linux
    traditionally, sql server databases were set up on dedicated servers or virtual machines, but docker has changed all that.
  • Create Database in MongoDBCreate Database in MongoDB
    the use database_name command in mongodb is used to create the database. this command will create a new database, if it does not exist yet, otherwise, this command will return the existing database.