- 27 Views
- 28/08/2025
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
- SQL Server Installed: Ensure SQL Server is installed and running on your machine or a remote server.
- sqlcmd Installed: The sqlcmd utility is included with SQL Server. Verify it’s accessible by running sqlcmd -? in the command prompt.
- .sql File: Have the large .sql file ready (e.g., database_backup.sql).
- Database Created: Create the target database in SQL Server where the .sql file will be imported.
- Permissions: Ensure you have the necessary permissions to access the SQL Server instance and the target database.
Steps to Import a Large .sql Database
- Open Command Prompt:
- Press Win + R, type cmd, and press Enter to open the Command Prompt.
- 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:
- 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:
Import the .sql File Using sqlcmd:
Run the following sqlcmd command to import the .sql file into the target database:
- -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:
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):
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 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:
Thanks for visit my website
