I'm trying to update my table if any value from table1 = value from table2
Table1 as 1 column with data
Table2 as many columns with data
If table2(data) = table1(data) update
But isn't working
I had one code that was working if i set table2 with 1 column
This one is working but table2 needs to have 1 column only
UPDATE table1
SET column1 = 'correct'
WHERE column2 in (SELECT column1 from table2);
I want to be able to do having more column
maybe something like this:
UPDATE
SET column1 = 'correct'
WHERE column2 in (SELECT * from table2);
The error:
Result: sub-select returns 11 columns - expected 1
How should I do it?
Maybe you can do it with EXISTS:
UPDATE table1
SET column1 = 'correct'
WHERE EXISTS (
SELECT 1 FROM table2
WHERE table1.somecolumn = table2.someothercolumn
);
If you want to check table1.someothercolumn against multiple columns of table2:
UPDATE table1
SET column1 = 'correct'
WHERE EXISTS (
SELECT 1 FROM table2
WHERE table1.somecolumn in (table2.col1, table2.col2, ...)
);
Related
I have an update query that when I run it instead of matching each unique row it is replicating the same row through the entire column.
Any help would be massively appreciated.
I have got
UPDATE Table1
SET Column1 = (SELECT Column1 FROM [Table2] WHERE Column2 = [Table2].Column2)
[Table2].Column2 referes to the column in Table2.
Column2 refers to the column in some table that has such a column. The innermost such table is Table2.
So this ends up being the same as Table2.Column2 = Table2.Column2.
To refer to the column in Table1, specify that table:
UPDATE Table1
SET Column1 = (SELECT Column1
FROM Table2
WHERE Table2.Column2 = Table1.Column2);
Relatively new to Oracle. I am trying to replace the columns of one table with the columns of another. Here is my code:
update ems.ptnaddress p
set (p.ptnid, p.address1,p.address2, p.address3,p.address4) =
(select p2.ptnid, p2.address1,p2.address2, p2.address3,p2.address4
from tempptnaddress p2
where p.ptnid = p2.ptnid);
I am getting the error :
SQL Error: ORA-01427: single-row subquery returns more than one row
Any ideas of what to do?
Basically your tempptnaddress table has more than one row for at least some of the ptnid values and Oracle isn't going to pick which one of those rows to use to do the update for you. :)
For example, if I have table1 as
COL1 COL2 COL3
1 ONE ENG
1 UNO SPA
2 TWO ENG
3 THREE ENG
4 FOUR ENG
4 CUATRO SPA
and table2 as
COL1 COL2
1
2
3
4
and try and update TABLE2.COL2 using something like:
UPDATE table2 t2
SET col2 = (SELECT col2 FROM table1 t1 WHERE t1.col1 = t2.col1)
what value in column TABLE1.COL2 should it use for 1 and 4? Oracle won't guess for us and that's when you get the ORA-01427.
It might be as easy as just picking one arbitrarily like:
UPDATE table2 t2
SET col2 = (SELECT col2 FROM table1 t1 WHERE t1.col1 = t2.col2 AND ROWNUM = 1)
but you probably want to put in some proper logic there like:
UPDATE table2 t2
SET col2 = (SELECT t1.col2 FROM table1 t1 WHERE t1.col1 = t2.col1 AND t1.col3 = 'ENG')
In your case, you need to get it so that the subquery in your update only returns one row per ptnid.
If you run this:
SELECT ptnid, COUNT(*)
FROM tempptnaddress
GROUP BY ptnid
HAVING COUNT(*) > 1
it will show you what ptnids that have more than one row in tempptnaddress. The trick will be to find out why there is more than one row per ptnid and how to pick the right one to use for the update.
Try this
UPDATE (SELECT p.ptnid ptnid1,p.address1 Addressnew1, p.address2 p.address3 p.address4 Addressnew4,p.ptnid ptnid2, p.address1 Addressold1, p.address2 p.address3 Addressold3, p.address4 Addressold4 FROM ems.ptnaddress p,tempptnaddress p2 WHERE p.ptnid = p2.ptnid) SET ptnid1 = ptnid2,Addressnew1 = Addressold1, Addressnew2 = Addressold2,Addressnew3 = Addressold3,Addressnew4 = Addressold4
I am trying to update one column in table1 from a column in table2. Here
is what i am doing but i am getting an ORA error.
ORA-01427: single-row subquery returns more than one row.
update table1 a
set a.art_num = (
select b.art_num from table2 b
where a.comp_id = b.comp_id );
Thanks so much in advance!
This happens because your subquery returns more than one result.
You should check this one:
select b.art_num
from table2 b
where a.comp_id = b.comp_id
You could try to select DISTINCT (search for distinct in the link for documentation) on the subquery:
update table1 a
set a.art_num = (
select distinct(b.art_num)
from table2 b
where a.comp_id = b.comp_id );
I am having two tables which i wish to join.
I am not using foreign keys.
table structure table1
id,val1
table structure table2
id,val2
I am using now following command:
update table1 set val1=(SELECT val2 FROM table2 WHERE table1.id LIKE table2.id)
How can i force sqlite not to insert a value in table1 if an id does not exist in table2?
To use exact comparisons, use = instead of LIKE.
To update only those rows where a match is found, use a WHERE clause:
UPDATE Table1
SET val1 = (SELECT val2
FROM Table2
WHERE Table1.id = Table2.id)
WHERE id IN (SELECT id FROM Table2);
If you omit the WHERE clause, rows without a match get updated with a NULL value.
I'd like to do something like this:
Suppose I have two tables, myTable1 and myTable2. Suppose both of these tables have columns myColumn1 and myColumn2.
update
myTable1
set
myTable1.myFlagColumn = 1
where
myTable1.myColumn1, myTable1.myColumn2
in
(select myTable2.myColumn1, myTable2.myColumn2 from myTable2)
Essentially, I want to change a value in myTable1 if there are any rows where the two columns in myTable1 and myTable2 match.
Is this possible?
Yes, but you will use an EXISTS clause:
update
myTable1
set
myTable1.myFlagColumn = 1
where
EXISTS
(select * FROM myTable2 WHERE myTable2.myColumn1 = myTable1.myColumn1
AND myTable2.myColumn2 = myTable1.myColumn2)