简体   繁体   English

比较两个SQL表(完全相同的列)添加缺少的行

[英]Compare Two SQL Tables (Exact Same Columns) Add Missing Rows

I have two tables that are in the exact same format. 我有两个完全相同的格式的表。 I want to compare both of the these tables to each other and if table A does not have a record that table B has then I want to add that column to Table A. How can I achieve this using SQL. 我想将这两个表相互比较,如果表A没有表B的记录,那么我想将该列添加到表A。如何使用SQL来实现这一点。 I only want to compare the tables on the following columns CompanyNumber, EmployeeNumber,StatusFlag, and Initials. 我只想比较以下列中的表CompanyNumber,EmployeeNumber,StatusFlag和Initials。

What is the best way to do this using SQL? 使用SQL的最佳方法是什么?

What Is Wrong with this query? 这个查询有什么问题?

MERGE EmployeeMaster AS Dest
USING ( SELECT * FROM EmployeeMasterCopy ) AS Src
JOIN  Dest.CompanyNumber = Src.CompanyNumber
and Dest.EmployeeNumber = Src.EmployeeNumber
and Dest.StatusFlag = Src.StatusFlag
and Dest.Initials = Src.Initials
WHEN NOT MATCHED THEN

   INSERT INTO EmployeeMaster(CompanyNumber, FirstName, LastName, FullName, Branch, DepartmentNumber, Initials, 
                                    JobTitle, StartDate, EmployeeNumber, EmployeeType,
                                    PayType, Rate, UnionNo, G2ID, EnterTimeFl, StatusFlag, CreateBy, CreateDate, MiddleName,ManagerEmpNo, NextReviewDate, UserName, TempFl, PrimaryDivision,PEWFl,PGTFl,PMPFl,PPGEFl,PPGFl,PRCFl,PTCFl,PPFl,SWPFl,ReviewPeriod, CorpFl, NickName, RateEffectiveDate, VacationBalance, SickBalance, TempEmployeeType, BusinessSegment)
        VALUES (Src.CompanyNumber, Src.FirstName, Src.LastName,Src.LastName + ', ' + Src.FirstName, Src.Branch, Src.DeptNo, upper(Src.Initials), Src.JobTitle, Src.StartDate, Src.EmployeeNumber,
                Src.EmployeeType, Src.PayType, Src.Rate, Src.UnionNo, Src.G2ID, Src.EnterTimeFl, 1, 'AJOHNSON', GETDATE(), Src.MiddleName, Src.ManagerEmpNo, Src.NextReviewDate, Src.UserName, Src.TempFl, Src.PrimaryDivision,Src.PEWFl,Src.PGTFl,Src.PMPFl,Src.PPGEFl,Src.PPGFl,Src.PRCFl,Src.PTCFl,Src.PPFl,Src.SWPFl,Src.ReviewPeriod, Src).CorpFl, Src.NickName,Src.RateEffectiveDate ,
    Src.VacationBalance ,
    Src.SickBalance ,
    Src.TempEmployeeType, Src.BusinessSegment

Here are the errors: 错误如下:

 Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'as'.
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'AS'.
Msg 128, Level 15, State 1, Line 13
The name "Src.CompanyNumber" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.

you can use MERGE 您可以使用MERGE

MERGE TableA as Dest
USING ( select * from TableB ) as Src
ON Dest.CompanyNumber = Source.companyNumber
and Dest.EmployeeNumber = Source.EMployeeNumber
and Dest.StatusFlag = Source.StatusFlag
and Dest.initials = Source.initials
WHEN NOT MATCHED then
   INSERT (CompanyNumber, EmployeeNumber, StatusFlag, initials, ...)
  values ( Src.CompanyNumber, Src.EmployeeNumber, Src.StatusFlag, Src.initials....)

Think a simple way could be: 认为一个简单的方法可能是:

select
   TableA.CompanyNumber as "Tab A Company No",
   TableB.CompanyNumber as "Tab B Company No",
   TableA.EmployeeNumber as "Tab A Employee No",
   TableB.EmployeeNumber as "Tab B Employee No",
   TableA.StatusFlag as "Tab A Status Flag",
   TableB.StatusFlag as "Tab B Status Flag",
   TableA.Initials as "Tab A Initials",
   TableB.Initials as "Tab B Initials",
from
   TableA,
   TableB

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM