DROP TABLE #Member SELECT A.FMobilePhone,A.FName,A.FMemId AS FNewMemId INTO #Member FROM TMember AS A,( SELECT FMobilePhone,FName,MIN(FTime) AS FTime FROM TMember WHERE FMobilePhone != '' GROUP BY FMobilePhone,FName HAVING(SUM(1) >1)) AS B WHERE A.FMobilePhone = B.FMobilePhone AND A.FName = B.FName AND A.FTime = B.FTime ALTER TABLE TMember ADD FSeq INT IDENTITY (1,1) ALTER TABLE TMember ADD FTime DATETIME UPDATE TMember SET FTime = DATEADD(MINute,FSeq,FCreateDate ) ALTER TABLE TMember ADD FNewMemId BIGINT UPDATE TMember SET FNewMemId = FMemId UPDATE A SET A.FNewMemId = B.FNewMemId FROM TMember AS A,#Member AS B WHERE A.FMobilePhone = B.FMobilePhone AND A.FName = B.FName AND B.FMobilePhone != '' AND A.FNewMemId != B.FNewMemId SELECT * FROM #Member ORDER BY FMobilePhone UPDATE TMember SET FMemNO = FMemNo + 'زرة¾؛د²¢',FDeleted = 1 WHERE FMemId != FNewMemId UPDATE A SET A.FMemId= B.FNewMemID FROM TMemberCard AS A,TMember AS B WHERE A.FMemId = B.FMemId AND A.FMemId != B.FNewMemId AND B.FMemId != B.FNewMemId UPDATE A SET A.FMemId= B.FNewMemID FROM TMemberCardAccount AS A,TMember AS B WHERE A.FMemId = B.FMemId AND A.FMemId != B.FNewMemId AND B.FMemId != B.FNewMemId UPDATE A SET A.FMemId= B.FNewMemID FROM TMemberCourse AS A,TMember AS B WHERE A.FMemId = B.FMemId AND A.FMemId != B.FNewMemId AND B.FMemId != B.FNewMemId UPDATE A SET A.FMemId= B.FNewMemID FROM TMemberCardAcctHist AS A,TMember AS B WHERE A.FMemId = B.FMemId AND A.FMemId != B.FNewMemId AND B.FMemId != B.FNewMemId UPDATE A SET A.FMemId= B.FNewMemID FROM TMemberCardOldAcctHist AS A,TMember AS B WHERE A.FMemId = B.FMemId AND A.FMemId != B.FNewMemId AND B.FMemId != B.FNewMemId UPDATE A SET A.FMemId= B.FNewMemID FROM TMemberOldTransHist AS A,TMember AS B WHERE A.FMemId = B.FMemId AND A.FMemId != B.FNewMemId AND B.FMemId != B.FNewMemId UPDATE A SET A.FMemId= B.FNewMemID FROM TMemberPointHist AS A,TMember AS B WHERE A.FMemId = B.FMemId AND A.FMemId != B.FNewMemId AND B.FMemId != B.FNewMemId UPDATE A SET A.FMemId= B.FNewMemID FROM TMemberArrear AS A,TMember AS B WHERE A.FMemId = B.FMemId AND A.FMemId != B.FNewMemId AND B.FMemId != B.FNewMemId