将一列移动到另一列

时间:2014-11-07 13:50:19

标签: sql sql-server sql-server-2008 sql-update

我有桌子

Employee 
{
EmpId,
FirstName,
LastName,
DeptId
}

Department
{
DeptId,
DeptName,
Specialization
}

现在我已将专业化列添加到Employee,所以现在新的Employee表是:

Employee 
{
EmpId,
FirstName,
LastName,
DeptId,
Specialization
}

现在我想要更新脚本将专业化数据从Department移到Employee

2 个答案:

答案 0 :(得分:1)

您可以使用update-join语法:

UPDATE e
SET    e.Specialization = d.Specialization
FROM   Employee e
JOIN   Department d ON e.DeptId = d.DeptId

答案 1 :(得分:0)

您可以使用简单的嵌套查询;

Update Employee Ee
Set    Specialization = (SELECT Specialization
                         FROM   Department Dpt
                         Where  Dpt.DeptId = Ee.DeptId)