Sql-server – Query to compare two column of table 1 with two column of table 2 and update the third column in table 1 as ‘y’ if the record matches

sql servertableupdate

I have two tables, Table 1 with column email , number, enabled and Table 2 with column email,number.
Now i have to compare the records of both the column from Table 1 and Table 2 , If the records matches in both the table then i have to update the column enabled as "Y" in table 1.
Please help me with the query.

Best Answer

One way would be

UPDATE t1
SET    t1.enabled = 'Y'
FROM   Table1 t1
WHERE  EXISTS (SELECT *
               FROM   Table2 t2
               WHERE  t1.email = t2.email
                      AND t1.number = t2.number);

Or a more concise alternative

UPDATE Table1
SET    enabled = 'Y'
FROM   Table2
WHERE  Table1.email = Table2.email
       AND Table1.number = Table2.number;