INSERT TMemberTransHistPayment(FMemId,FCardId,FBillType,FTransId,FTransDetailId,FPayTypeId,FPayAmt) SELECT T.FMemId,A.FCardId,'XF',T.FBillId,A.FDetailId,A.FPayTypeId,A.FPayAmt FROM TConsumeGoodsPayment AS A,TConsumeBill AS T WHERE A.FBillId = T.FBillId AND T.FMemId != 0 AND A.FDetailId NOT IN(SELECT FTransDetailId FROM TMemberTransHistPayment)