473,386 Members | 1,779 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,386 software developers and data experts.

Need help in Updating multiple rows

Hi,

New to writing sql script

I get this error in my sql script

Server: Msg 512, Level 16, State 1, Line 1
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.

I want to write a single sql script which will update column 1 (FK)
in table A with column 1 (PK) in table B

Here's an example of what I need to do

Table DATA_A
-------------
column C1 (Foreign Key)
11
11
11
22
22
22
33
33
33
33
44
55

Table DATA_B
------------
column C1 (Primary Key) Column C2 Column C3
11 ABC NULL
12 ABC 2004-12-12
22 EFG NULL
23 EFG 2003-12-12
33 HIJ NULL
34 HIJ 2003-12-12
44 KLM 2005-02-02
55 JJJ NULL

I need to update Table DATA_A set column C1 with 11 data to point to
Table DATA_B column C1 with 12 data. Currently, the problem is the
Table DATA_A Column C1 is pointing to the wrong primary key which has
NULL data in COLUMN C3. I need to point to the correct Primary Key with
Date filled in Column 3. The two primary key is tied together by
column C3.

Here's my SQl script

UPDATE DATA_A SET C1 =
(SELECT C1 FROM DATA_B
WHERE C2 in
(SELECT B1.C2 FROM DATA_B B1
WHERE EXISTS
(SELECT * FROM TABLE_B B2 WHERE B2.C3 is NOT NULL)
AND EXISTS
(SELECT * from TABLE_B B2 WHERE B2.C3 is NULL)
AND B2.C2 = B1.C2
GROUP BY B1.C2
HAVING COUNT(B1.C2) = 2)
AND C3 IS NOT NULL)
WHERE
(SELECT C1 FROM DATA_B
WHERE C2 in
(SELECT B1.C2 FROM DATA_B B1
WHERE EXISTS
(SELECT * FROM TABLE_B B2 WHERE B2.C3 is NOT NULL)
AND EXISTS
(SELECT * from TABLE_B B2 WHERE B2.C3 is NULL)
AND B2.C2 = B1.C2
GROUP BY B1.C2
HAVING COUNT(B1.C2) = 2)
AND C3 IS NULL)

Thanks - Been struggle at this for a while
MLR

Jul 23 '05 #1
1 1521
On 31 Mar 2005 08:44:54 -0800, ml*******@yahoo.com wrote:

(snip)
I want to write a single sql script which will update column 1 (FK)
in table A with column 1 (PK) in table B

Here's an example of what I need to do (snip)I need to update Table DATA_A set column C1 with 11 data to point to
Table DATA_B column C1 with 12 data. Currently, the problem is the
Table DATA_A Column C1 is pointing to the wrong primary key which has
NULL data in COLUMN C3. I need to point to the correct Primary Key with
Date filled in Column 3. The two primary key is tied together by
column C3.

(snip)

Hi MLR,

Try if this works:

UPDATE Data_A
SET C1 = (SELECT new.C1
FROM Data_B AS curr
INNER JOIN Data_B AS new
ON new.C2 = curr.C2
AND new.C3 IS NOT NULL
WHERE curr.C1 = Data_A.C1)

The above is untested. If you prefer a tested solution, then please post
a script with CREATE TABLE and INSERT statements, plus the output you
require. See www.aspfaq.com/5006.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #2

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

Similar topics

2
by: scott | last post by:
Hi, I'm having some trouble with something that should be relatively easy. I want to update multiple rows in one of my database tables simultaneously. In my table I have these values: ...
2
by: Irvin | last post by:
I new to ASP.net and am using the following code to attempt to update an Access 2000 mdb. The code does make it through the code following "try". NO rows are updated. There is a row with the...
1
by: marty.overdear | last post by:
>NET 2003, vb code behind. I am having a problem with the look of a page with a datagrid. I have the grid populated, and it can be a couple of hundred rows long. If the user is down toward the...
0
by: JimN1 | last post by:
Error: Update requires a valid UpdateCommand when passed DataRow collection with modified rows. This is a continuation of my previous table element update question. I am now getting the above...
1
by: Wavey | last post by:
Hi All, I have a problem with updating an Access database from a datatable. I have two rows of data in my database at the moment for testing, the first row is an ID number (primary key), the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.