How do I import a sql data file into SQL Server?

后端 未结 7 1785
北海茫月
北海茫月 2020-12-08 03:41

I have a .sql file and I am trying to import it into SQL Server 2008. What is the proper way to do this?

相关标签:
7条回答
  • 2020-12-08 04:14

    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.

    0 讨论(0)
  • 2020-12-08 04:16

    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

    0 讨论(0)
  • 2020-12-08 04:17

    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.

    0 讨论(0)
  • 2020-12-08 04:21

    In order to import your .sql try the following steps

    1. Start SQL Server Management Studio
    2. Connect to your Database
    3. Open the Query Editor
    4. Drag and Drop your .sql File into the editor
    5. Execute the import
    0 讨论(0)
  • 2020-12-08 04:24
    1. Start SQL Server Management Studio
    2. Connect to your database
    3. File > Open > File and pick your file
    4. Execute it
    0 讨论(0)
  • 2020-12-08 04:30

    If 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

    0 讨论(0)
提交回复
热议问题