use jinan_kadibao use qingdao_yishang use qingdao_zhuoyue DECLARE @CardId BIGINT DECLARE @CardNo VARCHAR(300) DECLARE List CURSOR FOR SELECT A.FCardId FROM TMemberSaleCard AS A,TMemberSaleBill AS B WHERE A.FBillId = B.FBilliD AND B.FState = 1 GROUP BY FCardId OPEN List FETCH List INTO @CardId WHILE @@FETCH_STATUS = 0 BEGIN DECLARE @EmpId BIGINT SELECT TOP 1 @EmpId = A.FEmpId ,@CardNo = B.FCardNo FROM TMemberSaleEmployee AS A,TMemberSaleCard AS B,TMemberSaleBill AS T WHERE T.FBillId = A.FBillId AND T.FBillId = B.FBillId AND B.FSalePrice > 0 AND B.FCardId = @CardId ORDER BY T.FBillTime DESC,A.FWorkType PRINT @CardNo IF @EmpId != 0 UPDATE TMemberCard SET FBizEmpid = @EmpId WHERE FCardId = @CardId FETCH List INTO @CardId END CLOSE List DEALLOCATE LIST