Update multiple rows in SQL Server with different table - sql

I have two tables let's say tables A and B. Each table has two columns ID and Name but the data in column Name of table A and B is different, but ID are the same, so I want to update column Name in table A with values from column Name in table B. How to achieve this? If any could be helpful

update tbl1 set tbl1.Name = tbl2.Name
from Table1 as tbl1
inner join Table2 as tbl2 on tbl1.Id = tbl2.Id
this is what your are looking for

Try with UPDATE using JOIN
UPDATE TableA
SET TableA.Name= TableB.Name
FROM TableA INNER JOIN TableB
ON TableA.ID= TableB.ID

Here you can do as join
UPDATE table_a
SET table_a.Name = table_b.Name
FROM table_a
INNER JOIN table_b ON table_a.id = table_b.id

Related

SQL Self Join and take a record by a condition on a third join

I have a query where I'm doing a self join on TableA on 'Id1' and 'Id2'. I then do an inner join on TableB. The table with the self join as 2 keys (Id1,Id2). I'm trying to get the records that do not match up with the Id in Table3. Is it a condition that I need to add on the join? Thanks!
SELECT *
FROM TableA t1
JOIN TableA t2 ON t1.Id1 = t2.Id2
JOIN TableB t3 ON t1.Id1 = t3.Id
EDIT :
SELECT *
FROM UCDetails ucd1
JOIN MS et ON ucd1.UKey = TRY_CAST(et.SubKey AS bigint)
JOIN dbo.UCDetails ucd2 ON ucd1.UKey = ucd2.ETTSubkey
WHERE ucd1.ETTSubkey IS NULL
Sorry, I should of been more specific, so here is the query. What I want is to take only the record from ucd1 or ucd2 where if the ucd1 ETTSubkey IS NULL and the 'UKey' DOES NOT match the 'et.Subkey'. I can pull the records with this join but I only want to take the 'UCD' record where there isn't a match with the 'Subkey' on the et table.
If you want rows where either id does not match in tableB, then use not exists:
select a.*
from tableA a
where not exists (select 1
from tableB b
where b.id = a.id1
) or
not exists (select 1
from tableB b
where b.id = a.id2
) ;
I'm trying to get the records that do not match up with the Id in
Table3
If by match up you mean that TableB's Id is equal to either Id1 or Id2 in TableA, then you only need a LEFT join that filters out the matching rows:
SELECT a.*
FROM TableA a LEFT JOIN TableB b
ON b.Id IN (a.Id1, a.Id2)
WHERE b.Id IS NULL

MS Sql - Update table 1 based on table 2 and table 3

I want to update table 1 based on table 2 and table 3. Right now am updating it in two separate statements.
Is it possible to update table 1 first based from table 2 then for the NULLS to be updated from table 3 all in a single update using join.
I have tried to build a query but where to include
tbl1.col1=tbl3.col1
update tbl1
set tbl1.col1= tbl2.col1
from table1 tbl1
left JOIN table2 tbl2
ON tbl1.col = tbl2.col
left JOIN table3 tbl3
on tbl1.col=tbl3.col and tbl2.col<>tbl3.col
I think you just want coalesce():
update tbl1
set col1 = coalesce(tbl2.col1, tbl3.col1)
from table1 tbl1 left join
table2 tbl2
on tbl1.col = tbl2.col left join
table3 tbl3
on tbl1.col = tbl3.col and tbl2.col <> tbl3.col;
Use COALESCE or ISNULL both returns the first NOT NULL value
Try this
UPDATE tbl1
SET tbl1.col1 = COALESCE(tbl2.col1, tbl3.col1) -- ISNULL(tbl2.col1, tbl3.col1)
FROM table1 tbl1
LEFT JOIN table2 tbl2
ON tbl1.col = tbl2.col
LEFT JOIN table3 tbl3
ON tbl1.col = tbl3.col
AND tbl2.col <> tbl3.col

What is this before a column in SQL?

I'm new to SQL and I am seeing a select that states SELECT something.columnName FROM Table.
I'm just wondering what the "something" is used for?
If you select from multiple tables in a single query then you can specify the table and column name you want to select. Example:
select table1.col1, table1,col2, table2.col5
from table1
join table2 on t1.id = t2.id
You can also specify alias names for tables or columns to make things shorter or if you join the same tables twice. Example:
select t1.col1 as A, t1.col2 as B, t2.col5 as C
from veryLongTableName1 t1
join veryLongTableName2 t2 on t1.id = t2.id
That is either a table name or an alias for a table name.
When joining tables, you might need to specify which table a field should be fetched from. Example:
select TableA.Name, TableB.Name
from TableA
inner join TableB on TableB.id = TableA.id
Example with aliases:
select a.Name, b.Name
from TableA as a
inner join TableB as b on b.id = a.id
It is a alias to show form wich table the column comes. You need this if you join to tables wich have a colum with the same name.

Retrieving values from another table which has its relationship to the first table defined by a third table

Right now I have
SELECT
table1.field1 AS whatever
, table2.field2 AS stuff
FROM
table1
INNER JOIN table2 ON table1.goId = table2.Id
I need to be able to select a field3 which is located in a table3. Thing is, table1 has no direct foreign key pointing to table3, but it DOES have foreign key that points to a table which has another foreign key pointing to table3. How do I make the join between these 3 tables?
Given the lack of database structure provided the following could be used
SELECT
--Put column Declarations here--
table1.columnName1
,table2.columnName2
,table3.columnName3
FROM
table1
JOIN table2 ON table1.columnName1 = table2.columnName1
JOIN table4 ON table1.columnName1 = table4.columnName1
JOIN table3 ON table4.columnName4 = table3.columnName4
OR you could nest the third table
SELECT
--Put column Declarations here--
table1.columnName1
,table2.columnName2
,table3.columnName3
FROM
table1
JOIN table2 ON table1.columnName1 = table2.columnName1
JOIN table4
JOIN table3 ON table4.columnName4 = table3.columnName4
ON table1.columnName1 = table4.columnName1
Say that fourth table is table2.
SELECT
table3.column_You_want
FROM
table3
INNER JOIN table2 ON table2.column_linked_to_table3 = table3.column_name_linked_to_table2
INNER JOIN table1 ON table2.column_linked_to_table1 = table1.column_linked_to_table2

delete records from a table based on data from a different table

table1 has column CITY and COUNTRY. table2 has column CITY.
how do i delete from table2 all records that have CITY in common with table1 but also the COUNTRY='Russia' ??
please keep in mind that both tables have about 1 million rows of data
DELETE table2
FROM table2 INNER JOIN table1
ON table2.CITY = table1.CITY
WHERE table1.COUNTRY = 'Russia'
You can use the multitable delete syntax:
DELETE table2
FROM table1
JOIN table2
ON table1.city = table2.city
WHERE table1.country = 'RUSSIA'