473,794 Members | 2,729 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

UPDATE Statement Involving Tables in Different Databases


I want to create an UPDATE statement that updates a value in a table in
database A based on the results of an inner join between tables each
residing in deifferent databases.

What is the correct syntax for doing this?

The following should give you an idea of what I'm trying to do:

UPDATE A.dbo.tblCars
SET A.dbo.tblCars.C ar = 'Ferrari'
FROM A.dbo.tblCars INNER JOIN B.dbo.tblHouses ON
A.dbo.tblCars.R ecID = B.dbo.tblHouses .RecID
WHERE (B.dbo.tblHouse s = 'Mansion')

Feb 22 '06 #1
7 24686
You can do this without a join.

UPDATE A.dbo.tblCars
SET Car = 'Ferrari'
WHERE RecID IN (SELECT RecID FROM B.dbo.tblHouses WHERE House =
'Mansion')

Using JOINs in UPDATEs may not give you the results you are expecting.
From BOL:

"The results of an UPDATE statement are undefined if the
statement includes a FROM clause that is not specified
in such a way that only one value is available for each
column occurrence that is updated (in other words, if the
UPDATE statement is not deterministic). "

Feb 22 '06 #2
Mark's suggestion worked great when I ran it on a simple test, but I'm
having problems applying this methodology to my real (more complex)
application. I get the following error when running the script from
Query Analyzer:

Server: Msg 7391, Level 16, State 1, Line 1
The operation could not be performed because the OLE DB provider
'SQLOLEDB' was unable to begin a distributed transaction.
[OLE/DB provider returned message: New transaction cannot enlist in the
specified transaction coordinator. ]
OLE DB error trace [OLE/DB Provider 'SQLOLEDB'
ITransactionJoi n::JoinTransact ion returned 0x8004d00a].

Note that both databases reside on the same server and I checked
Distributed Transaction Coordinator (DTC) to make sure that it's
running. What else about this kind of distributed UPDATE statement
would cause such an error?

Feb 24 '06 #3
Altemir (da***********@ gmail.com) writes:
Mark's suggestion worked great when I ran it on a simple test, but I'm
having problems applying this methodology to my real (more complex)
application. I get the following error when running the script from
Query Analyzer:

Server: Msg 7391, Level 16, State 1, Line 1
The operation could not be performed because the OLE DB provider
'SQLOLEDB' was unable to begin a distributed transaction.
[OLE/DB provider returned message: New transaction cannot enlist in the
specified transaction coordinator. ]
OLE DB error trace [OLE/DB Provider 'SQLOLEDB'
ITransactionJoi n::JoinTransact ion returned 0x8004d00a].

Note that both databases reside on the same server and I checked
Distributed Transaction Coordinator (DTC) to make sure that it's
running. What else about this kind of distributed UPDATE statement
would cause such an error?


If the databases are on the same instance of SQL Server, there is no
need for any distributed transaction. If you get this message, you
have somehow introduced a four-part notation into the mix.

If the databases are on different instances, you need one, even if the
instance are on the same machine.

What applies to your case? What exact syntax did you use?

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Feb 24 '06 #4

Erland Sommarskog wrote:

If the databases are on the same instance of SQL Server, there is no
need for any distributed transaction. If you get this message, you
have somehow introduced a four-part notation into the mix.

If the databases are on different instances, you need one, even if the
instance are on the same machine.

What applies to your case? What exact syntax did you use?


Sorry for the long delay in following this up ...

I'm only working with one instance of both databases. Don't know what
you mean by four-part notation.

