Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paste MS Excel data to SQL Server

I have a bunch of rows in Excel that I want to paste into a new table in MS SQL. Is there a simple way ?

like image 276
Bajji Avatar asked Nov 25 '08 10:11

Bajji


People also ask

Can you copy and paste into SQL?

In the SQL pane, copy the portion of the query you want to copy. Create a new query or open the query where you want to paste the copied SQL. Position the cursor where you want to add the SQL code. Right-click and from the shortcut menu click Paste.

How do you bulk insert Excel file in SQL Server?

Right-click on the database that contains your tables and clicks on > Tasks > Import Data . Select Data source as Microsoft Excel and specify the path of the Excel file. Choose the Destination as SQL Server then select the database. Click Finish to finish the wizard and wait until the wizard completed successfully.


2 Answers

If you have SQL Server Management Studio, you can just Copy from Excel and Paste into the table in Management Studio, using your mouse. Just

  1. Go to the table you want to paste into.
  2. Select "Edit Top 200 Rows".
  3. Right-click anywhere and select Paste.

Before you do this, you must match the columns between Excel and Management Studio. Also, you must place any non-editable columns last (right-most) using the Table Designer in Management Studio.

The whole procedure takes seconds (to set-up and start - not necessarily to execute) and doesn't require any SQL statements.

Regarding empty database tables and SSMS v18.1+.

like image 190
bzlm Avatar answered Nov 03 '22 02:11

bzlm


I have used this technique successfully in the past:

Using Excel to generate Inserts for SQL Server

(...) Skip a column (or use it for notes) and then type something like the following formula in it:

="insert into tblyourtablename (yourkeyID_pk, intmine, strval) values ("&A4&", "&B4&", N'"&C4&"')"

Now you’ve got your insert statement for a table with your primary key (PK), an integer and a unicode string. (...)

like image 27
Galwegian Avatar answered Nov 03 '22 03:11

Galwegian