日期:2014-05-17  浏览次数:20541 次

紧急求助:用游标取数来更新某一列的问题
如果来实现这种功能,第一行余额为37000,第二行的余额=37000+收款0-发出金额9000=28000,也就是余额是累加的。我写了一个存储过程,取出来的数不对,是把临时表里取到的所有的客户的余额都累加了。还望高手指点啊。
我的联系方式:QQ:26978895  TEL:18957719082

set nocount on
 create table #balancetemp


create index index_baltemp on #balancetemp(fdate,fdeptid,fcustid)

 declare @fdate char(10)
declare @fstarttime char(10)
declare @fendtime   char(10)
declare @fyear char(4)
declare @fperiod varchar(2)
declare @sumamount decimal(28,10) 
declare @fkthamount decimal(28,10)
declare @fentryid   int
declare @fitem varchar(100)
declare @fdept varchar(100)
declare @fcust varchar(100)
declare @bal   decimal(28,10)
select @fentryid=0
select @sumamount=0

select @fkthamount=0
--select * from t_systemprofile  Where FCategory = 'gl'
 Select @fyear =FValue From t_SystemProfile Where FCategory = 'gl' And FKey =  'fstartYear'
select @fperiod =FValue From t_SystemProfile Where FCategory = 'gl' And FKey =  'fstartperiod'

--定义游标,取出客户
declare @cust  int
declare @dept  int
declare cb_cust cursor  for select  f1,f2 from t_itemdetail  where fdetailid in (select fdetailid from t_itemdetailv ) and fdetailcount=2 and f1>0 and f2>0
order by f1,f2
OPEN cb_cust

FETCH NEXT FROM cb_cust 
INTO @cust,@dept

WHILE @@FETCH_STATUS = 0

begin 







--先取出当前期间的期初数

insert into  #balancetemp(fdate,fdeptid,fdept,fcustid,fcust,fbegbal,frecamount,ffyamount,fqtamount,fitemname,fthqty,
fthprice,foutamount,fkpqty,fkpprice,fkpamount,fwkpqty,fwkpprice,fwkpamount,fkthamount,fnote)
  select '' as fdate,f2.fdeptid,f2.fdeptname, fcustid,f1.fcustname,sum(tb.fbeginbalance),0,0 ,0,'',0,0,0,0,0,0,0,0,0,0,'C'as fnote from t_balance   tb
  inner join  (SELECT FDetailID ,b.FNumber AS F1Number ,b.fname as fdeptname,b.fitemid as  fdeptid,b.FNumber+'-'+b.fname F2 ,b.FLevel F1Level  ,b.FItemClassID F1ClassID  From 
t_ItemDetail a , t_Item b WHERE  a.F2 = b.FItemID AND b.FItemID <>0 ) F2 ON F2.FDetailID =tb.FDetailID  and tb.fcurrencyid=1
INNER JOIN 
( SELECT FDetailID ,b.FNumber AS F4Number ,b.fname as fcustname,b.fitemid as fcustid,b.FNumber+'-'+b.fname F1 ,b.FLevel F4Level  ,b.FItemClassID F4ClassID  From t_ItemDetail a , t_Item b WHERE  a.F1 = b.FItemID AND 
b.FItemID <>0 ) F1 ON F1.FDetailID =tb.FDetailID 
and tb.faccountid in 
(select faccountid from t_account where fnumber like '1400%') 

where -- fcustNAME='上海国精商贸有限公司'  and fdeptNAME='上海营销中心'  and fcurrencyid=1 and fperiod='9' --and&nb