Update local table from linked server (Excel file)

  • Hi there,

    I am a newbie in Sql Server querys, and I really need some help with a task that have been assigned to me. I need to update a table in one of the server databases with some info from an Excel Worksheet. For example, I have to change the wharehouse id from some items in the table, for the new ones as they appear in the excel sheet.

    As they appear in the database:

    ItemID___Wharehouse

    1---------external

    2---------external

    3---------internalD

    4---------internalE

    As they appear in the excel Sheet:

    1---------obsolete

    2---------obsolete

    3---------fullinternal

    4---------fullinternal

    I already put a linked server that points to the excel sheet, and when i query all the data (select * from SELECT * FROM ExcelDataSource...sheet1$) i can see all the data from excel.

    At this point everything is ok. But I need to make an UPDATE of the data, so if the itemid exists in the linkedserver, update the wharehouse column data.

    Something like this (sorry, is a rough idea):

    UPDATE dbo.tabletoupdate

    set WHAREHOUSE = sheet1$.WHAREHOUSE

    WHERE ItemID = sheet1$.ItemID

    Can someone help me get this done? I have searched and read a lot but I have found more information about the reverse process (update the excel file) and not about a scenario like this. And sorry if this have been already covered here, but i havent been able to find it.

    Note: I cannot import as a table the excel data as the database have a lot more information detail. The excel sheet have basically just the data to update.

    Thanks in advance.

  • if you've got the linked server up and running, that's 99% of it;

    the rest is just syntax.

    something like this is how you update from another table...and it doesn't matter if the other table is local, linked server, etc.

    aliases can help a lot here as well;

    --syntactically correct, updates all rows, many to same value, most likely

    UPDATE dbo.tabletoupdate

    SET WHAREHOUSE = MyExcel.WHAREHOUSE

    FROM ExcelDataSource...sheet1$ MyExcel

    WHERE dbo.tabletoupdate.ItemID = MyExcel.ItemID

    --much more explicit, only change rows that are different

    UPDATE MyTargetTable

    SET WHAREHOUSE = MyExcel.WHAREHOUSE

    FROM dbo.tabletoupdate MyTargetTable

    LEFT OUTER JOIN ExcelDataSource...sheet1$ MyExcel

    ON MyTargetTable.ItemID = MyExcel.ItemID

    WHERE MyTargetTable.WHAREHOUSE <> MyExcel.WHAREHOUSE

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Thanks Lowell,

    I'll try that as soon as I can, and will let you know how it went.

    Regards,

  • Hi Lowell,

    Sorry for answering until now, but i have been very busy working in another project...

    Your guidance was really userful, it worked like a charm!!!

    Thank you very much!!! I tested it in a non-working scenario and everything was ok. Now, I will change the table in the production database, but I need to know how to make a proper rollback just in case (I have no problems with my backups), using begin tran / commit tran / rollback tran. Is this recommended? I just want to be sure of the changes and commit those changes, from the management studio, so in case of error recover faster than using the daily backups. (Sorry if this requires to be in a new post. Correct me if it should be.)

    Regards,

  • this is typically how i do it.

    1. do a full backup, just in case.

    2. SELECT the rows that in theory, should be affected...we might get a specific row count out of this, which we want our update to match:

    SELECT *

    FROM dbo.tabletoupdate MyTargetTable

    LEFT OUTER JOIN ExcelDataSource...sheet1$ MyExcel

    ON MyTargetTable.ItemID = MyExcel.ItemID

    WHERE MyTargetTable.WHAREHOUSE <> MyExcel.WHAREHOUSE

    3. create an explicit transaction with teh update statement , and let it run:

    SET XACT_ABORT ON

    BEGIN TRAN

    --much more explicit, only change rows that are different

    UPDATE MyTargetTable

    SET WHAREHOUSE = MyExcel.WHAREHOUSE

    --SELECT *

    FROM dbo.tabletoupdate MyTargetTable

    LEFT OUTER JOIN ExcelDataSource...sheet1$ MyExcel

    ON MyTargetTable.ItemID = MyExcel.ItemID

    WHERE MyTargetTable.WHAREHOUSE <> MyExcel.WHAREHOUSE

    --ROLLBACK TRAN

    --COMMIT TRAN

    4. IMPORTANT Note the above has both COMMIT TRAN and ROLLBACK TRAN COMMENTED OUT! the table's going to be locked untill you are done reviewing and you either commit or rollback!

    5. Review the data in general, mae sure it looks good.

    SELECT *

    FROM dbo.tabletoupdate MyTargetTable

    LEFT OUTER JOIN ExcelDataSource...sheet1$ MyExcel

    ON MyTargetTable.ItemID = MyExcel.ItemID

    6. lets go ahead and commit, if it's good.

    COMMIT TRAN

    7. IF it was bad, just ROLLBACK the transaction and start fresh.

    ROLLBACK TRAN

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Hi Lowell,

    Your guidance has been very very helpful. The steps abouve are working really nice. I have updated about 8 tables using these methods, but...there is this one table that wont update, resulting in the following error:

    Msg 2627, Level 14, State 1, Line 1

    Violation of PRIMARY KEY constraint 'PK_WAREHOUSEINFO'. Cannot insert duplicate key in object 'dbo.WAREHOUSEINFO'.

    The statement has been terminated.

    This bugs me because apparently this table have the same structure as the ones where the update have worked.

    But apart from that, everything is going nice and smooth.

    Thanks Lowell!!!

  • Lowell (12/6/2012)


    if you've got the linked server up and running, that's 99% of it;

    the rest is just syntax.

    something like this is how you update from another table...and it doesn't matter if the other table is local, linked server, etc.

    aliases can help a lot here as well;

    --syntactically correct, updates all rows, many to same value, most likely

    UPDATE dbo.tabletoupdate

    SET WHAREHOUSE = MyExcel.WHAREHOUSE

    FROM ExcelDataSource...sheet1$ MyExcel

    WHERE dbo.tabletoupdate.ItemID = MyExcel.ItemID

    --much more explicit, only change rows that are different

    UPDATE MyTargetTable

    SET WHAREHOUSE = MyExcel.WHAREHOUSE

    FROM dbo.tabletoupdate MyTargetTable

    LEFT OUTER JOIN ExcelDataSource...sheet1$ MyExcel

    ON MyTargetTable.ItemID = MyExcel.ItemID

    WHERE MyTargetTable.WHAREHOUSE <> MyExcel.WHAREHOUSE

    Good stuff Lowell! Have you ever tried an UPDATE through OPENROWSET like this?

    Just wondering, because I haven't either. I've found this kind of integration to Excel to be somewhat of a pain in the past.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • Sorry, I found where the problem was....I know it was obvious but it was killing me...

    Of the 30,000 rows of the table with the problem, two of them became duplicated records (with the update), so the query aborted everytime. I made a temporary workaround (manually changing only those 2), so it could not affect the query.

    Sorry again, still learning from the experts...

    Great forum!

    Thanks.

Viewing 8 posts - 1 through 7 (of 7 total)

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