Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert Excel data into a SQL insert?

I need to upload several thousand addresses to a web portal. I do not have access to the site itself and must do so with the following INSERT statement:

INSERT INTO Address (AddressTypeID, Address1,City, State, Zip, Phone, PersonName, DisplayName, IsDefault, StatusID, UserID) 
VALUES ('1', 'SQL Herro st', 'SLC', '42', '84087', '8015957258', 'Bruce Wayne', 'Bruces address', 'False', '1', '3019')

I have the addresses in Excel but I don't really have any idea how to iterate this code a thousand times while importing the data.

like image 462
Justin Burgard Avatar asked Jan 31 '17 00:01

Justin Burgard


People also ask

How do I automatically import data from Excel to SQL Server?

One time: could right click database instance and choose Task-> Import Data. Automatic: build SSIS package and schedule job in SQL server to run ETL process.


1 Answers

You have different options depending on which server you are using, but with your current info you could do the following.

  1. click on the first empty column in the first row i.e. if data is in row 2 and all columns go from a to k, then you select L2
  2. Paste the following code

    ="INSERT INTO Address (AddressTypeID, Address1,City, State, Zip, Phone, PersonName, DisplayName, IsDefault, StatusID, UserID) VALUES '"&A2&"', '"&B2&"', '"&C2&"', '"&D2&"', '"&E2&"', '"&F2&"', '"&G2&"', '"&H2&"', '"&I2&"', '"&J2&"', '"&K2&"'"
    
  3. Check to see if the query is correct, it's calculated with the fields from a2-k2)

  4. copy -> paste special values only to the next column, so you keep your code, but you can select all rows with the calculated results to your sql query tool

enter image description here

like image 64
davejal Avatar answered Sep 21 '22 15:09

davejal