473,396 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

SQL update on multiple tables

Hi,

I tried to use the following query to update a value in one table with
a value from another table:

UPDATE tbl1
SET col1 = tbl2.col2
FROM tbl1, tbl2
WHERE tbl1.[id] = tbl2.[id]

but it won't work. I also tried this with a subquery using "TOP 1",
but that wouldn't work either. SQL Server 2000 gives me the following
error-message:

"Subquery returned more than 1 value. This is not permitted when the
subquery follows =, !=, <, <= , >, >= or when the subquery is used as
an expression.
The statement has been terminated."

What is wrong? It seems that the join is not functioning properly, but
it functions properly with a rather 'simple' SELECT. How to update
those fields, other than by hand ;)

Regards,

Falco Vermeer
Jul 20 '05 #1
3 70100
The query you posted appears to be valid TSQL.
"Subquery returned more than 1 value. This is not permitted when the
subquery follows =, !=, <, <= , >, >= or when the subquery is used as
an expression.
The statement has been terminated."


Do you get that error message from the UPDATE statement you posted or from
some other UPDATE statement? (you aren't quite clear on that point) If you
get that error from the statement you posted then I suspect the problem is
with a trigger since that statement doesn't include a subquery. Check any
trigger code you have on that table.

If you don't get that error message then what does "won't work" mean.

Note also that ANSI/ISO Standard SQL doesn't allow joins in an UPDATE
statement, only subqueries. The Standard SQL version of the statement you
posted is as follows:

UPDATE tbl1
SET col1 =
(SELECT col2
FROM tbl2
WHERE tbl1.[id] = tbl2.[id])

This assumes that ID is unique in Tbl2, otherwise you will get the error you
mentioned whereas the proprietary UPDATE syntax you posted will fail to
detect that logical error and go ahead by updating your table in an
indeterminate manner that may not be what you expect. I always recommend
sticking to the "safer" ANSI syntax where possible.

--
David Portas
SQL Server MVP
--
Jul 20 '05 #2
I get that error-message from the real UPDATE-statement, but I replaced
the tables with dummy-tables, so it's only reflecting the actual
situation and not showing any correct fieldnames.

There are more records in tbl1 than in tbl2. I also tried

UPDATE tbl1
SET col1 =
(SELECT col2
FROM tbl2
WHERE tbl1.[id] = tbl2.[id]) WHERE EXISTS
(SELECT col2
FROM tbl2
WHERE tbl1.[id] = tbl2.[id])

but that gives me the same error message ("Subquery returned more than 1
value."). I think one of those methods should work, but they fail both.
I have got triggers on that table, and I am going to check them, but
those triggers always worked correctly. I have no clue why they should
fail with this query.

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3
Falco Vermeer (f.*******@alternate.nl) writes:
I get that error-message from the real UPDATE-statement, but I replaced
the tables with dummy-tables, so it's only reflecting the actual
situation and not showing any correct fieldnames.
The statement you posted, cannot alone give that error message you
posted.
but that gives me the same error message ("Subquery returned more than 1
value."). I think one of those methods should work, but they fail both.
I have got triggers on that table, and I am going to check them, but
those triggers always worked correctly. I have no clue why they should
fail with this query.


Maybe the triggers were not written to handle multi-row updates?

Also, if you run your update from Query Analyzer, the error message
should contain where the error occurs. That is, if the occurs in a
trigger, the trigger name will be in the message, and so will the
line number of the statement that is causing the problem.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4

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

Similar topics

17
by: kalamos | last post by:
This statement fails update ded_temp a set a.balance = (select sum(b.ln_amt) from ded_temp b where a.cust_no = b.cust_no and a.ded_type_cd = b.ded_type_cd and a.chk_no = b.chk_no group by...
1
by: Wing | last post by:
Hi all, I have created 2 tables in sql database and join these 2 tables before assign the result to the dataset, and display the result in datagrid. Everything is fine up to this point. The...
9
by: jaYPee | last post by:
I have search a lot of thread in google newsgroup and read a lot of articles but still i don't know how to update the dataset that has 3 tables. my 3 tables looks like the 3 tables from...
1
by: Foef | last post by:
When I have a stored procedure, with multiple tables in MS Access, in a dataset and I change it in my DataGrid, I get the next message when I want to update my DataSet: ...
1
by: CS | last post by:
I need to update multiple tables from one form using a command button. The info from the different text boxes should then go into the tables. Can someone help me with some example code on how to...
6
by: shil | last post by:
Hi, I am writing a windows app in .net 2003. I have a datagrid which gets data from a storedprocedure. My question is how can I update the data in the datagrid? I want to call another...
1
by: andrewcw | last post by:
I have just 1 table that I am updating, the SQL I use to generate the DataTable is complex using multiple tables, however when I view the fieldnames of the DataTable from the DataSet - everything...
10
by: frizzle | last post by:
Hi there, I'm building a music site with a mysql backend. It has a many to many relational database. I use this to match music genres with certain artists, to maintain the possibility to add...
4
by: dstorms | last post by:
I'm trying to run an update query on multiple tables, and since Access doesn't allow me to update tables from a union query, I'm writing a module as a workaround. So I've set up a temporary recordest...
5
by: Bogdan | last post by:
Hi, I have a stored procedure that uses JOINs to return columns from multiple tables. I also have another stored proc that that takes a series of params and updates multiple tables. I used the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...
0
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,...

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.