一个MRP中算毛需求的SQL储存过程(100分)

  • 主题发起人 主题发起人 Amos.mao
  • 开始时间 开始时间
A

Amos.mao

Unregistered / Unconfirmed
GUEST, unregistred user!
{<br>简单说明<br>1: Select psn.materysn,planshu from MRPplan where psn=xxx (xxx 为传入参数),对应数据集为rsq1<br>&nbsp; &nbsp;If rsq1.RecordCount&gt;0 then<br>&nbsp; &nbsp; &nbsp;Begin<br>&nbsp; &nbsp; &nbsp; &nbsp;For I:=1 To rsq1.RecordCount Do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fstr:=rsq1.materysn<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Select * From mrpplanpsn &nbsp;where materysn=fstr and psn=xxx (xxx 为传入参数),对应数据集为rst<br>&nbsp; &nbsp; If rst.RecordCount&gt;0 Then &nbsp;将对应 planshu 数量进行相加<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Else <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rst.Append //新写入对应内容<br> &nbsp; &nbsp; End;<br> &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 进行BOM资料对应查找,rsq2.SQL.Add('select materysn from bom where fmaterysn=fstr)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If Rsq2.RecordCount Then &nbsp;SGmaterysn(Fstr;planshu) 进行循环<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br> End //For I:=1 To rsq1.RecordCount Do<br>&nbsp; &nbsp; &nbsp;End;<br>&nbsp;<br>1:由生产单MRPPlan 查找对应的资料 &nbsp; &nbsp;<br>2:计算制造该产品所需要的物料 计算依据为 毛需求量=生产批量*物料清单中的单位用量<br>3:将相关计算后的数据储存至MRP需求计算资料库 mrpplanpsn 中 <br>}<br><br><br>procedure TMRPNR.BtnGRClick(Sender: TObject); //毛需求<br><br>var<br>&nbsp; rsq1,rsq2:TADOQuery;<br>&nbsp; rst:TADODataSet;<br>&nbsp; tstr,remark:string;<br>&nbsp; i,n:integer;<br>&nbsp; planshu:double;<br>begin<br>&nbsp;<br>&nbsp; tmpPSN := CmbPsn.Text;<br>&nbsp; adoconnwrite.Close ;<br>&nbsp; adoconnwrite.ConnectionString:=strconnection;<br>&nbsp; adoconnwrite.Open ;<br>&nbsp; adoconnwrite.BeginTrans ;<br>&nbsp; try<br>&nbsp; &nbsp; rsq1:=TADOQuery.Create(self);<br>&nbsp; &nbsp; rst:=TADODataSet.Create(self);<br>&nbsp; &nbsp; rsq2:=TADOQuery.Create(self);<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; rsq2.Connection:=adoconnread;<br>&nbsp; &nbsp; &nbsp; rsq1.Connection:=adoconnread;<br>&nbsp; &nbsp; &nbsp; rsq1.SQL.Clear ; &nbsp;//MRPplan 为 生产单<br>&nbsp; &nbsp; &nbsp; rsq1.SQL.Add('select materysn,planshu,consigndate from MRPplan where psn='+''''+ Trim(tmpPSN)+''''+'');<br>&nbsp; &nbsp; &nbsp; rsq1.Prepared ;<br>&nbsp; &nbsp; &nbsp; rsq1.Open ;<br>&nbsp; &nbsp; &nbsp; n:=rsq1.RecordCount;<br>&nbsp; &nbsp; &nbsp; If n &gt; 0 Then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; tmpDate:= rsq1.fieldbyname('consigndate').AsString ; //交货日期<br>&nbsp; &nbsp; &nbsp; &nbsp; rsq1.First ;<br>&nbsp; &nbsp; &nbsp; &nbsp; rst.Connection:=adoconnWrite;<br>&nbsp; &nbsp; &nbsp; &nbsp; For i:=1 To n do<br>&nbsp; &nbsp; &nbsp; &nbsp; Begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fstr:=trim(rsq1.fieldbyname('Materysn').asstring); //<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Planshu:= rsq1.fieldbyname('planshu').AsFloat ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tstr:='select materysn,gr,fu,psn,consigndate,remark';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tstr:=tstr+ ' from mrpplanpsn';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tstr:=tstr+ ' where materysn='+''''+ Trim(fstr)+''''+'and psn='+''''+ Trim(CmbPsn.Text)+''''+'';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rst.Close ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rst.CommandText:=tstr;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rst.Prepared ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rst.Open ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (rst.RecordCount &gt;0) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rst.Edit ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rst.FieldByName('gr').AsFloat:=rst.FieldByName('gr').AsFloat+ planshu;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rst.FieldByName('remark').AsString:=rst.FieldByName('remark').AsString+','+ fstr;<br> &nbsp; &nbsp;rst.Post;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rst.Append ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rst.FieldByName('materysn').AsString :=trim(rsq1.fieldbyname('materysn').AsString) ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rst.FieldByName('gr').AsCurrency :=planshu; &nbsp;//毛需求 初始数据批量数目MRPPlan的planshu (批量数)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rst.FieldByName('fu').AsCurrency :=0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rst.FieldByName('psn').AsString :=trim(tmppsn);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rst.FieldByName('consigndate').AsDateTime :=strtodate(tmpdate); //交货日期<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rst.FieldByName('remark').AsString:=fstr; //备注说明<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rst.Post ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//if rst.RecordCount &gt;0 then<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rsq2.Close ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rsq2.SQL.Clear ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rsq2.SQL.Add('select materysn from bom where fmaterysn='+''''+Trim(rsq1.fieldbyname('materysn').AsString)+''''+'');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rsq2.Prepared ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rsq2.Open ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if rsq2.RecordCount &gt;0 then SGmaterysn(rsq1.FieldByName('materysn').AsString, Planshu);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rsq1.Next ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;// For i:=1 To n do<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tstr:='update mrpplantou';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tstr:= tstr+' set';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tstr:= tstr+' state='+''''+'3'+''''+'';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tstr:= tstr+' where psn='+''''+Trim(tmpPsn)+''''+'';<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rsq2.Close ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rsq2.SQL.Clear ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rsq2.SQL.Add(tstr);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rsq2.Prepared ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rsq2.ExecSQL ;<br>&nbsp; &nbsp; &nbsp; end;//If n &gt; 0 Then<br><br>&nbsp; &nbsp; &nbsp; &nbsp;adoconnwrite.CommitTrans ;<br>&nbsp; &nbsp; &nbsp; &nbsp;adoconnwrite.Close;<br>&nbsp; &nbsp; &nbsp;finally<br>&nbsp; &nbsp; &nbsp; &nbsp; Screen.Cursor:=crDefault;<br>&nbsp; &nbsp; &nbsp; &nbsp; rsq1.Free ;<br>&nbsp; &nbsp; &nbsp; &nbsp; rsq2.Free ;<br>&nbsp; &nbsp; &nbsp; &nbsp; rst.Free ;<br>&nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; &nbsp;except<br>&nbsp; &nbsp; &nbsp; messagedlg('保存过程中出现错误,未保存!',mtinformation,[mbok],0);<br>&nbsp; &nbsp; &nbsp; adoconnwrite.RollbackTrans ;<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp;adoconnwrite.Close ;<br>&nbsp; &nbsp;Animate1.Active:=false;<br>&nbsp; &nbsp;Animate1.Visible :=false;<br><br>Display_rs2; &nbsp;//显示 Mrp需求 mrpplanpsn 计算的相关信息<br>//end; //If n &gt; 0 Then<br>end;<br><br><br>procedure TMRPNR.SGmaterysn(Fmaterysn:string;planshu:Double);<br>var<br>&nbsp; rsq1,rsq2:TADOQuery;<br>&nbsp; rst:TADODataSet;<br>&nbsp; i,n:integer;<br>&nbsp; remark:String;<br>begin<br>&nbsp; rsq1:=TADOQuery.Create(self);<br>&nbsp; rsq2:=TADOQuery.Create(self);<br>&nbsp; rst:=TADODataSet.Create(self);<br>&nbsp; try<br>&nbsp; &nbsp; rst.Connection :=AdoConnWrite;<br>&nbsp; &nbsp; rsq2.Connection :=adoconnread;<br>&nbsp; &nbsp; rsq1.Connection :=adoconnread;<br>&nbsp; &nbsp; rsq1.SQL.Clear;<br>&nbsp; &nbsp; rsq1.SQL.Add('select materysn,useshu from bom where fmaterysn='+''''+ Trim(Fmaterysn)+''''+' and useshu&gt;'+''''+'0'+''''+'');<br>&nbsp; &nbsp; rsq1.Prepared ;<br>&nbsp; &nbsp; rsq1.Open ;<br>&nbsp; &nbsp; n :=rsq1.RecordCount ;<br>&nbsp; &nbsp; If n &gt; 0 Then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; rsq1.First ;<br>&nbsp; &nbsp; &nbsp; For i:= 1 To n do<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rst.Close ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rst.CommandText :='select materysn,gr,fu,psn,consigndate,remark from mrpplanpsn where materysn='+''''+ Trim(rsq1.fieldbyname('Materysn').AsString)+''''+' and psn='+''''+Trim(tmpPsn)+''''+'';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rst.Prepared ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rst.Open ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If rst.RecordCount &gt; 0 Then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rst.edit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rst.FieldByName('gr').AsFloat:=rst.FieldByName('gr').AsFloat+planshu*rsq1.fieldbyname('useshu').AsFloat;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rst.FieldByName('remark').AsString:=rst.FieldByName('remark').AsString+','+ fstr;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rst.Post ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rst.Append ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rst.FieldByName('materysn').AsString:=trim(rsq1.FieldByName('materysn').asstring);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rst.FieldByName('gr').AsFloat:= Planshu * rsq1.fieldbyname('useshu').AsFloat;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rst.FieldByName('fu').AsFloat:= 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rst.FieldByName('psn').AsString:= Trim(tmpPSN);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rst.FieldByName('consigndate').AsDateTime:= strtodate(tmpDate);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rst.FieldByName('remark').AsString:=fstr;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rst.Post ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; // If rst.RecordCount &gt; 0 Then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rsq2.Close ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rsq2.SQL.Clear ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rsq2.SQL.Add('select materysn from bom where fmaterysn='+''''+Trim(rsq1.fieldbyname('materysn').AsString)+''''+'');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rsq2.Prepared ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rsq2.Open ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if rsq2.RecordCount &gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SGmaterysn(rsq1.FieldByName('materysn').AsString, Planshu * rsq1.fieldbyname('useshu').asfloat);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rsq1.Next ;<br>&nbsp; &nbsp; &nbsp; &nbsp;end; //For i:= 1 To n do<br>&nbsp; &nbsp; end;//If n &gt; 0 Then<br>&nbsp; finally<br>&nbsp; &nbsp; rsq1.Free ;<br>&nbsp; &nbsp; rsq2.Free ;<br>&nbsp; &nbsp; rst.Free ;<br>&nbsp; end;<br>&nbsp;end;
 

Similar threads

I
回复
0
查看
772
import
I
I
回复
0
查看
693
import
I
I
回复
0
查看
558
import
I
后退
顶部