Here is the SQL statement that is generating the "'SQLOLEDB' was unable
to begin a distributed transaction" error as I stated before. As you
can see, I am attempting to perform an UPDATE of a table in database
"MFG" based on the recordset generated by joining three tables
together:
UPDATE MFG.dbo.PURC_OR DER_LINE SET MFG.dbo.PURC_OR DER_LINE.USER_1 =
'Vendor'
WHERE ROWID IN
(SELECT MFG.dbo.PURC_OR DER_LINE.ROWID FROM
Status.dbo.tblP OBaselineDueDat es INNER JOIN MFG.dbo.PURC_OR DER_LINE ON
Status.dbo.tblP OBaselineDueDat es.PURC_ORDER_I D =
MFG.dbo.PURC_OR DER_LINE.PURC_O RDER_ID AND
Status.dbo.tblP OBaselineDueDat es.LINE_NO =
MFG.dbo.PURC_OR DER_LINE.LINE_N O INNER JOIN
Status.dbo.qryP ODueDates ON
MFG.dbo.PURC_OR DER_LINE.PURC_O RDER_ID =
Status.dbo.qryP ODueDates.PURC_ ORDER_ID AND
MFG.dbo.PURC_OR DER_LINE.LINE_N O = Status.dbo.qryP ODueDates.LINE_ NO AND
Status.dbo.tblP OBaselineDueDat es.BaselineDueD ate =
Status.dbo.qryP ODueDates.Curre ntDueDate
WHERE (MFG.dbo.PURC_O RDER_LINE.USER_ 1 LIKE 'Aut%'))

Mar 12 '06 #5
i can't help you with the syntax, but this has GOT to be slow to run if
this table has any size to it.
is this an issue?
if so, consider changing the last line to
WHERE (left(MFG.dbo.P URC_ORDER_LINE. USER_1,3)= 'Aut'))

Consider finding logic to limit the search through PURC_ORDER_LINE . For
instance, is the vendor currently a specific wrong thing? Are they
always blank?
Anything you can do do limit the records in PURC_ORDER_LINE will speed
it all up a bunch.

Mar 12 '06 #6
Altemir (da***********@ gmail.com) writes:
I'm only working with one instance of both databases. Don't know what
you mean by four-part notation.
An example of four-part notation:

SELECT ... FROM Server.db.dbo.t bl

