51

I have two tables in different databases on the same database server.

Both the databases have the same structure, but different data. Database1 (Test1) is the latest, and database2 (Test2) is an old copy of the database.

  • Test1 has a table called Employee with 3000 records
  • Test2 has a table called Employee with 1000 records

I need to update the table in Test1 from the same table in Test2 for a particular column called DeptID, because the values in the Employee table in the Test2 DB (the old one) have been updated. So I need to update the table in the new DB from the table in the old DB which has around 1000 rows.

In other words, I need to update the DeptID column in the Employee table in the Test1 DB with whatever values I have in the DeptID column in the Employee table in the Test2 DB.

I know I can restore the DB itself, but that's not a solution. I need to update the values in the Test1 database from the Test2 database.

2
  • Do you want just to update the values that are on both tables? or do you want those two tables to be identical?
    – Lamak
    Feb 7, 2011 at 12:41
  • Unfortunately, your question is not clear. I guess it is difficult for you to state your problem in English clearly enough, bit don't despair! You could help it by giving some examples. Post some 5-10 rows from each table, explain what should become what, and that may just be enough for us to understand what you are trying to accomplish.
    – Andriy M
    Feb 7, 2011 at 13:00

7 Answers 7

144

If the two databases are on the same server, you should be able to create a SQL statement something like this:

UPDATE Test1.dbo.Employee
SET DeptID = emp2.DeptID
FROM Test2.dbo.Employee as 'emp2'
WHERE
   Test1.dbo.Employee.EmployeeID = emp2.EmployeeID

From your post, I'm not quite clear whether you want to update Test1.dbo.Employee with the values from Test2.dbo.Employee (that's what my query does), or the other way around (since you mention the db on Test1 was the new table......)

3
  • 2
    Outstanding!! Simple, and it works (except I removed the quotes from emp2). Thanks so much!
    – Chiramisu
    Aug 29, 2011 at 19:21
  • 2
    Exactly what I was looking for. I've done this before using subqueries, but I knew there had to be a simpler way. I just want to add that I only wanted to update a subset of the records in one table based on corresponding values in another table, which can easily be done by adding a condition to the WHERE clause, e.g. WHERE Test1.dbo.Employee.EmployeeID = emp2.EmployeeID AND Test1.dbo.Employee.Department IN ('Sales','Marketing')
    – Adi Inbar
    Oct 23, 2013 at 16:13
  • 1
    But what will we do if both databases are on different server??
    – yash
    Apr 15, 2014 at 12:53
14
update t2
set t2.deptid = t1.deptid
from test1 t1, test2 t2
where t2.employeeid = t1.employeeid
2
  • 1
    Only difference here is this version is using an alias for t2.
    – Fandango68
    Mar 4, 2014 at 1:08
  • 1
    This worked great. For some reason, @marc_s answer did not work. Possibly because of quotes around the alias name? Jan 5, 2016 at 1:25
2
UPDATE table1
SET column1 = (SELECT expression1
               FROM table2
               WHERE conditions)
[WHERE conditions];
1
update test1 t1, test2 t2
set t2.deptid = t1.deptid
where t2.employeeid = t1.employeeid

you can not use from keyword for the mysql

0

Try a query like

INSERT INTO NEW_TABLENAME SELECT * FROM OLD_TABLENAME;
1
  • The question states different databases. You haven't taken that into account. Plus the answer already has an accepted answer which works. Furthermore look at formatting your query. Have a look at this.
    – Bugs
    Oct 27, 2016 at 12:40
0

this works wonders - no its turn to call this procedure form code with DataTable with schema exactly matching the custType create table customer ( id int identity(1,1) primary key, name varchar(50), cnt varchar(10) )

 create type custType as table
 (
 ctId int,
 ctName varchar(20)
 )

 insert into customer values('y1', 'c1')
 insert into customer values('y2', 'c2')
 insert into customer values('y3', 'c3')
 insert into customer values('y4', 'c4')
 insert into customer values('y5', 'c5')

 declare @ct as custType 
 insert @ct (ctid, ctName) values(3, 'y33'), (4, 'y44')
 exec multiUpdate @ct

 create Proc multiUpdate (@ct custType readonly) as begin
 update customer set  Name = t.ctName  from @ct t where t.ctId = customer.id
 end

public DataTable UpdateLevels(DataTable dt)
        {
            DataTable dtRet = new DataTable();
            using (SqlConnection con = new SqlConnection(datalayer.bimCS))
            {
                SqlCommand command = new SqlCommand();
                command.CommandText = "UpdateLevels";
                command.Parameters.Clear();
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@ct", dt).SqlDbType = SqlDbType.Structured;
                command.Connection = con;
                using (SqlDataAdapter dataAdapter = new SqlDataAdapter(command))
                {
                    dataAdapter.SelectCommand = command;
                    dataAdapter.Fill(dtRet);
                }
            }
}
-1
 UPDATE Employee SET Empid=emp3.empid 
 FROM EMP_Employee AS emp3
 WHERE Employee.Empid=emp3.empid

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.