HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Importing SQL Server database from a .sql file

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
filesqldatabaseserverimportingfrom

Problem

I'd like to import a database without knowing its name from a .sql file.

In MySql server it is easy because there is tools like heidSQL in which I can import easily my database from a sql file but in SQL Server I can't find the method to do it.

I tried to read the file in notepad but the encoding makes the content unreadable.

When i execute this query RESTORE HEADERONLY FROM DISK = N'C:\Planning\bdd.sql' i have this

  • How can I do it with SQL server?



  • Can SQL Server Management Studio do this task or do I have to install a new tool?

Solution

Create an empty database or if you have one already target that one.

Open CMD with elevated privilege and run:

sqlcmd -S SERVERNAME -d MYDATABASE -U USERNAME -P PASSWORD -i C:\path\mysqlfile.sql -o C:\path\results.txt


basically:

  • -S: is your servername or localhost



  • -d: is the database you are targeting



  • -U: is the username



  • -P: is the password



  • -i: is the path to your .sql file



  • -o: is where the logs file will be saved so if you had problem during the importation you can debug them

Code Snippets

sqlcmd -S SERVERNAME -d MYDATABASE -U USERNAME -P PASSWORD -i C:\path\mysqlfile.sql -o C:\path\results.txt

Context

StackExchange Database Administrators Q#44101, answer score: 9

Revisions (0)

No revisions yet.