How to update remote table using openquery
-
Thursday, January 07, 2010 8:29 PM
When I run this code:
update openquery(securi01,'select nrate from employee') set nrate = RATES.nrate from RATES inner join employee on employee.cempid = RATES.cempid
I get this error:
Msg 208, Level 16, State 1, Line 1Invalid object name 'employee'.
Why doesn't this code work? I'm not sure if it can actually work but I can't find any useful information that helps me understand the OPENQUERY function so I'm hoping someone here can help- Moved by Tom PhillipsModerator Friday, January 08, 2010 2:59 PM TSQL Question (From:SQL Server Database Engine)
All Replies
-
Thursday, January 07, 2010 8:40 PM
Instead of using only the table name , try specifying the multipart name here
select nrate from db_name.schema_name.employee
Thanks, Leks -
Saturday, January 09, 2010 7:15 AMModerator
You can follow the syntax below. Let us know if helpful.
-- T-SQL OPENQUERY UPDATE remote table UPDATE oq SET oq.ListPrice = p.ListPrice FROM OPENQUERY([LINKEDSERVER], 'select ProductID, ListPrice from tempdb.dbo.Product') oq INNER JOIN AdventureWorks2008.Production.Product p ON p.ProductID = oq.ProductID
Kalman Toth SQL SERVER 2012 & BI TRAINING
New Book: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2012
- Proposed As Answer by Naomi NMicrosoft Community Contributor, Moderator Sunday, January 10, 2010 1:40 AM
- Marked As Answer by Zongqing Li Thursday, January 14, 2010 8:35 AM
- Edited by Kalman TothMicrosoft Community Contributor, Moderator Tuesday, October 09, 2012 5:49 PM
-
Tuesday, October 25, 2011 7:18 PM
Hi Kalman,
i've tried your solution on a Sql2k and i'm geting the following error
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'oq'.Is this syntax valid only on non 2k server? I don't think that i can use an alias with "from table" or am i missing something? I'm having troubles updating remote table on a linked server and with usual syntax, it's waaay to slow. Now, with openquery i'm almost there, but the last step is the toughest. I cannot update the remote table via openquery in an efficient way. Your solution looks very promising, if it only worked.
Thanks
-
Friday, June 15, 2012 4:53 PM
You can follow the syntax below. Let us know if helpful.
Following this sintax works for me in SQL2008, thanks.
-- T-SQL OPENQUERY UPDATE remote table UPDATE oq SET oq.ListPrice = p.ListPrice FROM OPENQUERY([LINKEDSERVER], 'select ProductID, ListPrice from tempdb.dbo.Product') oq INNER JOIN AdventureWorks2008.Production.Product p ON p.ProductID = oq.ProductID
Kalman Toth, SQL Server & BI Training, SSAS, SSIS, SSRS; http://www.SQLUSA.com

