You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
sql-tools/分离出.sql

261 lines
9.3 KiB

use QingDao_saidao16
select * from TEmployeeReferral
UPDATE TEmployeeReferral SET FCompId = 0
EXEC PChangeCenterCompany '003'
SELECT * FROM TMemberCardTypeAccount
DELETE TMemberCardTypeAccount WHERE FCompId = 4877384222910420443
--删除门店
SELECT * FROM DongYing_DongHua..TCompany
SELECT * FROM DongYing_DongHua_LiCangWanDa..TCompany
UPDATE DongYing_DongHua.dbo.TCompany SET FDeleted = 1,FNo = FNo + '已删' WHERE FNo IN('006')
UPDATE DongYing_DongHua_LiCangWanDa.dbo.TCompany SET FDeleted = 1,FNo = FNo + '已删' WHERE FNO NOT IN('','006')
update TCompany SET FDeleted = 1 WHERE FId = 0
SELECT * FROM TMemberCard WHERE FCompId != 4877384222910420443
AND FCardId IN(SELECT FCardId FROM TConsumeBill WHERE FCompId = 4877384222910420443)
--储值卡
UPDATE DongYing_DongHua.dbo.TMemberCard SET FDeleted = 1,FCardNo = FCardNo + '已删',FMemo = FMemo + '【李沧万达】'
WHERE FDeleted= 0
AND FCompId = 4877384222910420443
UPDATE DongYing_DongHua_LiCangWanDa.dbo.TMemberCard SET FDeleted = 1,FCardNo = FCardNo + '已删',FMemo = FMemo + '【大大士大】'
WHERE FDeleted= 0
AND FCompId != 4877384222910420443
UPDATE DongYing_DongHua..TMemberCourse SET FDeleted = 1, FMemo = FMemo + '【李沧万达】'
WHERE FDeleted= 0
AND FCompId = 4877384222910420443
UPDATE DongYing_DongHua_LiCangWanDa..TMemberCourse SET FDeleted = 1, FMemo = FMemo + '【大大士大】'
WHERE FDeleted= 0
AND FCompId != 4877384222910420443
UPDATE DongYing_DongHua..TMember SET FDeleted = 0,FMemNo = REPLACE(FMemNo, '已删',''),FMemo = REPLACE(FMemo , '【李沧万达】','')
WHERE FMemo LIKE '%【李沧万达】%'
AND FDeleted = 1
UPDATE DongYing_DongHua_licangwanda..TMember SET FDeleted = 0,FMemNo = REPLACE(FMemNo, '已删',''),FMemo = REPLACE(FMemo , '【大大士大】','')
WHERE FMemo LIKE '%【大大士大】%'
AND FDeleted = 1
UPDATE DongYing_DongHua..TMember SET FDeleted = 1,FMemNo = FMemNo + '已删',FMemo = FMemo + '【李沧万达】'
WHERE FDeleted= 0
AND FCompId = 4877384222910420443
AND FMemId NOT IN(SELECT FMemId FROM DongYing_DongHua..TMemberCard WHERE FDeleted = 0)
AND FMemId NOT IN(SELECT FMemId FROM DongYing_DongHua..TMemberCourse WHERE FDeleted = 0)
UPDATE DongYing_DongHua SET FMemId = (SELECT FMemId FROM TMemberCard )
UPDATE DongYing_DongHua_LiCangWanDa..TMember SET FDeleted = 1,FMemNo = FMemNo + '已删',FMemo = FMemo + '【大大士大】'
WHERE FDeleted= 0
AND FCompId != 4877384222910420443
AND FMemId NOT IN(SELECT FMemId FROM DongYing_DongHua_LiCangWanDa..TMemberCard WHERE FDeleted = 0)
AND FMemId NOT IN(SELECT FMemId FROM DongYing_DongHua_LiCangWanDa..TMemberCourse WHERE FDeleted = 0)
--基本资料
DELETE DongYing_DongHua..TEmployeeSub WHERE FCompId = 4877384222910420443
DELETE DongYing_DongHua..TItemSub WHERE FCompId = 4877384222910420443
DELETE DongYing_DongHua..TGoodsSub WHERE FCompId = 4877384222910420443
DELETE DongYing_DongHua..TBasicTypeSub WHERE FCompId = 4877384222910420443
DELETE DongYing_DongHua_LiCangWanDa..TEmployeeSub WHERE FCompId != 4877384222910420443
DELETE DongYing_DongHua_LiCangWanDa..TItemSub WHERE FCompId != 4877384222910420443
DELETE DongYing_DongHua_LiCangWanDa..TGoodsSub WHERE FCompId != 4877384222910420443
DELETE DongYing_DongHua_LiCangWanDa..TBasicTypeSub WHERE FCompId != 4877384222910420443
DROP TABLE #ConsumeBill
DROP TABLE #MemberSaleBill
-- SELECT FBillId INTO #ConsumeBill FROM TConsumeBill
-- WHERE FCompId = 8308717354799492172
-- AND FMemId NOT IN(SELECT FMemId FROM TMember WHERE FDeleted = 0)
-- SELECT FBillId INTO #MemberSaleBill FROM TMemberSaleBill
-- WHERE FCompId = 8308717354799492172
-- AND FMemId NOT IN(SELECT FMemId FROM TMember WHERE FDeleted = 0)
SELECT 'DELETE ' + NAME + ' WHERE FBillId IN(SELECT FBillId FROM #ConsumeBill)' FROM sysobjects WHERE name like 'TConsume%'
SELECT 'DELETE ' + NAME + ' WHERE FBillId IN(SELECT FBillId FROM #MemberSaleBill)' FROM sysobjects WHERE name like 'TMemberSale%'
DELETE TConsumeItemCostGoods WHERE FBillId IN(SELECT FBillId FROM #ConsumeBill)
DELETE TConsumeItemEmployee WHERE FBillId IN(SELECT FBillId FROM #ConsumeBill)
DELETE TConsumeItemPayment WHERE FBillId IN(SELECT FBillId FROM #ConsumeBill)
DELETE TConsumeReturnVisit WHERE FBillId IN(SELECT FBillId FROM #ConsumeBill)
DELETE TConsumeReturnVisitLog WHERE FBillId IN(SELECT FBillId FROM #ConsumeBill)
DELETE TConsumeBill WHERE FBillId IN(SELECT FBillId FROM #ConsumeBill)
DELETE TConsumeGoods WHERE FBillId IN(SELECT FBillId FROM #ConsumeBill)
DELETE TConsumeGoodsEmployee WHERE FBillId IN(SELECT FBillId FROM #ConsumeBill)
DELETE TConsumeGoodsPayment WHERE FBillId IN(SELECT FBillId FROM #ConsumeBill)
DELETE TConsumeItem WHERE FBillId IN(SELECT FBillId FROM #ConsumeBill)
DELETE TMemberSalePayment WHERE FBillId IN(SELECT FBillId FROM #MemberSaleBill)
DELETE TMemberSaleRepay WHERE FBillId IN(SELECT FBillId FROM #MemberSaleBill)
DELETE TMemberSaleBill WHERE FBillId IN(SELECT FBillId FROM #MemberSaleBill)
DELETE TMemberSaleCard WHERE FBillId IN(SELECT FBillId FROM #MemberSaleBill)
DELETE TMemberSaleCombo WHERE FBillId IN(SELECT FBillId FROM #MemberSaleBill)
DELETE TMemberSaleCourse WHERE FBillId IN(SELECT FBillId FROM #MemberSaleBill)
DELETE TMemberSaleCourseGoods WHERE FBillId IN(SELECT FBillId FROM #MemberSaleBill)
DELETE TMemberSaleCoursePack WHERE FBillId IN(SELECT FBillId FROM #MemberSaleBill)
DELETE TMemberSaleDetailPayment WHERE FBillId IN(SELECT FBillId FROM #MemberSaleBill)
DELETE TMemberSaleEmployee WHERE FBillId IN(SELECT FBillId FROM #MemberSaleBill)
DELETE TMemberSaleExchange WHERE FBillId IN(SELECT FBillId FROM #MemberSaleBill)
DELETE TMemberSaleExchangeSource WHERE FBillId IN(SELECT FBillId FROM #MemberSaleBill)
DELETE TMemberSaleFreeItem WHERE FBillId IN(SELECT FBillId FROM #MemberSaleBill)
DELETE TMemberSaleInfo WHERE FBillId IN(SELECT FBillId FROM #MemberSaleBill)
DELETE TMemberSaleDetailSplit WHERE FBillId IN(SELECT FBillId FROM #MemberSaleBill)
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 TBasicTypeSub WHERE FCompId NOT IN(SELECT FId FROM TCompany WHERE FDeleted =0 )
DELETE TMemberCardTypeSub 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 TEmployee WHERE FId NOT IN(SELECT FEmpId FROM TEmployeeSub )
AND FId > 1
AND FId NOT IN(SELECT FEmpId FROM TConsumeItemEmployee UNION ALL SELECT FEmpId FROM TCOnsumeGoodsEmployee UNION ALL SELECT FEmpId FROM TMemberSaleEmployee)
--根据最后一次充值门店设置会员归属门店
DROP TABLE #Card
CREATE TABLE #Card
(
FCardId BIGINT,
FCompId BIGINT,
FNewCompId BIGINT
)
INSERT #Card(FCardId,FCompId)
SELECT A.FCardId,C.FCompId FROM TMemberSaleCard AS A,TMemberSaleBill AS B,TMemberCard AS C
WHERE A.FBillId = B.FBilliD
AND C.FCardId = A.FCardId
AND B.FState = 1
AND B.FBizType = 'CardRchg'
AND A.FSalePrice > 0
GROUP BY A.FCardId,C.FCompId
DECLARE @CardId BIGINT
DECLARE @CardNo VARCHAR(300)
DECLARE @CompId BIGINT
DECLARE @OwnerCompId BIGINT
DECLARE List CURSOR FOR SELECT FCardId,FCompId FROM #Card
OPEN List
FETCH List INTO @CardId,@OwnerCompId
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @EmpId BIGINT
SELECT TOP 1 @CompId = T.FCompId
FROM TMemberSaleCard AS B,TMemberSaleBill AS T
WHERE T.FBillId = B.FBillId
AND B.FSalePrice > 0
AND B.FCardId = @CardId
ORDER BY T.FBillTime DESC
DECLARE @NewCompId BIGINT
SELECT @NewCompId = 0
IF @OwnerCompId = 4877384222910420443 AND @CompId != 4877384222910420443
BEGIN
SELECT @NewCompId = @CompId
END
IF @OwnerCompId != 4877384222910420443 AND @CompId = 4877384222910420443
BEGIN
SELECT @NewCompId = @CompId
END
IF @NewCompId != 0
BEGIN
PRINT @NewCompId
UPDATE #Card SET FNewCompId = @NewCompId WHERE FCardId = @CardId
END
FETCH List INTO @CardId,@OwnerCompId
END
CLOSE List
DEALLOCATE LIST
SELECT * FROM TMemberCard
WHERE FCardId IN(
SELECT FCardId FROM #Card WHERE ISNULL(FNewCompId,0) != 0
)
-- UPDATE A
-- SET A.FCompId = B.FNewCompId
-- FROM #Card AS B,DongYing_DongHua..TMemberCard AS A
-- WHERE A.FCardId = B.FCardId
-- AND A.FCompId != B.FNewCompId
UPDATE DongYing_DongHua..TMemberCard SET FDeleted = 0,FCardNO = REPLACE(FCardNo,'已删' ,''),FMemo = REPLACE(FMemo,'【李沧万达】','')
WHERE FCardId IN(SELECT FCardId FROM #Card)
UPDATE DongYing_DongHua..TMemberCard
SET FDeleted = 0,
FCardNO = REPLACE(FCardNo,'已删' ,''),
FMemo = REPLACE(FMemo,'【大大士大】','')
WHERE FCardId IN(SELECT FCardId FROM #Card )
AND FDeleted = 1
AND FMemo LIKE '%【大大士大】%'
UPDATE DongYing_DongHua..TMemberCard
SET FDeleted = 0,
FCardNO = REPLACE(FCardNo,'已删' ,''),
FMemo = REPLACE(FMemo,'【李沧万达】','')
WHERE 1=1
--WHERE FCardId IN(SELECT FCardId FROM #Card AND ISNULL(FNewCompId,0) != 0 )
AND FDeleted = 1
AND FMemo LIKE '%【李沧万达】%'
UPDATE A
SET A.FCompId = B.FCompId
FROM DongYing_DongHua..TMemberCard AS B,DongYing_DongHua..TMember AS A
WHERE A.FMemId = B.FMemId
AND A.FCompId != B.FCompId
AND b.FCardId IN(SELECT FCardId FROM #Card WHERE ISNULL(FNewCompId,0) != 0)
UPDATE A
SET A.FCompId = B.FCompId
FROM DongYing_DongHua_LICANGWANDA..TMemberCard AS B,DongYing_DongHua_LICANGWANDA..TMember AS A
WHERE A.FMemId = B.FMemId
AND A.FCompId != B.FCompId
AND b.FCardId IN(SELECT FCardId FROM #Card WHERE ISNULL(FNewCompId,0) != 0)