使用Microsoft SQL Server从SELECT更新

时间:2016-08-24 09:21:13

标签: sql-server sql-update sql-server-2014

我的问题是:

我有2个表:Employees(它有FirstNameLastNameEmployeeId列)和ProjectWorkers(它有FullNameWorkerId列)。

WorkerIdEmployeeId相同,因此我需要使用ProjectWorkers.FullNameEmployees.FirstName数据的加入数据填充LastName(根据WorkerId=EmployeeId })。

我编写了一个填充FullName列的查询,但只有FirstNames,但我不明白如何加入FirstNameFullName(它有简单但对我来说这是一个问题。)

我的查询:

USE [HRM_MVC_DEV_Test]
GO

UPDATE [dbo].[ProjectWorkers] set
    FullName = FirstName
        FROM [dbo].[Employees] as e
        INNER JOIN [dbo].[ProjectWorkers] as p
        ON e.EmployeeId = p.WorkerId
GO

P.S。感谢格式化文本,下次我会尝试自己动手)

2 个答案:

答案 0 :(得分:5)

使用+CONCAT(从SQL Server 2012开始):

USE [HRM_MVC_DEV_Test]
GO

UPDATE p
SET FullName = FirstName + ' ' + LastName
FROM [dbo].[ProjectWorkers] as p
INNER JOIN [dbo].[Employees] as e
    ON e.EmployeeId = p.WorkerId
GO

另一种方式:

SET FullName = CONCAT(FirstName,' ',LastName)

答案 1 :(得分:2)

       UPDATE ProjectWorkers SET 
       ProjectWorkers.FullName = Employees.FirstName FROM
       ProjectWorkers INNER JOIN Employees ON
       ProjectWorkers.EmployeeId = EmployeesON.EmployeeId