Reputation: 470
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.
Upvotes: 14
Views: 52619
Reputation: 7
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/
Upvotes: 1
Reputation: 11
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)
Upvotes: 0
Reputation: 4104
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.
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.
Upvotes: 1
Reputation: 6133
You have different options depending on which server you are using, but with your current info you could do the following.
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&"'"
Check to see if the query is correct, it's calculated with the fields from a2-k2)
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
Upvotes: 22