I'm trying to update the value in column 'ID' from table 1 with the value in column 'ID' from table 2 - Only if they do not match. I think I have everything except the set statement down.
I'm wondering if this is the best way to go about it and how to format the sub-query for this type of problem
update table1 B
set B.id = (select A.id
from table2 A
where B.num = A.num
and B.name = A.name)
where B.num = A.num
and B.name = A.name
and B.id <> A.id
;
Maybe something like this?
update B
set B.id=A.Id
from table1 B
join table2 A
on B.num=A.num
and B.name=A.name
and B.id<>A.id
Oracle does not support join in an update. But you can use two subqueries:
update table1 B
set id = (select A.id
from table2 A
where B.num = A.num and
B.name = A.name
)
where exists (select A.id
from table2 A
where B.num = A.num and
B.name = A.name and
B.id <> A.id
);
Use the MERGE command.
Here is a basic example:
MERGE INTO table1 a
USING (SELECT bb.asdf,cc.zxcv
from table2 bb,table3 cc
where bb.field1=cc.field1) b
ON (a.asdf=b.asdf)
WHEN MATCHED THEN
UPDATE SET a.zxcv=b.zxcv;
Related
Assume that I have table A, and table B. Table B contains some rows from table A
How do I run an update like the following?
update table A
set A.column = case when B.id is present then B.column else null
from B where B.id = A.id OR B is null
EDIT
The original query is incomplete, this is the updated one i need help with
update table A
set A.column1 = case when B.id is present then B.column else null end,
set A.column2 = case when B.id is present then null else A.id end
from B
where B.id = A.id OR B is null
You can simply use a subquery:
UPDATE A SET A.column = (
SELECT B.column FROM B WHERE B.id = A.id
)
You can use a left join in the FROM clause as well to update multiple columns:
UPDATE A SET column1 = B.column1, column2 = B.column2
FROM A x LEFT JOIN B ON x.id = B.id
WHERE A.id = x.id
The table A in the FROM clause is only to allow a left join with B.
I have two tables and I want to select all values from "TABLE A" that have a different value in a column from "TABLE B".
I tried this
SELECT A.* FROM tableA A
left join tableB B ON A.id = B.id WHERE B.column <> 1;
But this just return the value that I want to ignore.
SELECT A.*
FROM tableA A
INNER JOIN tableB B
ON A.id = B.id
WHERE B.column != 1;
or
SELECT A.* FROM tableA A WHERE A.Id NOT IN (SELECT B.Id FROM tableB B WHERE B.column != 1)
Depends on your SQL you can use <> or !=
I would suggest not exists:
SELECT A.*
FROM tableA A
WHERE NOT EXISTS (SELECT 1 FROM tableB B WHERE A.id = B.id AND B.column = 1);
Using a JOIN can result in duplicated rows, if more than one row in B matches the JOIN condition.
I want to update a table with data from another schema. Why does this not work?
UPDATE table a
SET a.value = b.value
FROM other_schema.table b
WHERE a.id = b.id AND b.value IS NOT NULL;
I've tried this with a join on id as well, but with the same syntax error.
Oracle doesn't support a FROM clause in an UPDATE statement. You'd want something like
UPDATE table a
SET a.value = (SELECT b.value
FROM other_schema.table b
WHERE a.id = b.id
AND b.value IS NOT NULL)
WHERE EXISTS(SELECT b.value
FROM other_schema.table b
WHERE a.id = b.id
AND b.value IS NOT NULL)
You could omit the EXISTS if you want to update every row in a whether or not there is a match in b but I assume that is not your goal.
If your join produces a key-preserved result, you could also
UPDATE( SELECT a.id, a.value a_value, b.value b_value
FROM table a
JOIN other_schema.table b
ON a.id = b.id AND
b.value IS NOT NULL )
SET a_value = b_value;
This is my query that does not work in Netezza:
UPDATE TABLE1 A
SET A.COL1= (SELECT DISTINCT B.COL1 FROM TABLE2 B WHERE B.ID= A.ID AND B.DeptID=104)
WHERE A.DeptID=3
How do I re-write this query?
Please help.
UPDATE TABLE1 A
SET A.COL1 = B.COL1
FROM TABLE2 B
WHERE
A.ID = B.ID AND
A.DeptID = 3 AND
B.DeptID = 104;
UPDATE
(
SELECT
a.COL1
FROM
TABLE1 a,
TABLE2 b,
TABLE3 c
WHERE
a.name = b.name
c.ccol = b.ccol AND
AND b.col1 = 'anyvalue'
AND a.col2 = 'anothervalue'
) u
SET
u.COL1 = 'VALUE'
This query does not work, since TABLE1 does not contains PK. How to write such a query ?
The following should achieve what it looks like you are trying to achieve above:
UPDATE TABLE1
SET COL1 = 'VALUE'
WHERE EXISTS
( SELECT 1
FROM TABLE2 B
INNER JOIN TABLE3 C
ON B.Ccol = C.Ccol
WHERE b.Name = Table1.Name
AND b.Col1 = 'AnyValue'
AND c.Col1 = 'AnotherValue'
)
I've never worked in oracle, but try something like this. Not having a pk shouldn't be an issue, as long as your joins & where statements are correct.
This is the SQL equivalent of what you're asking for
UPDATE u
SET u.COL1 = 'VALUE'
FROM Table1 AS u
INNER JOIN Table2 AS b ON u.name = b.name
INNER JOIN Table3 AS c ON c.ccol = b.ccol
WHERE b.col1 = 'anyvalue' AND u.col2 = 'anothervalue'