SQL insert data with inner join using oracle - sql

New to SQL I have two tables table_A and table_B.
I want to add data into a specific column of table_A depending on a inner join on table_B. I am using Oracle, following this method my SQL looks like this :
I first tried that :
INSERT INTO table_A (target_column)
SELECT table_B.wanted_data
FROM table_B INNER JOIN table_A ON table_B.someColumnB = table_A.someColumnA
Here the issue is that it would insert the data in new lines of my table_A and not in the existing lines.
So I tried that from the stackoverflow thread :
UPDATE(SELECT table_A.target_column, table_B.wanted_data
FROM table_A
INNER JOIN table_B
ON table_A.someColumnA = table_B.someColumnB
)
SET table_A.target_table = table_B.wanted_data
But it is not working either "SQL command not properly ended"
EDIT : target_column and wanted_data have the same name in my data set, not sure if it changes anything.

SQL Sever:
UPDATE a
SET a.target_column = b.wanted_data
FROM table_A a
JOIN table_B b
ON b.someColumnB = a.someColumnA
Oracle:
UPDATE
(
SELECT b.wanted_data AS wanted_data
,a.target_column AS old_data
FROM table_A a
INNER JOIN table_B b
ON b.someColumnB = a.someColumnA
) c
SET c.old_data = c.wanted_data

Regarding this approach, which you said fails with "SQL command not properly ended":
(This is the failing code:)
update
( select table_a.target_column
, table_b.wanted_data
from table_a
join table_b
on table_b.somecolumnb = table_a.somecolumna
)
set table_a.usineid = table_b.usineid;
I can't see why you would get that error, but the last line is incorrect because labels table_a and table_b only exist inside the inline view, which is anonymous. (Also there is no UsineId but I am assuming that is a copy-paste issue in your example.) It needs to be either:
update
( select table_a.target_column
, table_b.wanted_data
from table_a
join table_b
on table_b.somecolumnb = table_a.somecolumna
) -- you are updating an anonymous inline view
set target_column = wanted_data;
or
update
( select table_a.target_column
, table_b.wanted_data
from table_a
join table_b
on table_b.somecolumnb = table_a.somecolumna
) v -- you are updating an inline view named V
set v.target_column = v.wanted_data;
Then, there needs to be a unique index or constraint on the parent key, table_b.somecolumnb, otherwise Oracle will refuse to attempt any update with
ORA-01779: cannot modify a column which maps to a non key-preserved table
Alternatively, you can use merge:
merge into table_a a
using table_b b
on (b.somecolumnb = a.somecolumna)
when matched then update set a.target_column = b.wanted_data;

Give this a try:
update table_A A
set A.target_column = (
select table_B_data
from table_B
where shared_key = A.shared_key
);
This will only work if there is one row with table_B_date in table_B. If there are multiple rows, you need to specify the key

Related

How to Update Table A column 3 with input from Table B column 3 Teradata

Attached is my solution but it shows error expect something between column3 and from
update table_A
set table_A.column3 = table_B.column3
from table_A
join table_B on (table_A.column1 = table_B.column1 and
table_A.column2 = table_B.column2);
Update requires implicit join syntax:
update table_A
from table_A, table_B
set table_A.column3 = table_B.column3
where (table_A.column1 = table_B.column1 and
table_A.column2 = table_B.column2);
I fixed the order of keywords, too.

Redshift: UPDATE statement using a LEFT JOIN returns "table name specified more than once" error

