Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Update local table from linked server (Excel file)


Update local table from linked server (Excel file)

Author
Message
jmenax
jmenax
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 31
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.
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14955 Visits: 38950
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!

jmenax
jmenax
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 31
Thanks Lowell,
I'll try that as soon as I can, and will let you know how it went.
Regards,
jmenax
jmenax
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 31
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,
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14955 Visits: 38950
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!

jmenax
jmenax
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 31
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!!!
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4249 Visits: 6431
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!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
jmenax
jmenax
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 31
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search