Windows

How to Import Large SQL file Database into MSSQL

To import a large .sql database file into Microsoft SQL Server using the command line (cmd), you can use the sqlcmd utility, which is a command-line tool for SQL Server. Below are the steps to achieve this efficiently:

Prerequisites

  1. SQL Server Installed: Ensure SQL Server is installed and running on your machine or a remote server.
  2. sqlcmd Installed: The sqlcmd utility is included with SQL Server. Verify it’s accessible by running sqlcmd -? in the command prompt.
  3. .sql File: Have the large .sql file ready (e.g., database_backup.sql).
  4. Database Created: Create the target database in SQL Server where the .sql file will be imported.
  5. Permissions: Ensure you have the necessary permissions to access the SQL Server instance and the target database.

Steps to Import a Large .sql Database

  1. Open Command Prompt:
    • Press Win + R, type cmd, and press Enter to open the Command Prompt.
  2. Create the Target Database (if not already created):
    • Use SQL Server Management Studio (SSMS) or sqlcmd to create an empty database. For example, using sqlcmd:
sqlcmd -S <ServerName> -U <Username> -P <Password> -Q "CREATE DATABASE <DatabaseName>"
  • Replace <ServerName> with your SQL Server instance (e.g., localhost or ServerName\InstanceName).
  • Replace <Username> and <Password> with your SQL Server credentials.
  • Replace <DatabaseName> with the name of the database (e.g., MyDatabase).

Navigate to the Directory Containing the .sql File:

  • In the Command Prompt, use the cd command to navigate to the folder where your .sql file is located:

cd C:\Path\To\Your\SQLFile 

Import the .sql File Using sqlcmd:

  • Run the following sqlcmd command to import the .sql file into the target database:

sqlcmd -S <ServerName> -U <Username> -P <Password> -d <DatabaseName> -i database_backup.sql 
  • -S: Specifies the SQL Server instance.
  • -U: Specifies the SQL Server login username.
  • -P: Specifies the password.
  • -d: Specifies the target database.
  • -i: Specifies the input .sql file (e.g., database_backup.sql).

 Example:

sqlcmd -S localhost -U sa -P MyPassword123 -d MyDatabase -i database_backup.sql

Handling Large Files: 

Increase Timeout: For very large .sql files, you may need to increase the timeout to prevent interruptions. Add the -t option to specify the timeout in seconds (e.g., -t 0 for unlimited):

sqlcmd -S <ServerName> -U <Username> -P <Password> -d <DatabaseName> -i database_backup.sql -t 0

Split the File: If the .sql file is too large and causes memory issues, consider splitting it into smaller chunks using a text editor or tools like split (available on Linux or via third-party tools on Windows). Then, import each chunk sequentially:

sqlcmd -S <ServerName> -U <Username> -P <Password> -d <DatabaseName> -i chunk1.sql
sqlcmd -S <ServerName> -U <Username> -P <Password> -d <DatabaseName> -i chunk2.sql 

Verify the Import:

  • After the import completes, connect to the database using SSMS or sqlcmd to verify that the tables, data, and other objects were imported correctly:
 sqlcmd -S <ServerName> -U <Username> -P <Password> -d <DatabaseName> -Q "SELECT * FROM sys.tables"

Thanks for visit my website