如何在另一个表ssms中更新具有相同值的列?

时间:2015-09-03 09:35:20

标签: sql-server sql-server-2008

我有2张桌子 - 员工桌和Emplabor桌子 - 员工表:

Employee Table
    EMPLOYEEID (primary key)
      ,EMPLOYEENO
      ,EMPLOYEENAME
      ,LABORCODE
      ,LOGINNAME
      ,STARTDATE
      ,ADDRESS0 

Emplabor表:

EMPLABORID (primary key)
  ,LABORCRID
  ,EMPLOYEEID
  ,LABORCODE

我想更新Emplabor的Employee表中的LABORCODE列 这就是我所做的::

UPDATE dbo.EMPLOYEE
SET LABORCODE =dbo.EMPLABOR.LABORCODE
FROM dbo.EMPLABOR
WHERE dbo.EMPLOYEE.EMPLOYEEID=dbo.EMPLABOR.EMPLOYEEID

这项工作还可以更新我的所有员工的LABORCODE我已添加到数据库中,但是当我在Employee Table中添加一名新员工并从Emplabor Table中选择LABORCODE给该员工时,它就是dosn&#t; t在该员工的EMployee表中找到了LABORCODE

1 个答案:

答案 0 :(得分:0)

使用加入:

UPDATE emp
SET LABORCODE = empl.LABORCODE
FROM dbo.EMPLOYEE emp
JOIN dbo.EMPLABOR empl
 ON emp.EMPLOYEEID =  empl.EMPLOYEEID