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

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;

Related

How do I update the duplicate values and their foreign key in the child table?

I have 2 tables
Table A
Table B
Table A has 2 columns
ID, CellNo
Now Table B is the CHILD table of A.
In Table B, we have
ID, TableA_CellNO, TableA_ID.
I have duplicate CellNo's in table A and in Table B those two records exist but TableA_ID is updated with the wrong id's.
I need to Update all of the TableA_ID's with the correct values against the duplicate CellNo.
I have written a query to update by matching CellNo in both table but that doesn't work.
UPDATE TableA AS R
INNER JOIN TableB AS P
ON R.CellNo = P.TableA_CellNo
SET P.TableA_ID = R.ID
I'm assuming you want to update table A's Id to table B's latest Id, meaning that the last CellNo record inserted into B is the correct one.
This may not be the most efficient but it should work.
UPDATE a
SET a.Id = b.Id
FROM TableA a
JOIN TableB b
ON b.Id = a.Id
AND b.Id =
(
SELECT MAX(ib.Id) FROM TableB ib WHERE ib.CellNo = a.CellNo
);
You 've a syntax error there. I suppose you 're looking for
UPDATE P
SET P.TableA_ID = R.ID
FROM TableA R
JOIN TableB P ON R.CellNo = P.CellNo;
--WHERE <Some Conditions> if needed

SQL Get rows that doesn't appear in another table

I have this SQL problem: I have tables A and B. Table A has columns id and name, Table B amount and id which is a foreign key to table A.id.
I need to return all table A rows that don't have their id stored in table B. Any ideas?
So the complete opposite is:
SELECT *
FROM a
LEFT OUTER JOIN b ON a.id = b.id;
Here row what I need is left out of result
Just add a where clause:
SELECT a.*
FROM a LEFT OUTER JOIN
b
ON a.id = b.id
WHERE b.id IS NULL;
You can also use NOT EXISTS:
select a.*
from a
where not exists (select 1 from b where b.id = a.id);
In most databases, the two methods typically have similar performance.

Update fields with data from a second table

I have a database in PostgreSQL and I'm having some problems updating some values, let me explain.
I have three tables.
Table A:
id | itemID | Value
Table B:
Name | Value
Table C:
itemID | Name
I need to update the value field on table A with the value from table B where the itemId from the tables A and C are equal.
I don't really know how to explain, please ask me anything so I can explain it better
You need a join in the UPDATE statement:
update tablea
set "Value" = b."Value"
from tableb b inner join tablec c
on c.name = b.name
where c."itemID" = tablea."itemID";
See the demo.
You can use Update statement with a Common Table Expression, and bring the results directly by adding returning clause at the end.
with t0 as
(
select c.itemID , b.value
from tableC c
left join tableB b on b.name = c.name
)
update tableA a
set value = t0.value
from t0
where a.itemID = t0.itemID
returning a.*;
Demo

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

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!