使用set更新/设置多个列并选择语句

时间:2017-02-03 05:41:33

标签: sql-server database

我有下面的触发器写入,它对单列更新(firstname)完全正常,但是我们现在需要更新一些额外的列(lastname,mobileNumber等)

CREATE TRIGGER apl.updateJaWithCandidateDataAfterInsert ON [APL].[tblJobApplications_v2]
AFTER INSERT
AS
SET NOCOUNT ON

UPDATE [APL].[tblJobApplications_v2]
SET FirstName = (
        SELECT FirstName
        FROM [USR].[tblUserDetails]
        WHERE inserted.CandidateId = [USR].[tblUserDetails].CandidateId
        )
FROM [APL].[tblJobApplications_v2]
INNER JOIN Inserted ON [APL].[tblJobApplications_v2].CandidateId = Inserted.CandidateId
GO

尝试了一些选择,但无法使这些工作。有人可以指出我正确的方向吗?

1 个答案:

答案 0 :(得分:0)

[USR].[tblUserDetails]

可能还需要一个内连接
UPDATE [APL].[tblJobApplications_v2]
SET FirstName = [USR].[tblUserDetails].FirstName,
    LastName  = [USR].[tblUserDetails].LastName
FROM [APL].[tblJobApplications_v2]
INNER JOIN Inserted ON [APL].[tblJobApplications_v2].CandidateId = Inserted.CandidateId
INNER JOIN [USR].[tblUserDetails] ON inserted.CandidateId = [USR].[tblUserDetails].CandidateId