我是 SQL Server 的新手。我必须将一个列的值从表 A 复制到另一个表 B 关于另一列(JOIN)但在复制之前我必须检查该值是否存在于另一个表 C 中。如果是,则复制,否则返回其记录值不在表 C 中。我的查询是;
IF EXISTS (SELECT Branch_ID FROM ADM_Branch
INNER JOIN UBL$ on ADM_Branch.Branch_Code = UBL$.[Branch Code ]
WHERE ADM_Branch.Branch_Code = [UBL$].[Branch Code] )
UPDATE EMP_Personal
SET Account_Number = UBL$.[Account ] , Bank_ID = 1 , Branch_ID = (select Branch_ID from ADM_Branch join UBL$ on ADM_Branch.Branch_Code = UBL$.[Branch Code ] where EMP_Personal.Emp_ID = UBL$.[Employee ID ])
FROM EMP_Personal JOIN UBL$
ON EMP_Personal.Emp_ID = UBL$.[Employee ID ]
ELSE
( SELECT UBL$.[Employee ID ],UBL$.[Name ],UBL$.[Account ],UBL$.[Branch Code ]
FROM UBL$) except ( SELECT UBL$.[Employee ID ],UBL$.[Name ],UBL$.[Account ],UBL$.[Branch Code ]
FROM UBL$
right join ADM_Branch on ADM_Branch.Branch_Code = UBL$.[Branch Code ])