Skip to content

Export & Import MS SQL Table

If you want to copy a SQL table from one server to another this is an easy way to do it.

First of all save your desired SELECT query in a CSV file:

sqlcmd -S server -U loginid -P password -d DBname -Q "select * from tablename" -o output.csv

You can also use the SQL Management Studio, execute the query and then right click the upper left corner of the result window to save the result set as a CSV file.

Now generate the CREATE TABLE statement by doing a right click on the desired table, select “Script Table As”, “CREATE To”.

Execute the generated statement on the new database.

Now import the exported CSV file:

BULK INSERT dbo.YourTable
FROM 'C:\yourfile.csv'
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = ';',  --CSV field delimiter
ROWTERMINATOR = '\n',   --Use to shift the control to next row
ERRORFILE = 'C:\Error.csv',
TABLOCK
)

Leave a Reply

Your email address will not be published. Required fields are marked *

By transmitting your comment you agree to our mentioned privacy policy content.

19 − eight =