IF object_id('dbo.PCreateMemberSaleCardPayment','P') IS NOT NULL BEGIN DROP PROCEDURE dbo.PCreateMemberSaleCardPayment END GO CREATE PROCEDURE dbo.PCreateMemberSaleCardPayment ( @BillId bigint, @BizType VARCHAR(30) ) AS BEGIN IF EXISTS(SELECT TOP 1 1 FROM TMemberSaleDetailPayment WHERE FBillId = @BillId) BEGIN print '已经执行' RETURN END SELECT * INTO #Payment FROM TMemberSalePayment WHERE FBillId = @BillId --会费只能用现金支付 DECLARE @CashCardFee DECIMAL(18,6) DECLARE @BankCardFee DECIMAL(18,6) SET @CashCardFee = 0 SET @BankCardFee = 0 IF @BizType = 'CardSale' BEGIN DECLARE @Error INT DECLARE @CardFee DECIMAL(18,6) --会费 SELECT @CardFee = FCardFee FROM TMemberSaleCard WITH(NOLOCK) WHERE FBillId=@BillId IF @@ROWCOUNT = 0 BEGIN RETURN -1 END IF @CardFee > 0 BEGIN DECLARE @CashPay DECIMAL(18,6) SELECT @CashPay = ISNULL(FPayAmt,0) FROM #Payment WHERE FPayTypeId = 1 SET @CashPay = ISNULL(@CashPay,0) IF @CardFee > @CashPay BEGIN DECLARE @BankPay DECIMAL(18,6) SELECT @BankPay = ISNULL(FPayAmt,0) FROM #Payment WHERE FPayTypeId = 2 SET @BankPay = ISNULL(@BankPay,0) IF @CardFee > @CashPay + @BankPay BEGIN RAISERROR('会费必须使用现金或银行卡支付',18,1) RETURN -1 END SET @CashCardFee = @CashPay SET @BankCardFee = @CardFee - @CashCardFee END ELSE BEGIN SET @CashCardFee = @CashPay - @CardFee END IF @CashCardFee > 0 BEGIN INSERT TMemberSaleDetailPayment(FId,FBillId,FBizType,FDetailId,FPayTypeId,FPayAmt) VALUES(dbo.FNGetNewId(),@BillId,@BizType,2,1,@CashCardFee) END IF @BankCardFee > 0 BEGIN INSERT TMemberSaleDetailPayment(FId,FBillId,FBizType,FDetailId,FPayTypeId,FPayAmt) VALUES(dbo.FNGetNewId(),@BillId,@BizType,2,2,@BankCardFee) END END END --储值卡的支付明细 INSERT TMemberSaleDetailPayment(FId,FBillId,FBizType,FDetailId,FPayTypeId,FPayAmt) SELECT dbo.FNGetNewId(),@BillId,@BizType,1,ISNULL(FPayTypeId,1),ISNULL(CASE WHEN FPayTypeId = 1 THEN FPayAmt - @CashCardFee WHEN FPayTypeId = 2 THEN FPayAmt - @BankCardFee ELSE FPayAmt END,0) AS FPayAmt FROM #Payment WITH(NOLOCK) END GO DECLARE @sql VARCHAR(1000) declare #sqlList cursor for SELECT 'EXEC PCreateMemberSaleCardPayment ' + cast(FBillId AS VARCHAR(30)) + ',''' + FBizType + '''' FROM TMemberSaleBill WHERE FBizType IN( 'CardSale','CardRchg') open #sqlList fetch #sqlList into @sql while @@fetch_status=0 begin print @sql EXEC (@sql) fetch #sqlList into @sql end close #sqlList deallocate #sqlList