How to update data in one table from corresponding data in another table in SQL Server 2005

前端 未结 8 1777
谎友^
谎友^ 2020-11-30 00:24

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

相关标签:
8条回答
  • 2020-11-30 00:52
     UPDATE Employee SET Empid=emp3.empid 
     FROM EMP_Employee AS emp3
     WHERE Employee.Empid=emp3.empid
    
    0 讨论(0)
  • 2020-11-30 00:59

    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......)

    0 讨论(0)
  • 2020-11-30 01:02

    Try a query like

    INSERT INTO NEW_TABLENAME SELECT * FROM OLD_TABLENAME;
    
    0 讨论(0)
  • 2020-11-30 01:03
    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 讨论(0)
  • 2020-11-30 01:06

    use test1

    insert into employee(deptid) select deptid from test2.dbo.employee

    0 讨论(0)
  • 2020-11-30 01:10
    update t2
    set t2.deptid = t1.deptid
    from test1 t1, test2 t2
    where t2.employeeid = t1.employeeid
    
    0 讨论(0)
提交回复
热议问题