Update a table column using a mapping table? - sql

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';

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.

How to insert into a SQL table column using SELECT query?

I have tables A, B, and C.
I want to insert UserRoleName values in table C, into UserRoleName column in table A for the corresponding UserId. UserId in Table A, is a foreign key from Table B. UserRoleId is a foreign key in table B from table C. How can I do that?
I tried using this query,
insert into A (UserRoleName)
(
select C.UserRoleName
from C
inner join B on B.UserRoleId = C.UserRoleId
inner join A on A.UserId = B.UserId
)
But this is giving:
Cannot insert the value NULL into column 'Z
which is a non-nullable in column A. I didn't specify that column in picture of the table A though. Can you help me figure out what I am doing wrong?
You need to use update as follows:
Update a
Set a.userrolename = c.rolename
From b join c on b.userroleid = b.userroleid and b.userid = a.userid
Where a.userrolename is null
Or using standard sql (subquery) as follows:
Update a
Set a.userrolename =
(Select c.userrolename
From b join c on b.userroleid = c.userroleid
Where b.userid = a.userid)
Where a.userrolename is null;

How to join two tables in PL/SQL without creating a new one

I'm trying to join two tables in PL/SQL but I don't want to create another table. I would like to use "ALTER TABLE".
I want to alter my old table and add one column from another table. I've tried to use alter table and create the new column, which is the one from the second table.
ALTER TABLE TAB1
ADD VAR1 VARCHAR(30);
UPDATE TAB1 A
SET A.VAR1 = B.VAR1
WHERE EXISTS (SELECT VAR1 FROM TAB2 B WHERE A.ID = B.ID)
The error message is in the "B.VAR1".
update tab1 a set
a.var1 = (select b.var1
from tab2 b
where b.id = a.id
)
where exists (select null
from tab2 b
where b.id = a.id
);
Or:
merge into tab1 a
using (select b.id, b.var1
from tab2 b
) x
on (x.id = a.id)
when matched then update set
a.var1 = x.var1;

Sql Subquery result

I have 3 tables, A, B,C
Table A,Columns: latlong, name
Table B, columns : code, name
Table C, Columns : latlong, code
I want to update table A,column Name with the values from Table B, Column Name, something like:
update A set A.name =
(select b.name
from B where code = c.code)
where a.latlong = c.latlong
Note that all the columns are not related.
Would appreciate the right direction to go about it.
Have tried Sub queries with inner joins, but no good.
You can do this with an update using join:
update a
set name = b.name
from a join
c
on c.latlong = a.latlong join
b
on b.code = c.code;
Try update with INNER JOIN
update A set
A.name = B.name
FROM A
INNER JOIN C on C.latlong = A.latlong
INNER JOIN B on B.code = C.code
You have mentioned in your question the following:
I want to update table A,column Name with the values from Table B, Column Name
But what I can see from your query is that actually , you need only those values of the column Name of table B which has same value of code as in table C, and that your latlong in A should be the same as latlong in C, if I'm not mistaken.
Based on that, I can say you need an SQL JOIN operation for Tables B and C with table A. Something like this:
UPDATE A SET A.name = B.Name
FROM A
JOIN C
ON C.latlong = A.latlong
JOIN B
ON B.code = C.code
No need to create a SUBQUERY
Last condition is missing where Table A.Latlong = C.Latlong to pick up the correct code!

How to update with inner join in Oracle

Could someone please verify whether inner join is valid with UPDATE statment in PL SQL?
e.g.
Update table t
set t.value='value'
from tableb b inner join
on t.id=b.id
inner join tablec c on
c.id=b.id
inner join tabled d on
d.id=c.id
where d.key=1
This synthax won't work in Oracle SQL.
In Oracle you can update a join if the tables are "key-preserved", ie:
UPDATE (SELECT a.val_a, b.val_b
FROM table a
JOIN table b ON a.b_pk = b.b_pk)
SET val_a = val_b
Assuming that b_pk is the primary key of b, here the join is updateable because for each row of A there is at most one row from B, therefore the update is deterministic.
In your case since the updated value doesn't depend upon another table you could use a simple update with an EXIST condition, something like this:
UPDATE mytable t
SET t.VALUE = 'value'
WHERE EXISTS
(SELECT NULL
FROM tableb b
INNER JOIN tablec c ON c.id = b.id
INNER JOIN tabled d ON d.id = c.id
WHERE t.id = b.id
AND d.key = 1)
update t T
set T.value = 'value'
where T.id in (select id from t T2, b B, c C, d D
where T2.id=B.id and B.id=C.id and C.id=D.id and D.key=1)
-- t is the table name, T is the variable used to reffer to this table