MSSQL 时间通用存储过程

数据库操作及消息商品初始化
本文详细介绍了在特定数据库环境中,通过存储过程实现消息商品的初始化操作,包括年度和月份参数设定,涉及金额、余额、数量等关键字段的计算与更新。
/***********************************************************************************************
author: QLP
last updated: 2005-03-16, awens
remark:t_amount replace with numeric(18,8),double precision replace with numeric(18,8) mssql
***********************************************************************************************/
ALTER  procedure [dbo].[aa_msgoods_startacc] (
  @nyear int,
  @nmonth int
)  as
  declare @goodsid int
  declare @subjectid int
  declare @bmbala  numeric(18,8)
  declare @fbmbala numeric(18,8)
  declare @tmsum   numeric(18,8)
  declare @ftmsum  numeric(18,8)
  declare @bmqtybala  numeric(18,8)
  declare @temp_tab1 table(goodsid integer,
                           subjectid integer,
                           bmbala numeric(18,8),
                           fbmbala numeric(18,8) ,
                           bmqtybala numeric(18,8))
  declare @temp_tab2 table(goodsid integer,
                           subjectid integer,
                           tmsum numeric(18,8),
                           ftmsum numeric(18,8) )


begin
  insert into aa_msgoods (goodsid,subjectid,nyear,nmonth,tmsumdebit,tmsumcredit,tmsum,
                          ftmsumdebit,ftmsumcredit,ftmsum,tmqtysumdebit,tmqtysumcredit)
  select d.goodsid,d.subjectid,@nyear,@nmonth,sum(debit) tmsumdebit,sum(credit) tmsumcredit,
    sum((debit-credit)*dbo.fn_isnotzero(m.origintype-2002)) tmsum,
    sum(rawdebit) ftmsumdebit,sum(rawcredit) ftmsumcredit,
    sum((rawdebit-rawcredit)*dbo.fn_isnotzero(m.origintype-2002)) ftmsum,
    sum(dbo.fn_isnotzero(d.todebit)*quantity) tmqtysumdebit,sum((1-dbo.fn_isnotzero(d.todebit))*quantity) tmqtysumcredit
  from aa_creditem d
  inner join aa_cred m on m.credid=d.credid
  inner join al_subject s on d.subjectid=s.subjectid
  where s.togoods=1 and d.goodsid is not null
        and (d.credid<0 and (d.orderno=2 or d.orderno=3))
  group by d.subjectid,d.goodsid




  insert into @temp_tab1
  select d.goodsid,d.subjectid,sum((s.isdebit*2-1)*(debit+credit)) bmbala,
    sum((s.isdebit*2-1)*(rawdebit+rawcredit)) fbmbala,
    sum((s.isdebit*2-1)*(quantity))  bmqtybala
  from aa_creditem d
  inner join al_subject s on d.subjectid=s.subjectid
  where s.togoods=1 and d.goodsid is not null
    and (d.credid<0 and d.orderno=1)
  group by  d.subjectid,d.goodsid


  declare temp_tab1_cursor cursor for
  select * from @temp_tab1
  open  temp_tab1_cursor
  fetch next from  temp_tab1_cursor
  into  @goodsid,@subjectid,@bmbala,@fbmbala,@bmqtybala
  while @@fetch_status = 0
    begin
      update aa_msgoods set bmbala =@bmbala,fbmbala=@fbmbala,bmqtybala=@bmqtybala
      where goodsid = @goodsid and subjectid = @subjectid
            and nyear=@nyear and nmonth=@nmonth
      fetch next from  temp_tab1_cursor
      into  @goodsid,@subjectid,@bmbala,@fbmbala,@bmqtybala
    end
  close temp_tab1_cursor
  deallocate temp_tab1_cursor --http://www.52mvc.com


  insert into @temp_tab2
  select d.goodsid,d.subjectid,sum((s.isdebit*2-1)*(debit)) tmsum,
    sum((s.isdebit*2-1)*(rawdebit)) ftmsum
  from aa_creditem d
  inner join al_subject s on d.subjectid=s.subjectid
  where s.togoods=1 and d.goodsid is not null
      and (d.credid<0 and d.orderno=4)
  group by  d.subjectid,d.goodsid


  declare temp_tab2_cursor cursor for
  select * from @temp_tab2
  open  temp_tab2_cursor
  fetch next from  temp_tab2_cursor
  into  @goodsid,@subjectid,@tmsum,@ftmsum
  while @@fetch_status = 0
    begin
      update aa_msgoods set tmsum = @tmsum,ftmsum = @ftmsum
      where goodsid = @goodsid and subjectid = @subjectid
           and nyear=@nyear and nmonth=@nmonth
       fetch next from  temp_tab2_cursor
       into  @goodsid,@subjectid,@tmsum,@ftmsum
     end
  close temp_tab2_cursor
  deallocate temp_tab2_cursor
end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值