In this query, we retrieve data from a second server, which could be
SQL Server, Oracle or whatever. (But since it says dbo, it's likely to
be SQL Server or Sybase.
Here is the SQL statement that is generating the "'SQLOLEDB' was unable
to begin a distributed transaction" error as I stated before. As you
can see, I am attempting to perform an UPDATE of a table in database
"MFG" based on the recordset generated by joining three tables
together:
UPDATE MFG.dbo.PURC_OR DER_LINE SET MFG.dbo.PURC_OR DER_LINE.USER_1 =
'Vendor'
WHERE ROWID IN
(SELECT MFG.dbo.PURC_OR DER_LINE.ROWID FROM
Status.dbo.tblP OBaselineDueDat es INNER JOIN MFG.dbo.PURC_OR DER_LINE ON
Status.dbo.tblP OBaselineDueDat es.PURC_ORDER_I D =
MFG.dbo.PURC_OR DER_LINE.PURC_O RDER_ID AND
Status.dbo.tblP OBaselineDueDat es.LINE_NO =
MFG.dbo.PURC_OR DER_LINE.LINE_N O INNER JOIN
Status.dbo.qryP ODueDates ON
MFG.dbo.PURC_OR DER_LINE.PURC_O RDER_ID =
Status.dbo.qryP ODueDates.PURC_ ORDER_ID AND
MFG.dbo.PURC_OR DER_LINE.LINE_N O = Status.dbo.qryP ODueDates.LINE_ NO AND
Status.dbo.tblP OBaselineDueDat es.BaselineDueD ate =
Status.dbo.qryP ODueDates.Curre ntDueDate
WHERE (MFG.dbo.PURC_O RDER_LINE.USER_ 1 LIKE 'Aut%'))


Since I am unable to see the forest for all the trees, I rewrote the
query as:

UPDATE MFG.dbo.PURC_OR DER_LINE
SET USER_1 = 'Vendor'
WHERE ROWID IN (SELECT MFG.ROWID
FROM Status.dbo.tblP OBaselineDueDat es DD
JOIN MFG.dbo.PURC_OR DER_LINE MFG
ON DD.PURC_ORDER_I D = MFG.PURC_ORDER_ ID
AND DD.LINE_NO = MFG.LINE_NO
JOIN Status.dbo.qryP ODueDates qDD
ON MFG.PURC_ORDER_ ID = qDD.PURC_ORDER_ ID
AND MFG.LINE_NO = qDD.LINE_NO
AND DD.BaselineDueD ate = qDD.CurrentDueD ate
WHERE MFG.USER_1 LIKE 'Aut%')

Using aliases makes query much easier to read.

There is no apparent reference to any linked server. However, one table
in the Status database is called tbl-something and another qry-something.
Could the latter be a view that includes a table from a linked server?
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Mar 12 '06 #7
Yes that was the problem: My select statement was querying Database A,
which involved a linked table to Database B. Although, technically, it
was not a "Linked Server" link -- it was a "Transact SQL" link -- but
the outcome appears to be the same.

I was able to re-write the UPDATE statement so that no linked tables
were involved. This actually resulted in a better design.

Thank you very much for your help, Doug, Erland, and Mark!!!!!!!

Mar 20 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
7354
by: Kenneth Fosse | last post by:
Hi. I'm currently working on a project which involves the creation of a web page which reports selected data to customers from two back-end systems. I use ASP script language on IIS, the server is MS SQL 2000. Now I'm struggling with combining two tables from the different databases. I'm sure it's simple enough, but I'm a little short on the SQL expertise. I've got two databases, db1 and db2, and then two tables, db1.t1 and
8
3186
by: Perre Van Wilrijk | last post by:
Hello, I have 2 ways of updating data I'm using often 1) via a cursor on TABLE1 update fields in TABLE2 2) via an some of variables ... SELECT @var1=FLD1, @var2=FLD2 FROM TABLE1 WHERE FLD-ID = @inputVAR UPDATE TABLE2 SET FLDx = @var1, FLDy = @var2
8
11601
by: pb648174 | last post by:
I have a single update statement that updates the same column multiple times in the same update statement. Basically i have a column that looks like .1.2.3.4. which are id references that need to be updated when a group of items is copied. I can successfully do this with cursors, but am experimenting with a way to do it with a single update statement. I have verified that each row being returned to the Update statement (in an...
1
1488
by: ChrisA | last post by:
Hi all, we are developing an internal application that involves "Timesheets", "Support" and "Project Management". Imagine that there are 3 different databases for the above scenario, under SQL Server 2000. My task is to create one or a few table triggers for INSERT/UPDATE/DELETE operations. For example: - if a row is added in Table A of "Timesheets" database, then Table B of "Project Management" needs to be updated.
2
2524
by: serge | last post by:
/* This is a long post. You can paste the whole message in the SQL Query Analyzer. I have a scenario where there are records with values pointing to wrong records and I need to fix them using an Update statement. I have a sample code to reproduce my problem. To simplify the scenario I am trying to use Order related tables to explain a little better the tables i have to work with.
19
8385
by: Steve | last post by:
ASP error number 13 - Type mismatch with SELECT...FOR UPDATE statement I got ASP error number 13 when I use the SELECT...FOR UPDATE statement as below. However, if I use SELECT statement without FOR UPDATE, it is fine and no error. I also tried Set objRs = objConn.Execute("SELECT * FROM EMP UPDATE OF EMPNO"), but it still couldn't help. any ideas? I tried to search in the web but couldn't find similar
8
8200
by: Stephen Plotnick | last post by:
I have three forms and update one table in an Access DB with sucess in one form. In the other two forms I'm trying to do an update to a different table in the Access DB. The error I'm getting in syntax error on Update on the statement da.update(mydataset, "DATABASENAME") I do have a primary key on both and copied the code from the working update
2
2964
by: =?Utf-8?B?VGVycnk=?= | last post by:
Is it possible to join tables from different databases? If so, what is the format of the select statement? Both databases are on the same server. TIA, -- Terry
4
4102
by: sganeshsvk | last post by:
sir, i want to store the same data values in two different databases at that same time in mysql using php programming. suppose any one databases data will lose then we use the other databases. suppose there is any query for store the same data values in two different databases at the same time. for eg: databases sample1
0
9518
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10433
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10212
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10161
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9035
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6777
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.