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;
Related
I have a main table (T1) which's to be joined with two tables (T2 and T3) and need a way to update one column from T1 with values from another table. I can't seem to find an update statement which does the update operation accurately.
SELECT
T1.VALUE --This value to be updated as New_value from New_table
FROM
TABLE1 T1, TABLE2 T2, TABLE3 T3
WHERE
T1.VALUE = 'Out_dated value' --This value to be updated from New_table
AND T1.VA_ID=T2.VA_ID
AND T1.SI_ID=T3.SI_ID;
It would help if you listed some sample data and expected results. But I think you want something like this:
update table1 t1
set t1.value = (select new_value from new_table where old_value = t1.value)
where exists (SELECT 1
FROM
TABLE2 T2, TABLE3 T3
WHERE T1.VA_ID=T2.VA_ID
AND T1.SI_ID=T3.SI_ID)
You didn't show the column names for NEW_TABLE, I'm assuming here that they're NEW_VALUE and OLD_VALUE.
[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
Any method to do this?
Table1
1
2
3
4
5
Table2
3 (with the condition)
4 (without the condition)
I want to:
Select all records from Table1 if it exists in Table 2, where...(condition)
Select all records from Table1 if it not exists in Table2
Combine both select results. Sort all results with their created date.
For example, the result should be:
Result
1
2
3
5
Hopefully this can help.
SELECT t1.* from table1 t1
JOIN table2 t2
ON t1.ID = t2.ID
UNION ALL
SELECT t1.* from table1 t1 where ID in
(
SELECT t2.ID from table1 t1 except Select t2.ID from table2 t2
)
ORDER BY t1.CreatedDate
You can achieve this by doing:
SELECT t1.id
FROM Table1 t1
LEFT JOIN Table2 t2 on t1.id = t2.id
WHERE condition OR t2.id IS NULL
ORDER BY t1.CreatedDate;
See fiddle (I assumed condition to be t2.id!=4, but it can be anything else depending on other data in your tables).
There could be multiple solution.
One way
we can get the result set using two different queries and at last combine both of the result-set using UNION
Another way,
First statement is saying that get all the result set from TABLE1 if it exists in TABLE2 as well with some criteria (condition in where clause)
means using INNER JOIN we can achieve this
Second statement is saying get all the result set from TABLE1 which are not present in TABLE2
means along with INNER JOIN ed query also include the TABLE1's data if not present in TABLE2
here we can take the help of LEFT OUTER JOIN (taking TABLE1 on the left side)
Assumption: (condition: t1.Id != 4)
Let's try to understand the query using both of the above mentioned ways
---- -- --Step1 Create table and insert records
---- create table1 with Id int identity columsn
--CREATE TABLE Table1 (Id INT IDENTITY(1,1), CreatedDate smalldatetime default(getdate()));
--go
---- insert 1st 5 integers into Table1
--INSERT INTO Table1 DEFAULT VALUES
--go 5
---- create Table2 with Id int column
--CREATE TABLE Table2 (Id INT , CreatedDate smalldatetime default(getdate()));
--go
---- insert records 3,5 into Table2
--INSERT INTO Table2(Id) VALUES (3), (4);
-- -- -- Solution: one way
; WITH cteMyFirstResult AS
(
-- 2.1. Select all records from Table1 if it exists in Table 2, where...(condition)
SELECT
Id, CreatedDate
FROM Table1 AS t1
WHERE t1.Id IN (SELECT Id FROM Table2 AS t2)
AND t1.Id != 4 -- assumption it can be any condition
),cteMySecondResult AS (
-- 2.2. Select all records from Table1 if it not exists in Table2
SELECT
Id, CreatedDate
FROM Table1 AS t1 WHERE t1.Id NOT IN (SELECT Id FROM Table2 AS t2)
)
-- 2.3. Combine both select results. Sort all results with their created date.
SELECT
Id, CreatedDate
FROM cteMyFirstResult
UNION
SELECT
Id, CreatedDate
FROM cteMySecondResult
ORDER BY CreatedDate;
-- -- Solution: Another way (with bug)
SELECT t1.Id, t1.CreatedDate
FROM Table1 AS t1
LEFT JOIN Table2 AS t2 on t1.id = t2.id
WHERE t1.Id != 4
Order by T1.CreatedDate;
-- in this query we are using the criteria after doing the join operation.
-- thus after filtering out the result set based on JOIN Condition this condition will get applied
-- and if there is any null record in the Table1 for column Id (used in join) will not come in the final result-set
-- to avoid this we can include NULL check along with our criteria
-- -- Solution: Another way
SELECT t1.Id, t1.CreatedDate
FROM Table1 AS t1
LEFT JOIN Table2 AS t2 on t1.id = t2.id
WHERE ( t1.Id != 4 ) OR t1.Id IS NULL -- include all your criteria within small-barcket)
Order by T1.CreatedDate;
Thanks for all responses.
I come out with the answer I want:
SELECT *
FROM Table1 t1
WHERE NOT EXISTS(SELECT 1 FROM Table2 t2
WHERE t1.ID = t2.ID
AND t2.CIF_KEY = #CifKey
AND t2.STATUS <> ''3'')
AND (condition in where clause)
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
Situation:
I have two tables
Table 1 always has records
Table 2 is the result of a select statement and may or may not have records
Desired Results:
If Table 2 has ANY records, I want only matching records from Table 1. Otherwise, I want all records from Table 1.
I realize I can do this:
DECLARE #count int
SELECT #count=COUNT(*) FROM Table2
IF #count>0
SELECT t1.* FROM Table1 t1 INNER JOIN Table2 t2 ON t1.id=t2.id
ELSE
SELECT * FROM Table1
However, I am trying to avoid IF statements if possible.
Is that even possible?
select t1.*
from Table1 t1
left join
Table2 t2
on t1.id = t2.id
where t2.id is not null -- Match found
or not exists -- Or Table2 is empty
(
select *
from Table2
)