12

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.

2

4 Answers 4

18

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

4
  • After figuring out a little more about the SQL a little tweak to what you gave me works. Thanks. Jan 31, 2017 at 0:43
  • what server are you using and what administration tools do you have?
    – davejal
    Jan 31, 2017 at 0:45
  • MS SQL Server with MS SQL Server Management Studio as my main tool, but I'm pretty sure I have everything buried somewhere. Management Studio is the only tool I have on Citrix, thus I do everything I can through it. Jan 31, 2017 at 1:03
  • I don't have server management studio, but mysql's phpmyadmin has options to import csv files (but this is not relevant to you). But you could look into the following links for future reference: support.discountasp.net/kb/a1179/… and mssqltips.com/sqlservertutorial/203/…
    – davejal
    Jan 31, 2017 at 1:32
1

SQL Server Management Studio will allow you to import data from Excel files. What this actually does is create a temporary SSIS package to run your import.

  1. Right click the database you want to import the data into.
  2. Check your column settings and make sure all your data types are correct.
  3. In the menu Select "Tasks" then find "Import data"
  4. In the data source drop-down you can select "Microsoft Excel"
  5. Set your file path and then click next
  6. Select the SQL Native client as your data source and it should default to the server and DB that you right-clicked
  7. You can import to an existing table or create a new one and configure the mappings.
  8. Run the process and check for any errors.

Now, some caveats. In order to import from an Excel file you need the Microsoft.ACE.OLEDB driver installed. I've also had issues with data type translations and other such things from Excel.

When I need to do an Excel import I will usually convert it to a CSV as that is much simpler. It's as easy as saving the document as a CSV in the Excel application. Then just change the first step above to flat file import.

It is a good idea when doing any import to really double check the column settings on the import. For CSV files at least you can have the import guess at the data types by parsing some of the rows. This usually does a good job but I often have to clean it up a little bit. Like changing floats to decimals and setting larger lengths on strings to avoid any truncation errors.

1
  • Yeah, the truncation errors can be a real bore. And also the error messages from SSIS are offen not very helpful, especially when trying to import large amounts of data. So, make sure to have plenty of time for this exercise! Jan 31, 2017 at 6:10
1

In Excel you can use this formula in any column ="("&A2&","&B2&","&E2&","&F2&","&G2&"),"

then copy the format by "CTRL" + scroll down to get all the data with same format. With date data, you can come to this site for more information: [a link]https://www.excel-exercise.com/title-with-date-excel/

0

Previously I have created excel VBA macros which automatically generate the sql statements from the inoput sheet, provided the input sheet is formatted correctly (basically excel headers match sql columns)

1
  • 2
    This answer doesn't address the question. You can find more information on how to write good answers in the help center.
    – Spinner
    Nov 25, 2021 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.