IF OBJECT_ID('PCreateConsumeTransHist') IS NOT NULL DROP PROC PCreateConsumeTransHist GO CREATE PROC PCreateConsumeTransHist ( @BillId BIGINT ) AS BEGIN DECLARE @CompId BIGINT DECLARE @MemId BIGINT DECLARE @CardId BIGINT DECLARE @BillMemo VARCHAR(1000) DECLARE @BillTime DATETIME DECLARE @BillNo VARCHAR(30) SELECT @CompId = FCompId, @MemId = FMemId, @CardId = FCardId, @BillMemo = FBillMemo, @BillTime = FBillTime, @BillNo = FBillNo FROM TConsumeBill WHERE FBillId= @Billid SELECT A.FId, A.FItemId ,A.FOrigPrice,FDiscount,FAmount,FDctPrice,A.FSequence, A.FQuantity, A.FMemCourseId, A.FMemComboItemId, A.FUseFreeCourse, A.FDeptId,A.FPoint, IDENTITY(int) FIndex INTO #Item FROM TConsumeItem AS A WHERE A.FBillId = @Billid --物品表 SELECT A.FId, A.FGoodsId ,A.FOrigPrice,FDiscount,FAmount,FDctPrice,A.FSequence, A.FQuantity, A.FUnitId,FStoreId, A.FMemComboGoodsId, A.FDeptId,A.FPoint, IDENTITY(int) FIndex INTO #Goods FROM TConsumeGoods AS A WHERE A.FBillId = @Billid --项目支付明细 SELECT FDetailId,FPayTypeId,FPayAmt ,FCardId INTO #ItemPayment FROM TConsumeItemPayment WITH(NOLOCK) WHERE FBillId = @Billid --产品支付明细 SELECT FDetailId,FPayTypeId,FPayAmt,FCardId INTO #GoodsPayment FROM TConsumeGoodsPayment WITH(NOLOCK) WHERE FBillId = @Billid --员工 SELECT 'Item' AS FBizType,0 AS FIndex,FDetailId, E1.FNo + '-' + E1.FName + CASE WHEN A.FDispatchMode != 10002 THEN ('[' + ISNULL(S.FName,'') + ']') ELSE '' END AS FEmployees INTO #HistEmployee_ FROM TConsumeItemEmployee AS A WITH(NOLOCK) LEFT JOIN TBasicType AS S WITH(NOLOCK) ON S.FId = A.FDispatchMode AND S.FKey = 'ServeDispatchMode' ,TEmployee AS E1 WITH(NOLOCK) -- LEFT JOIN TEmployeeSub AS E2 WITH(NOLOCK) ON E2.FCompId = @CompId AND E2.FEmpId = E1.FId WHERE FBillId = @BillId AND E1.FId = A.FEmpId UNION ALL SELECT 'Goods' AS FBizType,0 AS FIndex,FDetailId, E1.FNo + '-' + E1.FName AS FEmployees FROM TConsumeGoodsEmployee AS A WITH(NOLOCK),TEmployee AS E1 WITH(NOLOCK) -- LEFT JOIN TEmployeeSub AS E2 WITH(NOLOCK) ON E2.FCompId = @CompId AND E2.FEmpId = E1.FId WHERE FBillId = @BillId AND E1.FId = A.FEmpId DECLARE @DetailId BIGINT DECLARE @Index INT DECLARE @Employee VARCHAR(1000) SET @DetailId = 1 SET @Index = 0 UPDATE #HistEmployee_ SET @Employee = CASE WHEN @DetailId = FDetailId THEN @Employee + ',' + FEmployees ELSE FEmployees END, @Index = CASE WHEN @DetailId = FDetailId THEN @Index + 1 ELSE FIndex END, FEmployees = @Employee, FIndex = @Index, @DetailId = FDetailId SELECT FDetailId,FBizType,MAX(FEmployees) AS FEmployees INTO #HistEmployee FROM #HistEmployee_ GROUP BY FDetailId,FBizType EXEC PAuditConsumeMemberTransHist @BillId = @Billid, --单据ID @CompId = @CompId, --公司ID @BillNo = @BillNo, --单号 @BillTime =@BillTime, --单据时间 @BillType ='XF', --单据类别 @MemId = @MemId, --会员id @CardId = @CardId, --卡ID @BillMemo = @BillMemo --单据备注 DROP TABLE #Item DROP TABLE #Goods DROP TABLE #ItemPayment DROP TABLE #GoodsPayment END GO DROP TABLE #Consume SELECT ' EXEC PCreateConsumeTransHist ' + CAST(FBillId AS VARCHAR) AS FSql INTO #Consume FROM TConsumeBill WHERE FState = 1 AND FMemID > 0 DECLARE @I INT SELECT @I = 1 DECLARE @Sql VARCHAR(8000) DECLARE List CURSOR FOR SELECT FSql FROM #Consume OPEN List FETCH List INTO @Sql WHILE @@FETCH_STATUS = 0 BEGIN EXEC(@SQL) PRINT @I SELECT @I = @I + 1 FETCH List INTO @Sql END CLOSE List DEALLOCATE LIST