


SELECT * FROM TCompany 

UPDATE TItemDiscountMaster SET FCompId = 

DROP TABLE #Master
DROP TABLE #Mid
DROP TABLE #Detail

SELECT *INTO #Master FROM TItemDiscountMaster WHERE FCompId = 3472334686330552320 AND FItemId != 6653890054222136817
SELECT * INTO #Mid FROM TItemDiscountMid WHERE FParentId IN(SELECT FId FROM #Master)
SELECT * INTO #Detail FROM TItemDiscountDetail WHERE FParentId IN(SELECT FId FROM #Mid)

ALTER TABLE #Master ADD FNewId BIGINT  

ALTER TABLE #Mid ADD FNewId BIGINT  
ALTER TABLE #Detail ADD FNewId BIGINT  


UPDATE #Master SET FNewId = dbo.FNNewId()
UPDATE #Mid SET FNewId = dbo.FNNewId()
UPDATE #Detail SET FId = dbo.FNNewId()

UPDATE A
   SET A.FParentId = B.FNewId
  FROM #Mid AS A,#Master AS B
 WHERE A.FParentId = B.FId
 
 UPDATE A
   SET A.FParentId = B.FNewId
  FROM #Detail AS A,#Mid AS B
 WHERE A.FParentId = B.FId

UPDATE #Master SET FId = FNewId
UPDATE #Mid SET FId = FNewId

SELECT * FROM #Master

ALTER TABLE #Master DROP COLUMN FNewID 
ALTER TABLE #dETAIL DROP COLUMN FNewID 
ALTER TABLE #Mid DROP COLUMN FNewID 


SELECT * FROM #Master
SELECT * FROM TItemDiscountMaster
WHERE FCompId IN(3472331387795668992)

UPDATE #master SET FCompId = 3472332487307296768


INSERT TItemDiscountMaster (FId,FCompId,FItemId,FType,FCreateDate,FWeek,FTimeMode)
SELECT FId,FCompId,FItemId,FType,FCreateDate,FWeek,FTimeMode FROM #master 
WHERE FItemId = 6653890054222136817

DELETE TItemDiscountMid WHERE FParentId = 5807149648437453239

INSERT TItemDiscountMid 
SELECT * FROM #Mid WHERE FParentId = 5807149648437453239

INSERT TItemDiscountDetail 
SELECT * FROM #Detail  WHERE FParentId IN(SELECT FId FROM #Mid)


UPDATE #master SET FId = FNewId

SELECT * FROM TItemDiscountMaster WHERE FCompId = 3472331387795668992 AND FItemId IN(SELECT FItemId FROM #master)

UPDATE A
   SET A.FNewId = B.FId
  FROM #master AS A,TItemDiscountMaster AS B
 WHERE B.FCompId = 3472332487307296768
 AND A.FItemId = B.FItemId
 


SELECT * FROM TItem