这对我来说很难解释,但我尽我所能让你们尽可能轻松。
我有两张桌子。一个存储有关客户tbl_installations
的信息。另一个他的互联网账单tbl_bills
(这个更重要)。
tbl_installations
CREATE TABLE [dbo].[tbl_installations](
[SrNo] [int] IDENTITY(1000,1) NOT NULL,
[name] [varchar](200) NULL,
[email] [varchar](100) NULL,
[phone] [varchar](20) NULL,
[plandesc] [varchar](50) NULL,
[plancost] [money] NULL,
[amtpaid] [money] NULL,
[amtbalance] [money] NULL,
[address] [varchar](500) NULL,
[referencename] [varchar](20) NULL,
[installationdate] [date] NULL,
[planduration] [int] NULL,
[MkrId] [varchar](20) NULL,
[MkrDt] [datetime] NULL,
[FromDate] [datetime] NULL,
[ToDate] [datetime] NULL,
[ExpiryDate] [datetime] NULL,
[Status] [varchar](20) NULL,
[CustomerId] [varchar](20) NULL,
[ServiceTax] [money] NULL,
[TotalAmt] [money] NULL
) ON [PRIMARY]
tbl_bills
CREATE TABLE [dbo].[tbl_Bills](
[Srno] [int] IDENTITY(1,1) NOT NULL,
[CustomerId] [varchar](20) NULL,
[PlanDuration] [int] NULL,
[Amount] [money] NULL,
[PaidAmt] [money] NULL,
[discount] [money] NULL,
[PlanDesc] [varchar](20) NULL,
[FromDate] [datetime] NULL,
[ToDate] [datetime] NULL,
[MkrId] [varchar](20) NULL,
[MkrDt] [datetime] NULL,
[ServiceTax] [money] NULL,
[TotalAmt] [money] NULL,
[PendingAmt] AS ([TotalAmt]-([PaidAmt]+[discount])),
[PaidStatus] [varchar](10) NULL
) ON [PRIMARY]
这篇文章会给你我的数据:
insert into tbl_installations(name,email,phone,plandesc,plancost,amtpaid,amtbalance,address,referencename,installationdate,planduration,MkrId,
MkrDt,FromDate,ToDate,ExpiryDate,Status,CustomerId,ServiceTax,TotalAmt)
values('Gulzar','asdasd','3242342',null,null,null,null,'asda asd aada','ref name',GETDATE()-4,1,'arbaaz', GETDATE(),null,null,
null,'INSTALLED','C2002',null,null )
insert into tbl_bills (PlanDuration,Amount,PaidAmt,discount,PlanDesc,FromDate,ToDate,PaidStatus,MkrId,MkrDt,ServiceTax,TotalAmt,CustomerId)
values(1,800,600,0,'1MB',GETDATE()-4,DATEADD(month,1, GETDATE()-4),'PAID','Arbaaz',getdate(),800*(12.36/100),800+800*(12.36/100),'C2001')
tbl_bills中的 PendingAmt
是一个计算机列,根据totalamt - (paidamt+discount)
tbl_bills.Amount
是替代费用,例如:800
tbl_bills.TotalAmt
是Amount + ServiceTax的总和
方案
第一个月:
客户的承保费用(金额)为800 PaidAmt = 600 ServiceTax = 98.88 TotalAmt = 898.88 PendingAmt = 298.88(计算字段)
第二个月:
客户仍然拥有相同的订阅价值800(总计898.88)
但他支付了1100美元,以便他解决了一些未决数额。
此1100必须首先解决所有先前的待处理金额(通过更新tbl_bills.paidamt
,然后应该将剩下的内容作为一个全新的行插入同一个表中。
这是我失败的尝试:
这是徒步运行
declare @srno as int=0
declare @Cid as varchar(20)='C2001'
declare @PlanDuration as int=1
declare @Amount as money=800
declare @PlanDesc as varchar(20)='1MB'
declare @FromDate as datetime ='2014-11-11'
declare @ToDate as datetime='2014-12-11'
declare @PaidStatus as varchar(20)
declare @MkrId as varchar(20)='arbaaz'
declare @status as varchar(20)='PAID'
declare @Discount as money=0
declare @PaidAmt as money=1100
--as
if(@status='INSTALLED')
BEGIN
insert into tbl_bills (PlanDuration,Amount,PaidAmt,discount,PlanDesc,FromDate,ToDate,PaidStatus,MkrId,MkrDt,ServiceTax,TotalAmt,CustomerId)
values(@PlanDuration,@Amount,@paidamt,@discount,@plandesc,@FromDate,@ToDate,'PAID','Arbaaz',getdate(),@Amount*(12.36/100),@Amount+@Amount*(12.36/100),@Cid)
if((select sum(PendingAmt) from tbl_Bills where CustomerId=@cid)=0 )
begin
update tbl_installations set Status='PAID' ,ExpiryDate=@ToDate where CustomerId=@Cid
end
else
begin
update tbl_installations set Status='UNPAID' ,ExpiryDate=@ToDate where CustomerId=@Cid
end
END
ELSE
BEGIN
declare @pAmt as money
select @pAmt=sum(PendingAmt) from tbl_Bills where CustomerId=@Cid
declare @Amt as money =@Amount
select @Amt=@Amt
--select srno from tbl_Bills where CustomerId=@Cid and PendingAmt<>0 order by ToDate
if(@pAmt>0)
BEGIN------------------------
DECLARE @ColExpir datetime
DECLARE @ColFallprotec datetime
DECLARE @CurSrno int
--------------------------------------------------------
DECLARE @MyCursor CURSOR
SET @MyCursor = CURSOR FAST_FORWARD
FOR
select srno from tbl_Bills where CustomerId=@Cid and PendingAmt<>0 order by todate
OPEN @MyCursor
FETCH NEXT FROM @MyCursor
INTO @CurSrno
WHILE @@FETCH_STATUS = 0
BEGIN
IF(@Amt>0)
BEGIN
declare @PendingAtCurSrno money
select @PendingAtCurSrno=pendingamt from tbl_Bills where SrNo=@CurSrno
print @CurSrno -----
print @PendingAtCurSrno ----
if(@Amt>@PendingAtCurSrno)
begin
update tbl_Bills set PaidAmt=TotalAmt where SrNo=@CurSrno
select @Amt=@Amt-@PendingAtCurSrno
print '1st'----
print @amt----
if(@amt=0)
begin
CLOSE @MyCursor --tried break and return here too
DEALLOCATE @MyCursor --
end
end
else
begin
update tbl_Bills set PaidAmt=paidamt+@Amt where SrNo=@CurSrno
select @Amt=0
print '2nd'
print @amt----
if(@amt=0)
begin
CLOSE @MyCursor --tried break and return here too
DEALLOCATE @MyCursor
end
end
END
END
FETCH NEXT FROM @MyCursor
INTO @PendingAtCurSrno
END
CLOSE @MyCursor
DEALLOCATE @MyCursor
END-------------------------
if(@Amt>0)
begin
insert into tbl_bills (PlanDuration,Amount,PaidAmt,discount,PlanDesc,FromDate,ToDate,PaidStatus,MkrId,MkrDt,ServiceTax,TotalAmt,CustomerId)
values(@PlanDuration,@Amount,@Amt,@discount,@plandesc,@FromDate,@ToDate,'PAID','Arbaaz',getdate(),@Amount*(12.36/100),@Amount+@Amount*(12.36/100),@Cid)
end
if((select sum(PendingAmt) from tbl_Bills where CustomerId=@cid)=0 )
begin
update tbl_installations set Status='PAID' ,ExpiryDate=@ToDate where CustomerId=@Cid
end
else
begin
update tbl_installations set Status='UNPAID' ,ExpiryDate=@ToDate where CustomerId=@Cid
end
它不仅一直无休止地运行,它不断地一遍又一遍地更新paidamt
,并且永远不会到达应该插入剩余金额的另一行。