Sample SQL Code to do a table update from Excel

  • I used to know how to do this, but after 20 years the old grey matter has forgotten.

    I am doing a migration from Goldmine to CRM 4.0 and forgot to map the web site field.

    CRM 4.0 does not allow single field import, only whole records.

    I have my excel file with primary key, web site address and need to create a sql query to read the excel file and when the primary key matches, do a crm update, then loop to the next one till finished.

    I can do this one at a time but have forgotten how to loop around.

    Many thanks.

  • While it may be possible to do this directly from excel, I would do it as follows:

    Import the excel sheet as a table using the import wizard.

    Update the original table using a normal update statement

    Something like:

    UPDATE YourTable

    SET WebAddress = Excel.Webaddress

    FROM YourTable Y INNER JOIN ExcelTable Excel ON Y.PrimaryKey = Excel.PrimaryKey

    Seth Phelabaum


    Consistency is only a virtue if you're not a screwup. 😉

    Links: How to Post Sample Data[/url] :: Running Totals[/url] :: Tally Table[/url] :: Cross Tabs/Pivots[/url] :: String Concatenation[/url]

  • That worked great! Many Thanks.

    I used a CSV input file.

    Is it possible to read multiple CSV files by referencing a folder ?

    I have a bunch of text files i need to import.

    What would i do different ?

  • If it's something you need to do on a regular basis, you're probably going to want to look into setting up a bulk import job. I've never done one of these myself, so can't give you specific pointers, but I believe there are a lot of articles on setting them up.

    Seth Phelabaum


    Consistency is only a virtue if you're not a screwup. 😉

    Links: How to Post Sample Data[/url] :: Running Totals[/url] :: Tally Table[/url] :: Cross Tabs/Pivots[/url] :: String Concatenation[/url]

  • ifila (12/15/2008)


    That worked great! Many Thanks.

    I used a CSV input file.

    Is it possible to read multiple CSV files by referencing a folder ?

    I have a bunch of text files i need to import.

    What would i do different ?

    Have a look into this:

    http://www.databasejournal.com/features/mssql/article.php/3325701/Import-multiple-Files-to-SQL-Server-using-T-SQL.htm

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply