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

Copy all values of a column from one table to another

Hi,
I need to Copy all values of a column from one table to another. Below are the details:
Source: STL_GRP table, VEND column
Destination PARTNER table, VEND column.

I am using the below query which is incorrect:
Expand|Select|Wrap|Line Numbers
  1.  
  2. UPDATE PARTNER
  3. SET VEND=
  4. (
  5.     SELECT DISTINCT STL.VEND
  6.     FROM STL_GRP STL, PARTNER PRT
  7.     WHERE PRT.PARTNER_CD = STL.PARTNER_CD
  8. )
  9.  
This query is returning error: "ORA-01427: single-row subquery returns more than one row", which is true.

Can someone suggest an alternative to do this? Any help will be appreciated. Thanks.
Mar 22 '08 #1
7 3883
chaarmann
785 Expert 512MB
What do you expect the database to return if it found two rows in the second table that matches the current row? And also these values are different??? (so they are not filtered out by DISTINCT?
Take the first one or the second one? Concatenate data of first and second one and then store?

I mean you must make your "where" clause in a way that exactly one record of source table matches exactly one record (or none) in destination table.
You can do that by making sure the matching keys PRT.PARTNER_CD = STL.PARTNER_CD are unique in both tables. Or filter out more inside your where clause, by matching with a second column.
Or matching with min(rownum) to get the first record.

Hi,
I need to Copy all values of a column from one table to another. Below are the details:
Source: STL_GRP table, VEND column
Destination PARTNER table, VEND column.

I am using the below query which is incorrect:

UPDATE PARTNER
SET VEND=
(
SELECT DISTINCT STL.VEND
FROM STL_GRP STL, PARTNER PRT
WHERE PRT.PARTNER_CD = STL.PARTNER_CD
)

This query is returning error: "ORA-01427: single-row subquery returns more than one row", which is true.

Can someone suggest an alternative to do this? Any help will be appreciated. Thanks.
Mar 22 '08 #2
amitpatel66
2,367 Expert 2GB
Try using EXISTS and check if it works.

Expand|Select|Wrap|Line Numbers
  1.  
  2. UPDATE PARTNER
  3. SET VEND=
  4. (
  5. SELECT DISTINCT STL.VEND
  6. FROM STL_GRP STL, PARTNER PRT
  7. WHERE PRT.PARTNER_CD = STL.PARTNER_CD
  8. )
  9. WHERE EXISTS (SELECT DISTINCT STL.VEND
  10. FROM STL_GRP STL, PARTNER PRT
  11. WHERE PRT.PARTNER_CD = STL.PARTNER_CD)
  12.  
  13.  
Mar 24 '08 #3
chaarmann
785 Expert 512MB
So what is the difference of the original and the solution below?
The SQL below does not solve the issue with 2 matching records.
The only difference I can see is that if a record does not exist in second table, no copy into first table is done instead of assigning null value there.

Try using EXISTS and check if it works.

Expand|Select|Wrap|Line Numbers
  1.  
  2. UPDATE PARTNER
  3. SET VEND=
  4. (
  5. SELECT DISTINCT STL.VEND
  6. FROM STL_GRP STL, PARTNER PRT
  7. WHERE PRT.PARTNER_CD = STL.PARTNER_CD
  8. )
  9. WHERE EXISTS (SELECT DISTINCT STL.VEND
  10. FROM STL_GRP STL, PARTNER PRT
  11. WHERE PRT.PARTNER_CD = STL.PARTNER_CD)
  12.  
  13.  
Mar 24 '08 #4
amitpatel66
2,367 Expert 2GB
So what is the difference of the original and the solution below?
The SQL below does not solve the issue with 2 matching records.
The only difference I can see is that if a record does not exist in second table, no copy into first table is done instead of assigning null value there.
Mistake!! I meant with few more conditions or with Aggregate function to select single value from more than one records returned by the query!!
Mar 24 '08 #5
I will re-phrase the problem. If there are multiple entries of VEND in STL_GRP, then first entry should be picked up.
Now, certainly below query will not help me, as internal query returns all the VEND values not just first one. Can you suggest something?

Expand|Select|Wrap|Line Numbers
  1.  
  2. UPDATE PARTNER
  3. SET VEND=
  4. (
  5.     SELECT DISTINCT STL.VEND
  6.     FROM STL_GRP STL, PARTNER PRT
  7.     WHERE PRT.PARTNER_CD = STL.PARTNER_CD
  8. )
  9.  
  10.  
Mar 27 '08 #6
amitpatel66
2,367 Expert 2GB
Try This:

Expand|Select|Wrap|Line Numbers
  1.  
  2. UPDATE PARTNER PRT
  3. SET PRT.VEND=
  4. (
  5.     SELECT DISTINCT STL.VEND
  6.     FROM STL_GRP STL
  7.     WHERE PRT.PARTNER_CD = STL.PARTNER_CD
  8.     AND STL.ROWID = (SELECT MAX(ROWID) FROM STL_GRP ST WHERE     PRT.PARTNER_CD = ST.PARTNER_CD))
  9.  
  10.  
Mar 27 '08 #7
try limit 1 if you only want the first returned....
Expand|Select|Wrap|Line Numbers
  1. UPDATE PARTNER 
  2. SET VEND= 
  3.     SELECT DISTINCT STL.VEND 
  4.     FROM STL_GRP STL, PARTNER PRT 
  5.     WHERE PRT.PARTNER_CD = STL.PARTNER_CD limit 1
  6. )
Oct 15 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

26
by: Agoston Bejo | last post by:
I want to enforce such a constraint on a column that would ensure that the values be all unique, but this wouldn't apply to NULL values. (I.e. there may be more than one NULL value in the column.)...
2
by: C G | last post by:
Dear All, I'm trying to insert an xml file into my database. I have a table with a single text column. My intention is just to have the xml file take up one row in the table. I've tried the...
2
by: Robert Fitzpatrick | last post by:
Does COPY require values for each column even though the database field is not set to NOT NULL? I did a COPY TO first to line up the data and then COPY FROM a tab delimited file. Here are the first...
5
by: Blazej | last post by:
First of all I want to say Hi to every one. So.. Hi :) I'm new in Access, so please be patient. I've got this problem: I have a subform, where user can see results from one table called A. There...
4
by: GTi | last post by:
I have two identical tables with one IDENTITY column and several other columns. I have tested the COPY * INTO table2 FROM table1 WHERE xx but it requers that table2 does not exist. TABLE2 is a...
10
by: pbd22 | last post by:
Hi. Like the title says - how do i do this? I was given the following example: INSERT INTO TABLE2 SELECT * FROM TABLE1 WHERE COL1 = 'A' The above statement threw the following error:
5
by: NamelessNumberheadMan | last post by:
I have a table I need to make changes to. Dropping columns isn't my problem, but before I drop one of the columns I need to copy the values that aren't null to another existing column in the same...
2
by: NamelessNumberheadMan | last post by:
I have a table I need to make changes to. Dropping columns isn't my problem, but before I drop one of the columns I need to copy the values that aren't null to another existing column in the same...
3
by: Manuel | last post by:
Hi to all, I'm trying to copy a Datacolumn from a table to another, but with this code: destTable.Columns.Add(srcTable.Column); I got this error: Column 'colname' already belongs to another...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...

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.