我有 2 个表 'tbMemeberInfo' 和 'tbDocument'。
上传文档时,它会在 tbDocument 中记录一个字段 InfoID,即 tbMemeberInfo 中的 PK(MemberInfoID)。
然而,在 tbMemeberInfo 中有重复项。用户有一个“AgreementNo”和一个“IDNumber”,重复记录只包含 1 或其他。
我需要合并这些记录以将“AgreementNo”和“IDNumber”插入其中一条记录并删除另一条记录。
这是迄今为止我尝试过的表结构和代码......
tb会员信息
MemberInfoID AgreementNo IDNumber DOB Initials FirstName LastName Language Role CountryID Email
861616 124346665 NULL 1976-08-24 00:00:00.000 DV DAMIAN Example English Member 1 damian.example@mail.com
866185 NULL 7608241234123 1976-08-24 00:00:00.000 DV DAMIAN Example English Member 1 damian.example@mail.com
tb文档
DocumentID r_object_id DocumentTypeID UniqueDocumentNo ContentLength ContentType FileName CreatedUserID CreatedDate InfoID
293787 0900d431800bc987 13 PPS156329L 1753819 application/pdf Example_DV_PROV_APP_2009110316140300[1].pdf 362 2010-01-13 16:21:46.250 861616
293794 0900d431800bc998 530 PPS156335O 66750 image/tiff Example, DV DRS REPORT.tif 362 2010-01-13 16:26:48.420 861616
SQL 代码
DECLARE
@MemberInfoID int
,@AgreementNo varchar(50)
,@IDNumber varchar
,@DOB datetime
,@TitleID int
,@FirstName varchar(150)
,@LastName varchar(150)
,@ModifiedDate datetime
SELECT @AgreementNo = AgreementNo, @IDNumber = IDNumber, @FirstName = FirstName, @LastName = LastName, @DOB = DOB
FROM tbMemberInfo mi
INNER JOIN tbDocument d
ON mi.MemberInfoID = d.InfoID
WHERE (mi.AgreementNo = '') OR (mi.IDNumber = '')
--SELECT @IDNumber = IDNumber From tbMemberInfo mi
--INNER JOIN tbDocument d
--ON mi.MemberInfoID = d.InfoID
--WHERE (mi.AgreementNo = '')
--SELECT @AgreementNo = AgreementNo From tbMemberInfo mi
--INNER JOIN tbDocument d
--ON mi.MemberInfoID = d.InfoID
--WHERE (mi.IDNumber = '') AND (FirstName = @FirstName) AND (LastName = @LastName) AND (DOB = @DOB)
UPDATE tbMemberInfo
SET [IDNumber] = @IDNumber, [AgreementNo] = @AgreementNo, ModifiedDate = GETDATE()
FROM tbMemberInfo mi
INNER JOIN tbDocument d
ON mi.MemberInfoID = d.InfoID
WHERE (IDNumber = '') OR (AgreementNo = '') AND (FirstName = @FirstName)
AND (LastName = @LastName) AND (DOB = @DOB)
GROUP BY MemberInfoID
这些都不起作用。它将“7”放入所有 IDNumber 列。关于如何做到这一点的任何想法。我还没来得及删除重复项,我想先合并。