473,785 Members | 2,607 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Copy all values of a column from one table to another

2 New Member
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 3900
chaarmann
785 Recognized Expert Contributor
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 Recognized Expert Top Contributor
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 Recognized Expert Contributor
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 Recognized Expert Top Contributor
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
himanshupancholi
2 New Member
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 Recognized Expert Top Contributor
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
45448
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.) How can I achieve this? I suppose I would get the most-hated "table/view is changing, trigger/function may not see it" error if I tried to write a trigger that checks the uniqueness of non-null values upon insert/update.
2
7072
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 command COPY t1 FROM '/tmp/file.xml';
2
3466
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 fields in the first line of the incoming file (omit the quotes, I put them there to see where the start and end of the line is), you should find 3 tabs after the second '1': '1 1 ' I have a table with this structure below and when...
5
6753
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 is a checkbox in one of the columns in this table. What I want to do is to copy the specific row, after user will check the checkbox, as a new row to another table called B. But I don't want to make exact copy, but only values from three columns...
4
5694
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 history database of TABLE1 How can I copy row(s) from table1 to table2 without conflict with IDENTITY columns using SQL query. Or must I do this from a program (C# .NET)
10
17952
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
5790
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 table. So for psuedo-code it would be something like: update Mapping Set SID = (select SMappingID from Mapping where SMappingID is not null) I don't know if I should be doing this in a loop (which I'm not totally familiar with using) or if there...
2
22242
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 table. So for psuedo-code it would be something like: update Mapping Set SID = (select SMappingID from Mapping where SMappingID is not null) I don't know if I should be doing this in a loop (which I'm not totally familiar with using) or if there...
3
24685
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 DataTable. Anyone can help me?
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
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
10341
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
8979
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...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6741
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
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3656
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.