use jinan_jumei USE JINAN_MEISHANG SELECT * FROM TCompany UPDATE TCompany SET FDeleted = 1,FNo = FNo + '已删' WHERE FNo = '003' SELECT * FROM TMemberCard WHERE FCompId = 3472331387795668992 DROP TABLE #Card --有跨店充值的卡,更新为充值公司 SELECT C.FCardId,C.FCardNo ,C.FCompId AS FOldCompId,CAST(0 AS BIGINT) AS FNewCompId INTO #Card FROM TMemberSaleCard AS A,TMemberSaleBill AS B,TMemberCard AS C WHERE A.FBillId = B.FBilliD AND B.FState = 1 AND C.FCardId = A.FCardId AND ((B.FCompId = 3472331387795668992 AND C.FCompId != 3472331387795668992) OR (B.FCompId != 3472331387795668992 AND C.FCompId = 3472331387795668992)) GROUP BY C.FCardId,C.FCardNo ,C.FCompId DECLARE @CardId BIGINT DECLARE @CardNo VARCHAR(300) DECLARE @OldCompId BIGINT DECLARE @NewCompId BIGINT DECLARE List CURSOR FOR SELECT A.FCardId,A.FOldCompId FROM #Card AS A OPEN List FETCH List INTO @CardId,@OldCompId WHILE @@FETCH_STATUS = 0 BEGIN SELECT TOP 1 @NewCompId = T.FCompId FROM TMemberSaleBill AS T,TMemberSaleCard AS B WHERE T.FBillId = B.FBillId AND B.FSalePrice > 0 AND B.FCardId = @CardId ORDER BY T.FBillTime DESC print @NewCompId print @OldCompId IF @OldCompId != @NewCompId UPDATE #Card SET FNewCompId = @NewCompId WHERE FCardId = @CardId -- UPDATE TMemberCard SET FCompId = @CompId FETCH List INTO @CardId,@OldCompId END CLOSE List DEALLOCATE LIST UPDATE #Card SET FNewCompId = 0 UPDATE A SET A.FCompId = B.FNewCompId FROM TMemberCard AS A,#Card AS B WHERE A.FCardId = B.FCardId --更新会员门店 SELECT A.FMemId FROM TMemberCard AS A,TMember AS B WHERE A.FCompId = B.FCompId AND A.FMemID = B.FMemId SELECT * FROM #Card WHERE FOldCompID != FNewCompId UPDATE TMemberCard SET FDeleted = 1,FMemo = FMemo + '【2018年6月一日从美尚分离】',FCardNo = FCardNo + '已删' WHERE FCompId != 3472331387795668992 AND FDeleted = 0 UPDATE TMemberCourse SET FDeleted = 1,FMemo = FMemo + '【2018年6月一日从美尚分离】' WHERE FCompId != 3472331387795668992 AND FDeleted = 0 UPDATE TMember SET FDeleted = 1,FMemo = FMemo + '【2018年6月一日从美尚分离】' ,FMemNo = FMemNo + '已删' WHERE FMemId NOT IN(SELECT FMemId FROM TMemberCard WHERE FDeleted = 0 UNION ALL SELECT FMemId FROM TMemberCourse WHERE FDeleted = 0) AND FDeleted = 0 AND (FIsVisitor = 0 OR (FIsVisitor = 1 AND FCompId != 3472331387795668992)) UPDATE TCompany SET FDeleted = 1,FNo =FNo + '已删' WHERE FId != 3472331387795668992 AND FId !=0 UPDATE TCompany SET FDeleted = 0,FNo =REPLACE(FNo , '已删','') WHERE FId = 1 --删除非006的单据 UPDATE TMember SET FCompId = 3472331387795668992 WHERE FDeleted = 0 --处理美尚 UPDATE A SET A.FCompId = B.FCompId FROM jinan_meishang..TMemberCard AS A,jinan_jumei..TMemberCard AS B WHERE A.FCardId = B.FCardId AND A.FCompId != B.FCompId UPDATE A SET A.FCompId = B.FCompID FROM TMemberCard AS B,TMember AS A WHERE A.FMemId = B.FMemId UPDATE jinan_meishang..TMemberCard SET FDeleted = 1,FMemo = FMemo + '【2018年6月一日从美尚分离】',FCardNo = FCardNo + '已删' WHERE FCompId = 3472331387795668992 AND FDeleted = 0 UPDATE jinan_meishang..TMemberCourse SET FDeleted = 1,FMemo = FMemo + '【2018年6月一日从美尚分离】' WHERE FCompId = 3472331387795668992 AND FDeleted = 0 UPDATE jinan_meishang..TMember SET FDeleted = 1,FMemo = FMemo + '【2018年6月一日从美尚分离】' ,FMemNo = FMemNo + '已删' WHERE FMemId NOT IN(SELECT FMemId FROM jinan_meishang..TMemberCard WHERE FDeleted = 0 UNION ALL SELECT FMemId FROM jinan_meishang..TMemberCourse WHERE FDeleted = 0) AND FDeleted = 0 AND (FIsVisitor = 0 OR (FIsVisitor = 1 AND FCompId = 3472331387795668992)) DELETE JiNan_MeiShang..TBizLog WHERE FCompId = 3472331387795668992 DELETE JiNan_MeiShang..TBizLogIndex WHERE FLogId NOT IN(SELECT FId FROM JiNan_MeiShang..TBizLog) DELETE JiNan_JuMei..TBizLog WHERE FCompId != 3472331387795668992 DELETE JiNan_JuMei..TBizLogIndex WHERE FLogId NOT IN(SELECT FId FROM JiNan_JuMei..TBizLog) DELETE JiNan_MeiShang..TLoginSession WHERE FCompId = 3472331387795668992 DELETE JiNan_JuMei..TLoginSession WHERE FCompId != 3472331387795668992 DELETE TBizLog WHERE FCompId = 3472331387795668992 DELETE TBizLogIndex WHERE FLogId NOT IN(SELECT FId FROM TBizLog) PCopyTable 'TBizLog','JINAN_MEISHANG..TBizLog',' AND FCompId = 3472331387795668992 ' SET IDENTITY_INSERT TBizLog OFF DELETE TItemSub WHERE FCompId NOT IN(SELECT FId FROM TCompany WHERE FDeleted = 0) DELETE TGoodsSub WHERE FCompId NOT IN(SELECT FId FROM TCompany WHERE FDeleted = 0) DELETE TEmployeeSub WHERE FCompId NOT IN(SELECT FId FROM TCompany WHERE FDeleted = 0) DELETE TMemberCardTypeSub WHERE FCompId NOT IN(SELECT FId FROM TCompany WHERE FDeleted = 0)