Importing into sql server management studio

Pamela Whittaker 1 Reputation point
2021-01-26T16:09:18.75+00:00

I can see hw to create a files of sql commands from a database table but how do import it into another test database please. Do I use import flat file as taht appears to be for csv files. Thanks

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,128 questions
{count} votes

8 answers

Sort by: Most helpful
  1. Yitzhak Khabinsky 26,201 Reputation points
    2021-01-26T16:21:04.81+00:00

    You can use BULK INSERT for that.

    For example:
    MLTC.csv file content:

    "Employee","Email","FirstName","LastName"
    "7777777","user@tests.com","Joe","Smith"
    "1234567","user@yahoo.com","Simon","Monte"
    "5555555","user@gmail.com","Ben","Conners"
    

    SQL

    USE tempdb;
    GO
    
    DROP TABLE IF EXISTS dbo.tbl;
    
    CREATE TABLE dbo.tbl (EmployeeID VARCHAR(20),Email VARCHAR(20),FirstName VARCHAR(20),LastName VARCHAR(30));
    
    BULK INSERT dbo.tbl
    FROM 'e:\Temp\MLTC.csv'
    WITH (FORMAT='CSV' -- starting from SQL Server 2017 onwards
       , FIRSTROW = 2
       , FIELDQUOTE = '"'
       , FIELDTERMINATOR = ','
       , ROWTERMINATOR = '\n');
    
    SELECT * FROM dbo.tbl;
    

    Just FYI: Azure Data Studio allows to save result of any SELECT statement as a file on the file system in the following formats: Excel, csv, JSON, and XML.

    0 comments No comments

  2. Pamela Whittaker 1 Reputation point
    2021-01-26T17:10:02.923+00:00

    Do you put that in the new sql box? I presume so. What would it be for a .txt files or a .sql file please

    0 comments No comments

  3. Pamela Whittaker 1 Reputation point
    2021-01-26T17:11:16.86+00:00

    The file would contain "insert into" etc

    0 comments No comments

  4. Pamela Whittaker 1 Reputation point
    2021-01-26T17:21:01.13+00:00

    Bult inserts won't work will it on a file of insert statements?

    0 comments No comments

  5. Tom Phillips 17,741 Reputation points
    2021-01-26T19:58:18.13+00:00
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.