I have an UPDATE statement in Redshift that I'd like to use the LEFT JOIN method to identify records that don't exist in the second table. The statement keeps returning the "table name specified more than once" error. I understand I can use different methods such as NOT IN with a subquery but I'd like to learn how can I adapt this script in PostgreSQL using LEFT JOIN approach. Thank you in advance.
UPDATE A
SET A.column_Name = 'Y'
FROM tbl_A A
LEFT JOIN tbl_B B
ON A.Mathcing_Column_Name = B.Matching_Column_Name
WHERE B.Matching_Column_Name is NULL
Use NOT EXISTS instead:
UPDATE tbl_A A
SET column_Name = 'Y'
WHERE NOT EXISTS (SELECT 1
FROM tbl_B B
WHERE A.Matching_Column_Name = B.Matching_Column_Name
);
Try this. (it's working)
with temp AS
(
SELECT A.* FROM tbl_A A
LEFT JOIN tbl_B B
ON A.Mathcing_Column_Name = B.Matching_Column_Name
WHERE B.Matching_Column_Name is NULL
)
UPDATE tbl_A C SET column_Name = 'Y'
from temp D
where C.id=D.id

Update a table column using a mapping table?

I have three tables: a, b, c. Table b is a mapping table for table a and table c. I am trying to update table a column = table c column.
When implementing the update, I receive an error:
[S0001][4104] The multi-part identifier could not be bound
UPDATE table_a
SET table_a.Sector = table_c.Sector
FROM table_a
INNER JOIN table_b
ON table_a.business_ID = cast(table_b.business_id as BIGINT)
INNER JOIN table_c
ON table_b.ACARA_SML_ID = table_c.ACARA_SML_ID
WHERE a.State = 'ABC';
Use UPDATE statement directly from SELECT clause :
UPDATE table_a SET table_a.Sector = C.Sector
FROM table_c C
WHERE EXISTS
( SELECT 1 FROM table_b B B.ACARA_SML_ID = C.ACARA_SML_ID AND
table_a.business_ID = CAST(B.business_id AS BIGINT)
) AND A.[STATE] = 'ABC'
In your WHERE clause you are using a.State, here a was not set as table alias for any of the table.
Can you try this query with correct table alias:
UPDATE A
SET Sector = C.Sector
FROM table_a A
INNER JOIN table_b B ON A.business_ID = CAST(B.business_id AS BIGINT)
INNER JOIN table_c C ON B.ACARA_SML_ID = C.ACARA_SML_ID
WHERE A.[STATE] = 'ABC';

Updating a key on table from another table in Oracle

I am trying to update a key on a table (t1) when the key value is (abc) by getting the value from table (t2).
It is working as expected when I am limiting it to a specific person
update table_a t1
set t1.u_key = (select t2.u_key
from table_b t2
where t2.name_f=t1.name_f
and t2.name_l=t1.name_l
and rownum<=1
and t2='NEVADA')
where t1.u_key = 'abc'
and e.name_f='Lori'
and e.name_l='U'
;
I initially tried without rownum and it said too many rows returned.
To run on all the data with t1.u_key='abc' and took out the specific name, I tried this which has been running until time out.
update table_a t1
set t1.u_key = (select t2.u_key
from table_b t2
where t2.name_f=t1.name_f
and t2.name_l=t1.name_l
and rownum<=1
and t2='NEVADA')
where t1.u_key = 'abc'
;
Can you please look at it and suggest what am I missing.
You should first take a look what is returned when you run the inner SELECT statement alone:
SELECT t2.u_key FROM table_b t2
WHERE t2.name_f IN (SELECT name_f FROM table_a WHERE u_key = 'abc')
AND t2.name_l IN (SELECT name_l FROM table_a WHERE u_key = 'abc')
AND t2='NEVADA'
Examine the results and you will see that there are more than one row returned.
If there should be only matching row per key, you would need to add the key to the inner SELECT as well but I can't tell you how it should look like without additional table descriptions and possibly some sample entries from table_a and table_b.
Use this:
update (
SELECT t2.u_key t2key,
t1.ukey t1key
FROM table_b t2,
table_a t1
where t2.name_f=t1.name_f
and t2.name_l=t1.name_l
and t2='NEVADA'
and rownum<=1 )
SET t1key = t2key
where t1key = 'abc';
merge into table_a t1
using(
select name_f, name_l, max(u_key) as new_key
from table_b t2
where t2='NEVADA'
group by name_f, name_l
) t2
on (t1.name_f=t2.name_f and t1.name_l=t2.name_l and t1.u_key='abc')
when matched then
update set t1.u_key=t2.new_key

Update table values from another table with the same user name

I have two tables, with a same column named user_name, saying table_a, table_b.
I want to, copy from table_b, column_b_1, column_b2, to table_b1, column_a_1, column_a_2, respectively, where the user_name is the same, how to do it in SQL statement?
As long as you have suitable indexes in place this should work alright:
UPDATE table_a
SET
column_a_1 = (SELECT table_b.column_b_1
FROM table_b
WHERE table_b.user_name = table_a.user_name )
, column_a_2 = (SELECT table_b.column_b_2
FROM table_b
WHERE table_b.user_name = table_a.user_name )
WHERE
EXISTS (
SELECT *
FROM table_b
WHERE table_b.user_name = table_a.user_name
)
UPDATE in sqlite3 did not support a FROM clause for a long time, which made this a little more work than in other RDBMS. UPDATE FROM was implemented in SQLite 3.33 however (2020-08-14) as mentioned at: https://stackoverflow.com/a/63079219/895245
If performance is not satisfactory, another option might be to build up new rows for table_a using a select and join with table_a into a temporary table. Then delete the data from table_a and repopulate from the temporary.
Starting from the sqlite version 3.15 the syntax for UPDATE admits a column-name-list
in the SET part so the query can be written as
UPDATE table_a
SET
(column_a_1, column_a_2) = (SELECT table_b.column_b_1, table_b.column_b_2
FROM table_b
WHERE table_b.user_name = table_a.user_name )
which is not only shorter but also faster
the last "WHERE EXISTS" part
WHERE
EXISTS (
SELECT *
FROM table_b
WHERE table_b.user_name = table_a.user_name
)
is actually not necessary
It could be achieved using UPDATE FROM syntax:
UPDATE table_a
SET column_a_1 = table_b.column_b_1
,column_a_2 = table_b.column_b_2
FROM table_b
WHERE table_b.user_name = table_a.user_name;
Alternatively:
UPDATE table_a
SET (column_a_1, column_a_2) = (table_b.column_b_1, table_b.column_b_2)
FROM table_b
WHERE table_b.user_name = table_a.user_name;
UPDATE FROM - SQLite version 3.33.0
The UPDATE-FROM idea is an extension to SQL that allows an UPDATE statement to be driven by other tables in the database. The "target" table is the specific table that is being updated. With UPDATE-FROM you can join the target table against other tables in the database in order to help compute which rows need updating and what the new values should be on those rows
There is an even much better solution to update one table from another table:
;WITH a AS
(
SELECT
song_id,
artist_id
FROM
online_performance
)
UPDATE record_performance
SET
op_song_id=(SELECT song_id FROM a),
op_artist_id=(SELECT artist_id FROM a)
;
Update tbl1
Set field1 = values
field2 = values
Where primary key in tbl1 IN ( select tbl2.primary key in tbl1
From tbl2
Where tbl2.primary key in tbl1 =
values);
The accepted answer was very slow for me, which is in contrast to the following:
CREATE TEMPORARY TABLE t1 AS SELECT c_new AS c1, table_a.c2 AS c2 FROM table_b INNER JOIN table_a ON table_b.c=table_a.c1;
CREATE TEMPORARY TABLE t2 AS SELECT t1.c1 AS c1, c_new AS c2 FROM table_b INNER JOIN t1 ON table_b.c=t1.c2;