I have a .sql file and I am trying to import it into SQL Server 2008. What is the proper way to do this?
-
What do you mean by import? Do you want to run the queries in the .sql file against a database?Barry Jordan– Barry Jordan2011-10-19 21:03:04 +00:00Commented Oct 19, 2011 at 21:03
-
it has a bunch of table that I want to use in a web siteAmen Ra– Amen Ra2011-10-19 21:38:53 +00:00Commented Oct 19, 2011 at 21:38
8 Answers
If your file is a large file, 50MB+, then I recommend you use sqlcmd, the command line utility that comes bundled with SQL Server. It is easy to use and it handles large files well. I tried it yesterday with a 22GB file using the following command:
sqlcmd -S SERVERNAME\INSTANCE_NAME -i C:\path\mysqlfile.sql -o C:\path\output_file.txt
The command above assumes that your server name is SERVERNAME, that you SQL Server installation uses the instance name INSTANCE_NAME, and that windows auth is the default auth method. After execution output.txt will contain something like the following:
...
(1 rows affected)
Processed 100 total records
(1 rows affected)
Processed 200 total records
(1 rows affected)
Processed 300 total records
...
use readfileonline.com if you need to see the contents of huge files.
UPDATE
This link provides more command line options and details such as username and password:
https://dba.stackexchange.com/questions/44101/importing-sql-server-database-from-a-sql-file
3 Comments
-d like so: sqlcmd -S SERVERNAME -d INSTANCE_NAME -i C:\path\mysqlfile.sql -o C:\path\output_file.txtIf you are talking about an actual database (an mdf file) you would Attach it
.sql files are typically run using SQL Server Management Studio. They are basically saved SQL statements, so could be anything. You don't "import" them. More precisely, you "execute" them. Even though the script may indeed insert data.
Also, to expand on Jamie F's answer, don't run a SQL file against your database unless you know what it is doing. SQL scripts can be as dangerous as unchecked exe's
Comments
- Start SQL Server Management Studio
- Connect to your database
- File > Open > File and pick your file
- Execute it
2 Comments
In order to import your .sql try the following steps
- Start SQL Server Management Studio
- Connect to your Database
- Open the Query Editor
- Drag and Drop your .sql File into the editor
- Execute the import
2 Comments
.sql file in the query editor. I did that in my data base, and I got all my tables (like 10 of them). Just be sure your are not dropping something you already have and you may want to keep.Try this process -
Open the Query Analyzer
Start --> Programs --> MS SQL Server --> Query Analyzer
Once opened, connect to the database that you are wish running the script on.
Next, open the SQL file using File --> Open option. Select .sql file.
Once it is open, you can execute the file by pressing F5.
Comments
A .sql file is a set of commands that can be executed against the SQL server.
Sometimes the .sql file will specify the database, other times you may need to specify this.
You should talk to your DBA or whoever is responsible for maintaining your databases. They will probably want to give the file a quick look. .sql files can do a lot of harm, even inadvertantly.
See the other answers if you want to plunge ahead.

