问题原因:请先做好数据备份!问题原因是年度结转后ia_subsidiary表及ia_summary表中期初记录有错误。执行完附件中的语句后需执行重算存货总帐工具,先点‘修复存货总帐’,再点‘重算存货总帐’即可使上下年的存货期初完全一致。 use ufdata_001_2006 select cwhcode,cinvcode,cinvhead into km from ia_subsidiary where imonth=0 and cvoutype=’34’ group by cwhcode,cinvcode,cinvhead having cinvhead is not null delete ia_subsidiary from ia_subsidiary where autoid not in (select max(autoid) from ia_subsidiary where imonth=0 and cvoutype=’34’ group by cwhcode,cinvcode ) and imonth=0 and cvoutype=’34’ update ia_subsidiary set iainquantity=(case when a.inum=0 then null when a.inum is null then null else a.inum end), iincost=(case when a.inum=0 then null when a.inum is null then null else a.imoney/a.inum end), iainprice=a.imoney from ia_subsidiary full outer join (select cwhcode,cinvcode,inum,imoney from UFDATA_001_2005..ia_summary where imonth=12 )a on ia_subsidiary.cwhcode=a.cwhcode and ia_subsidiary.cinvcode=a.cinvcode where (isnull(ia_subsidiary.iainquantity,0)isnull(a.inum,0) or isnull(ia_subsidiary.iainprice,0)isnull(a.imoney,0)) and ia_subsidiary.autoid is not null and ia_subsidiary.imonth=0 and cvoutype=’34’ delete from ia_subsidiary where cvoutype=’34’ and imonth=0 and (isnull(iainquantity,0)=0 and isnull(iainprice,0)=0) update ia_subsidiary set cinvhead=km.cinvhead From km full outer join ia_subsidiary on ia_subsidiary.cwhcode=km.cwhcode and ia_subsidiary.cinvcode=km.cinvcode where km.cwhcode is not null and cvoutype=’34’ and imonth=0 and isnull(ia_subsidiary.cinvhead,0)km.cinvhead SET NOCOUNT ON declare @cVouCode as varchar(30), @cwhcode as varchar(10),@cinvcode as varchar(20),@iainquantity as float,@iincost as float,@iainprice as money set @cVouCode=(select max(cVouCode)-10000 FROM [IA_Subsidiary] where cvoutype=’34’ and imonth=0) DECLARE aa CURSOR FOR select b.cwhcode,b.cinvcode,(case when inum=0 then null when inum is null then null else inum end)inum, (case when inum=0 then null when inum is null then null else imoney/inum end) cost, imoney from( select cwhcode,cinvcode,sum(isnull(iainquantity,0))q,sum(isnull(iainprice,0)) p from ia_subsidiary where cvoutype=’34’and imonth=0 group by cwhcode,cinvcode)a full outer join ( select cwhcode,cinvcode,inum,imoney from UFDATA_001_2005..ia_summary where imonth=12 and(inum0 or imoney0)) b on a.cwhcode=b.cwhcode and a.cinvcode=b.cinvcode where isnull(a.q,0)isnull(b.inum,0) or isnull(a.p,0)isnull(imoney,0) OPEN aa FETCH NEXT FROM aa INTO @cwhcode,@cinvcode,@iainquantity,@iincost,@iainprice WHILE (@@fetch_status=0) BEGIN insert into ia_subsidiary([bRdFlag], [cVouCode], [ID], [dVouDate], [dKeepDate], [iMonth], [cVouType], [cWhCode], [cInvCode], [cHandler], [iAInQuantity],[iInCost], [iAInPrice], [cAccounter], [cMaker], [bMoneyFlag], [bSale], [cPZID]) values(1, @cVouCode,@cVouCode,’2005-12-31′,’2005-12-31′, 0, 34, @cWhCode, @cInvCode,’朱虹’,@iAInQuantity,@iInCost,@iAInPrice,’朱虹’,’朱虹’,1,0,0) FETCH NEXT FROM aa INTO @cwhcode,@cinvcode,@iainquantity,@iincost,@iainprice set @[email protected]+1 END CLOSE aa DEALLOCATE aa GO drop table km