February 7, 2010 at 9:25 pm
Hi,
we have two tables employee and employee1 where it has data like
Employee:
id name sal
1 A 1000
2 B 2000
Employee1:
id name sal
1 A 3000
2 B 4000
both has same structure but the second table data is correct one in case of salaries. so it is found that table1(employee) has mis lead data so i want to udpate the data from table2 to table 1 how can i do that?
February 7, 2010 at 11:40 pm
This is basic SQL stuff isnt it? you should've given it a try.
Not getting into the details of why would they keep two similar tables, here goes a way of updating it.
CREATE TABLE #TABLE1 ( id int, name varchar(10), salary int)
CREATE TABLE #TABLE2 ( id int, name varchar(10), salary int)
INSERT INTO #TABLE1 VALUES(1,'A', 1000)
INSERT INTO #TABLE1 VALUES(2,'B', 2000)
INSERT INTO #TABLE2 VALUES(1,'A', 3000)
INSERT INTO #TABLE2 VALUES(2,'B', 4000)
UPDATE T1
SET T1.Salary = T2.SALARY --Select *
FROM #TABLE1 T1
JOIN #TABLE2 T2
ON T1.id = T2.id
AND T1.Name = T2.Name
Select * from #Table1
---------------------------------------------------------------------------------
February 8, 2010 at 3:20 am
Its interview question i did with cursor.
Thanks nabha
-Ashok
February 8, 2010 at 5:36 am
why would you use cursor here when a simple update can do the job (as explained by Nabha)
any ways, did you get the job?
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply