日期:2014-05-18  浏览次数:20409 次

由Delphi程序改写Sql(2000)的存储过程
各位前辈大家好,请帮我解决一个棘手的问题,我不会写SQL存储过程,目前一个Delphi程序执行非常的慢,想将其用SQL存储过程替代
请各位前辈帮忙,在线等
主要 资料表单有 MRPplan,MRPplantou (生产单资料表), mrpplanpsn(物料需求单资料表),bom(BOM 资料表) 四个,
其中关联字段为Psn(生产单号),materysn(物料编号)

从 MRPplan 中依据Psn查找资料进行循环,
a:如果mrpplanpsn 中有对应Psn的materysn则将MRPplan.planshu数量加到 mrpplanpsn.gr上
b:如果mrpplanpsn 中没有有对应Psn的materysn则将MRPplan中相关资料写入 mrpplanpsn
c:进行对MRPplan.materysn 资料进行BOM分解,若有子项则对子项进行以上动作

1: Select psn.materysn,planshu from MRPplan where psn=xxx (xxx 为传入参数),对应数据集为rsq1
  If rsq1.RecordCount>0 then
  Begin
  For I:=1 To rsq1.RecordCount Do
  Begin
  fstr:=rsq1.materysn
  Select * From mrpplanpsn where materysn=fstr and psn=xxx (xxx 为传入参数),对应数据集为rst1
  If rst1.RecordCount>0 Then 将对应 rst1.gr=rst1.gr+rsq1.planshu
  Else 
  Begin
  rst1.Append //新写入对应内容
rst1.materysn=rsq1.materysn
rst1.gr=rsq1.planshu //数量  
rst1.consigndate=rsq1.consigndate //交获日期
  ...
End;

  进行BOM资料对应查找,rsq2.SQL.Add('select materysn from bom where fmaterysn=fstr)
  If Rsq2.RecordCount Then SGmaterysn(Fstr;rsq1.planshu) 进行循环
  Rsq2.Next  
  End //For I:=1 To rsq1.RecordCount Do
  End;
 

SGmaterysn 过程如下:

 select materysn,useshu from bom where fmaterysn=Fstr and useshu>0
 n:=Rsq2.RecordCount  
 If n > 0 Then
 Begin
  For i:= 1 To n do
  begin
  Fstr=Rsq2.Materysn 
  planshu:=Rsq2.Materysn 
  select materysn,gr,fu,psn,consigndate,remark from mrpplanpsn where materysn=Rsq2.Materysn and psn=xxx

  If rst2.RecordCount > 0 Then 则将对应 rst2.gr=rst2.gr+rsq2.planshu
  Else 
  Begin
  rst2.Append //新写入对应内容
rst2.materysn=rsq2.materysn
rst2.gr=rsq2.planshu  
rst2.consigndate=rsq2.consigndate //交获日期
  ...
End;

  进行BOM资料对应查找,rsq2.SQL.Add('select materysn from bom where fmaterysn=fstr)
  If Rsq2.RecordCount >0 Then SGmaterysn(Fstr;planshu) 进行递归循环
  SGmaterysn(Fstr;planshu); 
  rsq2.Next ;
  end; //For i:= 1 To n do
  end;//If n > 0 Then


------解决方案--------------------
declare @MRPplan table
(
layer int,
Psn varchar(100),
materysn varchar(100),
planshu decimal(8,2),
f_planshu int,
cdate datetime
)
--这里忘改了,改成这样
f_planshu decimal(8,2),