簡體   English   中英

比較兩個SQL表(完全相同的列)添加缺少的行

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

我有兩個完全相同的格式的表。 我想將這兩個表相互比較,如果表A沒有表B的記錄,那么我想將該列添加到表A。如何使用SQL來實現這一點。 我只想比較以下列中的表CompanyNumber,EmployeeNumber,StatusFlag和Initials。

使用SQL的最佳方法是什么?

這個查詢有什么問題?

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

錯誤如下:

 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.

您可以使用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....)

認為一個簡單的方法可能是:

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