Insert column from one table to other using Join - sql-server

I need to insert 3 columns from one table to another, using JOIN by 3 fields: name, surname and age
I need update column status, status1 and status2 in table_2 with values from table_1
IF
table_1.name = table_2.name
table_1.surname = table_2.surname
table_1.age= table_2.age

UPDATE t2
SET
t2.[status]=t1.[status]
,t2.[status1]=t1.[status1]
,t2.[status2]=t1.[status2]
FROM [table_1] t1
INNER JOIN [table_2] t2
ON (t1.name=t2.name AND t1.surname=t2.surname AND t1.age=t2.age)
As you mentioned in comments that these table from different databases, then please change only the two line like.
FROM [yourDataBase1Name].[dbo].[table_1] t1
INNER JOIN [yourDataBase2Name].[dbo].[table_2] t2

Just update the table with Join.
it will be something like this:
UPDATE t2
SET t2.status = t1.status,
t2.status1 = t1.status1,
t2.status2 = t1.status2
FROM t2 JOIN t1 on (t1.first_name = t2.first_name AND t1.last_name = t2.last_name AND t1.age = t2.age);
Look here for more information:SQL update query using joins
SQL Fiddle: http://sqlfiddle.com/#!3/b3951/1

Related

Swap values of two columns of two tables

There are two tables T1 and T2 having columns like Id and Name.
How to swap name column values of both the tables on the basis of Id?
Create a temporary table with the values as same as the table T1 values. Then update the name column of table T1 with name column value of table T2. Then update the name column of table T2 with name column value of the temp table. After updating both the tables, drop the temp table.
Query
-- create temp table
select * into #temp from [T1];
-- update table T1
update t1
set t1.[Name] = t2.[Name]
from [T1] t1
join [T2] t2
on t1.[Id] = t2.[Id];
-- update table T2
update t1
set t1.[Name] = t2.[Name]
from [T2] t1
join #temp t2
on t1.[Id] = t2.[Id];
-- dop temp table
drop table #temp;
-- check both the tables
select * from [T1];
select * from [T2];
Create temp table with same schema as T1
populate it with T1 values
update T1 with T2 values
update T2 with temp table values
drop temp table
Sample code:
SELECT Id, Name INTO #tt FROM T1
UPDATE T1
SET T1.Name = T2.Name
FROM T1 INNER JOIN T2 ON T1.Id = T2.Id
UPDATE T2
SET T2.Name = #tt.Name
FROM T2 INNER JOIN #tt ON T2.Id = #tt.Id
DROP TABLE #tt
Just to be sure you could also add some error checking or transaction to avoid catastrophic results
Create a temporary column in T1, called tempName.
Copy names of T2 into tempName of T1 with corresponding id.
update T1
set tempName = (
select name
from T2
where T1.id = T2.id
);
Copy names of T1 into T2 with corresponding id:
update T2
set name = (
select name
from T1
where T1.id = T2.id
);
Copy the values of tempName to name in T1:
update T1
set name = tempName;
Drop tempName from T1;

I have two tables Table1 and Table2 :I want to swap name column value From Table1 to Table2 and Table2 to Table1

[Below are two tables Table1 and Table2][1]
Table1:Column names id,name
Table2:Column names id,name
After the swap ,the name column data of Table1 will reflect in Table2 and name of Table2 will reflect in Table1.
I tried to resolve the issue using below query:
update table1 t set t.name=replace(t.name,(select name from T1 where T1.id=t.id),(select name from T2 where T2.id = t.id));
update table2 t set t.name=replace(t.name,(select name from T2 where T2.id=t.id),(select name from T1 where T1.id = t.id));
But,it is not giving correct result.
You can't do this in a single pass, as you are overwriting the values you want to move in the next step. Here's a solution using a temporary table:
SELECT * INTO #temp FROM table1;
UPDATE t1 SET name = t2.name FROM table1 t1 INNER JOIN table2 t2 ON t2.id = t1.id;
UPDATE t2 SET name = t1.name FROM #temp t1 INNER JOIN table2 t2 ON t2.id = t1.id;
DROP TABLE #temp;
A little modification in your query and it worked.
create table temp1 as select * from table1;
update table1 set name = (select name from table2 where table2.id = table1.id);
update table2 set name = (select name from temp1 where temp1.id = table2.id);
Thanks,
Vinita Singh

SQL Server insert new rows ONLY based on multiple columns

I searched in SO but couldn't find anything for my purpose. I need to insert unique rows ONLY from one table into another. I have:
table1
id name bookid bookname start_date end_date rel_date rel_id
1 horror 1221 rockys 04/01/2016 04/30/2016 05/01/2016 4545
2 horror 1331 elm 04/01/2016 04/30/2016 05/01/2016 5656
table2
id name bookid bookname start_date end_date rel_date rel_id
1 horror 1221 rockys 04/01/2016 04/30/2016 05/01/2016 4545
2 horror 1441 elm 04/01/2016 04/30/2016 05/01/2016 5656
I need to insert into table1 the row with id = 2 in table2 AND also delete the row with id = 2 from table1, because bookid is different even though the rest of the columns match.
I tried following:
insert into table1
select * from table2
where not exists (select * from table2 where table1.id = table2.id
and table1.name = table2.name and table1.bookid = table2.bookid and
table1.bookname = table2.bookname and table1.start_date = table2.start_date
and table1.end_date = table2.end_date and table1.rel_date = table2.rel_date
and table1.rel_id = table2.rel_id)
Any way I can do all of this in one sql block?
In theory the following merge statement should achieve what you are looking for.
MERGE table1 [Target]
USING table2 [Source]
ON ([Target].[name] = [Source].[name]
AND
[Target].[bookname] = [Source].[bookname]
AND
[Target].[start_date] = [Source].[start_date]
AND
[Target].[end_date] = [Source].[end_date]
AND
[Target].[rel_date] = [Source].[rel_date]
AND
[Target].[rel_id] = [Source].[rel_id]
)
WHEN MATCHED AND ([Target].[bookid] <> [Source].[bookid]) THEN
UPDATE
SET [Target].[name] = [Source].[name]
,[Target].[bookid] = [Source].[bookid]
,[Target].[bookname] = [Source].[bookname]
,[Target].[start_date] = [Source].[start_date]
,[Target].[end_date] = [Source].[end_date]
,[Target].[rel_date] = [Source].[rel_date]
,[Target].[rel_id] = [Source].[rel_id]
WHEN NOT MATCHED THEN
INSERT(
[name]
,[bookid]
,[bookname]
,[start_date]
,[end_date]
,[rel_date]
,[rel_id]
)
VALUES
(
[Source].[name]
,[Source].[bookid]
,[Source].[bookname]
,[Source].[start_date]
,[Source].[end_date]
,[Source].[rel_date]
,[Source].[rel_id]
);
Note that there are some risks and limitations to this approach. If your [id] column has a uniqueness constraint, then it should be set as an identity column otherwise you will run into uniqueness violation errors. Also if [id] column value in table1 is different to [id] column in table2 then merge statement will keep the original [id] value from table1.
Basically this query simply updates your existing record in table1 with the matching record in table2 and insert new records from table2 into table1 if they don’t already exists.
All you should need to achieve your objective is this:
UPDATE T1
SET T1.bookid = T2.bookid
FROM Table1 T1
JOIN Table2 T2
ON T1.ID = T2.ID
However, to answer the question exactly as it was asked:
DELETE T1
FROM Table1 T1
JOIN Table2 T2
ON T1.ID = T2.ID
AND T1.bookid <> T2.bookid
INSERT INTO Table1
SELECT id, name, bookid, bookname, start_date, end_date, rel_date, rel_id
FROM Table2 T2
LEFT OUTER JOIN Table1 T1
ON T1.ID = T2.ID
AND T1.bookid = T2.bookid
WHERE T1.id IS NULL
Note that if your ID fields aren't unique, you'll need to add other conditions to the ON clauses.
If you are just concerned about updating the bookid value from table2, you can change the value of bookid with the below query
UPDATE t1 SET t1.bookid = t2.bookid
FROM table1 t1
JOIN table2 t2 ON t1.id = t2.id
If you think your id column is not unique in two tables, you might need to consider adding other matching columns in the JOIN.

Merging two columns with values from another table

I have similar table
How can i merge these two tables ? There are other columns, but these are the same.
How can i fill Table1 with values of Table2 or merge these tables ?
In Table2 is only 1 customer.
So the result table will have all customers with their values (Table1 will have Customer4 with Sales 50).
Thank you.
To update the table do
update t1
set t1.sales = t2.sales
from table1 t1
join table2 t2 on t1.customername = t2.customername
and as a select use
select t1.customername,
coalesce(t1.sales, t2.sales) as sales,
t1.date,
t1.variable1
from table1 t1
left join table2 t2 on t1.customername = t2.customername

SQL Update after Joining Two Tables

I am new to SQL, using Microsoft SQL Server Management Studio.
I am trying to write a SQL statement that performs an update after two tables are joined.
I have two tables: myTable1 and myTable2. Both share a field MyID, which is going to be the field that I join on. myTable1 contains a column called BitToUpdate. And MyTable2 contains a column called BitToCheck.
I want to set BitToUpdate in myTable1 to be 1 where BitToCheck in myTable2 is 1 as well.
Here is what I have:
SELECT M.MyID, BitToUpdate, BitToCheck
INTO #temp_table
FROM myTable1 as T1
LEFT JOIN myTable2 as T2
ON M.MyId = PO.MyId
So first I tried to join the two tables myTable1 and myTable2 on their IDs, and store the result in a temporary table.
Next, I want to update BitToUpdate to be 1 where BitToCheck is 1.
So to do that in the temporary table, I have:
UPDATE #temp_table
SET
`BitToUpdate` = 1
WHERE
`BitToCheck` = 1
This updates the BitToUpdate successfully in #temp_table. However, when I do a select on myTable1, I find that BitToUpdate is not changed. I suppose this makes sense as #temp_table isn't really a "pointer"....
But what would be the correct way to approach this join and update?
You don't need to use a LEFT JOIN here, since you are checking on a condition from table 2, so an INNER JOIN should be better here.
UPDATE T1
SET T1.BitToUpdate = 1
FROM myTable1 T1
INNER JOIN myTable2 T2
ON T1.MyId = T2.MyId
WHERE T2.BitToCheck = 1
What you are doing in your first query is updating a temp table named #temp. the updates never go to the actual table myTable1 or mayTable2. To update records while joining with other tables try this:
UPDATE T1
SET T1.BitToUpdate = 1
FROM myTable1 as T1
LEFT JOIN myTable2 as T2 (ON T1.MyId = T2.MyId)
WHERE T2.BitToCheck = 1
--SELECT M.MyID, BitToUpdate, BitToCheck
--INTO #temp_table
update t1
set t1.BitToUpdate = 1
FROM myTable1 as T1
LEFT JOIN myTable2 as T2
ON t1.MyId = t2.MyId
where t2.bittocheck = 1
UPDATE T1
SET BitToUpdate=1
FROM myTable1 T1
LEFT JOIN myTable2 T2
ON T1.MyId=T2.MyId
WHERE T2.BitToCheck=1

Resources