一个存储过程(32分)

  • 主题发起人 主题发起人 月满C楼
  • 开始时间 开始时间

月满C楼

Unregistered / Unconfirmed
GUEST, unregistred user!
有点长,但很简单,打印出来的SQL能够执行,但就在存储过程内不能执行<br>CREATE procedure up_ylpf(<br>&nbsp; @jhrq1 varchar(200),<br>&nbsp; @jhrq2 varchar(200),<br>&nbsp; @nkxh varchar(200),<br>&nbsp; @htbh varchar(200),<br>&nbsp; @khmc varchar(200)<br>)<br>as begin<br>&nbsp; &nbsp; declare <br>&nbsp; &nbsp; &nbsp; @sql varchar(4000)<br>&nbsp; &nbsp; &nbsp; set @sql='<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a.[ID],<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b.TechnicsId,--to right join<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;khmc,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;htbh,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nkxh,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;gylm,/*钴盐料名*/<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;slz,/*深料重*/<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;blz,/*白料重*/<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;qlz,/*透料重*/<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tlz into #jhbf /*浅料重*/ <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from jhbf a ,bomTechnicsMaster b <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; where [dbo].GetModel(a.nkxh)=b.model <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; and [dbo].GetColor(a.nkxh)=b.color<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; and jhrq&gt;=''' + @jhrq1 + ''' and jhrq&lt;''' +@jhrq2 + ''''<br>&nbsp; &nbsp; if @nkxh&lt;&gt;'' <br>&nbsp; &nbsp; &nbsp; set @sql=@sql + ' and a.nkxh like ''' + @nkxh + '%'''<br>&nbsp; &nbsp; <br>&nbsp; &nbsp; if @htbh&lt;&gt;'' <br>&nbsp; &nbsp; &nbsp; set @sql=@sql + ' and a.htbh like ''' + @htbh + '%'''<br>&nbsp; &nbsp; <br>&nbsp; &nbsp; if @khmc&lt;&gt;''<br>&nbsp; &nbsp; &nbsp; set @sql=@sql + ' and a.khmc like ''%' + @khmc + '%'''<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; set @sql=@sql + ' order by nkxh'<br><br>&nbsp; &nbsp; <br>&nbsp; &nbsp; /*cope with TechItem_ls*/<br>&nbsp; &nbsp; set @sql=@sql + char(13) + char(13)<br>&nbsp; &nbsp; set @sql=@sql +<br>&nbsp; &nbsp; ' select<br>&nbsp; &nbsp; &nbsp; a.[ID], /*to join #jhbf*/<br>&nbsp; &nbsp; &nbsp; [PKNO]=1, /*to make the order 深白透浅*/<br>&nbsp; &nbsp; &nbsp; b.TechnicsId, /*right join bomPaint*/<br>&nbsp; &nbsp; &nbsp; b.MasterId, /*to join #jhbf in report*/<br>&nbsp; &nbsp; &nbsp; b.Technics,<br>&nbsp; &nbsp; &nbsp; a.slz as lz into #TechItem<br>&nbsp; &nbsp; from #jhbf a,bomTechnicsItem b<br>&nbsp; &nbsp; where a.TechnicsId=b.MasterId<br>&nbsp; &nbsp; &nbsp; and ltrim(rtrim(b.Technics))=''深''' &nbsp;<br>&nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; set @sql=@sql + char(13) + char(13)<br>&nbsp; &nbsp; set @sql=@sql + <br>&nbsp; &nbsp; ' insert into #TechItem <br>&nbsp; &nbsp; select<br>&nbsp; &nbsp; &nbsp; a.[ID], /*to join #jhbf*/<br>&nbsp; &nbsp; &nbsp; [PKNO]=2, /*to make the order 深白透浅*/<br>&nbsp; &nbsp; &nbsp; b.TechnicsId, /*right join bomPaint*/<br>&nbsp; &nbsp; &nbsp; b.MasterId, /*to join #jhbf in report*/<br>&nbsp; &nbsp; &nbsp; b.Technics,<br>&nbsp; &nbsp; &nbsp; a.blz as lz <br>&nbsp; &nbsp; from #jhbf a,bomTechnicsItem b<br>&nbsp; &nbsp; where a.TechnicsId=b.MasterId<br>&nbsp; &nbsp; &nbsp; and ltrim(rtrim(b.Technics))=''白'''<br>&nbsp; &nbsp; <br>&nbsp; &nbsp; set @sql=@sql + char(13) + char(13)<br>&nbsp; &nbsp; set @sql=@sql + <br>&nbsp; &nbsp; ' insert into #TechItem <br>&nbsp; &nbsp; select <br>&nbsp; &nbsp; &nbsp; a.[ID], /*to join #jhbf*/<br>&nbsp; &nbsp; &nbsp; [PKNO]=3, /*to make the order 深白透浅*/<br>&nbsp; &nbsp; &nbsp; b.TechnicsId, /*right join bomPaint*/<br>&nbsp; &nbsp; &nbsp; b.MasterId, /*to join #jhbf in report*/<br>&nbsp; &nbsp; &nbsp; b.Technics,<br>&nbsp; &nbsp; &nbsp; a.tlz as lz &nbsp;<br>&nbsp; &nbsp; from #jhbf a,bomTechnicsItem b<br>&nbsp; &nbsp; where a.TechnicsId=b.MasterId<br>&nbsp; &nbsp; &nbsp; and ltrim(rtrim(b.Technics))=''透''' &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; set @sql=@sql + char(13) + char(13)<br>&nbsp; &nbsp; set @sql=@sql + &nbsp; &nbsp; <br>&nbsp; &nbsp; ' insert into #TechItem <br>&nbsp; &nbsp; select<br>&nbsp; &nbsp; &nbsp; a.[ID], /*to join #jhbf*/<br>&nbsp; &nbsp; &nbsp; [PKNO]=4, /*to make the order 深白透浅*/<br>&nbsp; &nbsp; &nbsp; b.TechnicsId, /*right join bomPaint*/<br>&nbsp; &nbsp; &nbsp; b.MasterId, /*to join jhbf_ls in report*/<br>&nbsp; &nbsp; &nbsp; b.Technics,<br>&nbsp; &nbsp; &nbsp; a.qlz as lz <br>&nbsp; &nbsp; from #jhbf a,bomTechnicsItem b<br>&nbsp; &nbsp; where a.TechnicsId=b.MasterId<br>&nbsp; &nbsp; &nbsp; and ltrim(rtrim(b.Technics))=''浅'''<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp;/*cope with bomPaint_ls*/<br><br>&nbsp; &nbsp; set @sql=@sql + char(13) + char(13)<br>&nbsp; &nbsp; set @sql=@sql + &nbsp; &nbsp;<br>&nbsp; &nbsp; ' select <br>&nbsp; &nbsp; &nbsp; a.[ID], /*to join #TechItem*/<br>&nbsp; &nbsp; &nbsp; [ID2]=1, /* to make order*/<br>&nbsp; &nbsp; &nbsp; b.TechnicsId, /*to join #TechItem*/<br>&nbsp; &nbsp; &nbsp; ''深'' as colorname,<br>&nbsp; &nbsp; &nbsp; b.DyeName,<br>&nbsp; &nbsp; &nbsp; a.lz,<br>&nbsp; &nbsp; &nbsp; b.Amount,<br>&nbsp; &nbsp; &nbsp; isnull(a.lz,0) * isnull(b.Amount,0) * 10 as weight into #bomPaint<br>&nbsp; &nbsp; from #TechItem a,bomPaint b<br>&nbsp; &nbsp; where a.TechnicsId=b.TechnicsId<br>&nbsp; &nbsp; &nbsp; and ltrim(rtrim(a.Technics))=''深''' &nbsp;<br>&nbsp; &nbsp; <br>&nbsp; &nbsp; set @sql=@sql + char(13) + char(13)<br>&nbsp; &nbsp; set @sql=@sql + &nbsp;<br>&nbsp; &nbsp; ' insert into #bomPaint <br>&nbsp; &nbsp; select <br>&nbsp; &nbsp; &nbsp; a.[ID],<br>&nbsp; &nbsp; &nbsp; [ID2]=2,<br>&nbsp; &nbsp; &nbsp; b.TechnicsId,<br>&nbsp; &nbsp; &nbsp; ''白'' as colorname,<br>&nbsp; &nbsp; &nbsp; b.DyeName,<br>&nbsp; &nbsp; &nbsp; a.lz,<br>&nbsp; &nbsp; &nbsp; b.Amount,<br>&nbsp; &nbsp; &nbsp; isnull(a.lz,0) * isnull(b.Amount,0) * 10 as weight <br>&nbsp; &nbsp; from #TechItem a,bomPaint b<br>&nbsp; &nbsp; where a.TechnicsId=b.TechnicsId<br>&nbsp; &nbsp; &nbsp; and ltrim(rtrim(a.Technics))=''白''' &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; set @sql=@sql + char(13) + char(13)<br>&nbsp; &nbsp; set @sql=@sql + &nbsp; &nbsp;<br>&nbsp; &nbsp; ' insert into #bomPaint &nbsp;<br>&nbsp; &nbsp; &nbsp; select <br>&nbsp; &nbsp; &nbsp; a.[ID],<br>&nbsp; &nbsp; &nbsp; [ID]=3,<br>&nbsp; &nbsp; &nbsp; b.TechnicsId,<br>&nbsp; &nbsp; &nbsp; ''透'' as colorname,<br>&nbsp; &nbsp; &nbsp; b.DyeName,<br>&nbsp; &nbsp; &nbsp; a.lz,<br>&nbsp; &nbsp; &nbsp; b.Amount,<br>&nbsp; &nbsp; &nbsp; isnull(a.lz,0) * isnull(b.Amount,0) * 10 as weight <br>&nbsp; &nbsp; from #TechItem a,bomPaint b<br>&nbsp; &nbsp; where a.TechnicsId=b.TechnicsId<br>&nbsp; &nbsp; &nbsp; and ltrim(rtrim(a.Technics))=''透'''<br>&nbsp; &nbsp; &nbsp; <br><br>&nbsp; &nbsp; set @sql=@sql + char(13) + char(13)<br>&nbsp; &nbsp; set @sql=@sql + &nbsp; &nbsp;<br>&nbsp; &nbsp; ' insert into &nbsp;#bomPaint <br>&nbsp; &nbsp; select <br>&nbsp; &nbsp; a.[ID],<br>&nbsp; &nbsp; [ID]=4,<br>&nbsp; &nbsp; b.TechnicsId,<br>&nbsp; &nbsp; ''浅'' as colorname,<br>&nbsp; &nbsp; b.DyeName,<br>&nbsp; &nbsp; a.lz,<br>&nbsp; &nbsp; b.Amount,<br>&nbsp; &nbsp; isnull(a.lz,0) * isnull(b.Amount,0) * 10 as weight <br>&nbsp; from #TechItem a,bomPaint b<br>&nbsp; where a.TechnicsId=b.TechnicsId<br>&nbsp; &nbsp; and ltrim(rtrim(a.Technics))=''浅''' <br>&nbsp; <br>&nbsp; set @sql=@sql +char(13) + char(13)<br>&nbsp; set @sql=@sql + <br>&nbsp; '<br>&nbsp; &nbsp; select * from #jhbf<br>&nbsp; &nbsp; select distinct * from #TechItem<br>&nbsp; &nbsp; select distinct * from #bomPaint<br>&nbsp; &nbsp; drop table #jhbf<br>&nbsp; &nbsp; drop table &nbsp;#TechItem<br>&nbsp; &nbsp;drop table #bomPaint<br>&nbsp; ' <br>&nbsp; &nbsp;print @sql &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp;exec @sql &nbsp; &nbsp; &nbsp;<br>end<br>GO<br>//测试<br>exec up_ylpf '2008-7-1','2008-8-1','','',''<br>//打印出来的SQL,复制到查询分析器能够执行,并返回结果集<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a.[ID],<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b.TechnicsId,--to right join<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;khmc,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;htbh,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nkxh,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;gylm,/*钴盐料名*/<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;slz,/*深料重*/<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;blz,/*白料重*/<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;qlz,/*透料重*/<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tlz into #jhbf /*浅料重*/ <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from jhbf a ,bomTechnicsMaster b <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; where [dbo].GetModel(a.nkxh)=b.model <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; and [dbo].GetColor(a.nkxh)=b.color<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; and jhrq&gt;='2008-7-1' and jhrq&lt;'2008-8-1' order by nkxh<br><br>&nbsp;select<br>&nbsp; &nbsp; &nbsp; a.[ID], /*to join #jhbf*/<br>&nbsp; &nbsp; &nbsp; [PKNO]=1, /*to make the order 深白透浅*/<br>&nbsp; &nbsp; &nbsp; b.TechnicsId, /*right join bomPaint*/<br>&nbsp; &nbsp; &nbsp; b.MasterId, /*to join #jhbf in report*/<br>&nbsp; &nbsp; &nbsp; b.Technics,<br>&nbsp; &nbsp; &nbsp; a.slz as lz into #TechItem<br>&nbsp; &nbsp; from #jhbf a,bomTechnicsItem b<br>&nbsp; &nbsp; where a.TechnicsId=b.MasterId<br>&nbsp; &nbsp; &nbsp; and ltrim(rtrim(b.Technics))='深'<br><br>&nbsp;insert into #TechItem <br>&nbsp; &nbsp; select<br>&nbsp; &nbsp; &nbsp; a.[ID], /*to join #jhbf*/<br>&nbsp; &nbsp; &nbsp; [PKNO]=2, /*to make the order 深白透浅*/<br>&nbsp; &nbsp; &nbsp; b.TechnicsId, /*right join bomPaint*/<br>&nbsp; &nbsp; &nbsp; b.MasterId, /*to join #jhbf in report*/<br>&nbsp; &nbsp; &nbsp; b.Technics,<br>&nbsp; &nbsp; &nbsp; a.blz as lz <br>&nbsp; &nbsp; from #jhbf a,bomTechnicsItem b<br>&nbsp; &nbsp; where a.TechnicsId=b.MasterId<br>&nbsp; &nbsp; &nbsp; and ltrim(rtrim(b.Technics))='白'<br><br>&nbsp;insert into #TechItem <br>&nbsp; &nbsp; select <br>&nbsp; &nbsp; &nbsp; a.[ID], /*to join #jhbf*/<br>&nbsp; &nbsp; &nbsp; [PKNO]=3, /*to make the order 深白透浅*/<br>&nbsp; &nbsp; &nbsp; b.TechnicsId, /*right join bomPaint*/<br>&nbsp; &nbsp; &nbsp; b.MasterId, /*to join #jhbf in report*/<br>&nbsp; &nbsp; &nbsp; b.Technics,<br>&nbsp; &nbsp; &nbsp; a.tlz as lz &nbsp;<br>&nbsp; &nbsp; from #jhbf a,bomTechnicsItem b<br>&nbsp; &nbsp; where a.TechnicsId=b.MasterId<br>&nbsp; &nbsp; &nbsp; and ltrim(rtrim(b.Technics))='透'<br><br>&nbsp;insert into #TechItem <br>&nbsp; &nbsp; select<br>&nbsp; &nbsp; &nbsp; a.[ID], /*to join #jhbf*/<br>&nbsp; &nbsp; &nbsp; [PKNO]=4, /*to make the order 深白透浅*/<br>&nbsp; &nbsp; &nbsp; b.TechnicsId, /*right join bomPaint*/<br>&nbsp; &nbsp; &nbsp; b.MasterId, /*to join jhbf_ls in report*/<br>&nbsp; &nbsp; &nbsp; b.Technics,<br>&nbsp; &nbsp; &nbsp; a.qlz as lz <br>&nbsp; &nbsp; from #jhbf a,bomTechnicsItem b<br>&nbsp; &nbsp; where a.TechnicsId=b.MasterId<br>&nbsp; &nbsp; &nbsp; and ltrim(rtrim(b.Technics))='浅'<br><br>&nbsp;select <br>&nbsp; &nbsp; &nbsp; a.[ID], /*to join #TechItem*/<br>&nbsp; &nbsp; &nbsp; [ID2]=1, /* to make order*/<br>&nbsp; &nbsp; &nbsp; b.TechnicsId, /*to join #TechItem*/<br>&nbsp; &nbsp; &nbsp; '深' as colorname,<br>&nbsp; &nbsp; &nbsp; b.DyeName,<br>&nbsp; &nbsp; &nbsp; a.lz,<br>&nbsp; &nbsp; &nbsp; b.Amount,<br>&nbsp; &nbsp; &nbsp; isnull(a.lz,0) * isnull(b.Amount,0) * 10 as weight into #bomPaint<br>&nbsp; &nbsp; from #TechItem a,bomPaint b<br>&nbsp; &nbsp; where a.TechnicsId=b.TechnicsId<br>&nbsp; &nbsp; &nbsp; and ltrim(rtrim(a.Technics))='深'<br><br>&nbsp;insert into #bomPaint <br>&nbsp; &nbsp; select <br>&nbsp; &nbsp; &nbsp; a.[ID],<br>&nbsp; &nbsp; &nbsp; [ID2]=2,<br>&nbsp; &nbsp; &nbsp; b.TechnicsId,<br>&nbsp; &nbsp; &nbsp; '白' as colorname,<br>&nbsp; &nbsp; &nbsp; b.DyeName,<br>&nbsp; &nbsp; &nbsp; a.lz,<br>&nbsp; &nbsp; &nbsp; b.Amount,<br>&nbsp; &nbsp; &nbsp; isnull(a.lz,0) * isnull(b.Amount,0) * 10 as weight <br>&nbsp; &nbsp; from #TechItem a,bomPaint b<br>&nbsp; &nbsp; where a.TechnicsId=b.TechnicsId<br>&nbsp; &nbsp; &nbsp; and ltrim(rtrim(a.Technics))='白'<br><br>&nbsp;insert into #bomPaint &nbsp;<br>&nbsp; &nbsp; &nbsp; select <br>&nbsp; &nbsp; &nbsp; a.[ID],<br>&nbsp; &nbsp; &nbsp; [ID]=3,<br>&nbsp; &nbsp; &nbsp; b.TechnicsId,<br>&nbsp; &nbsp; &nbsp; '透' as colorname,<br>&nbsp; &nbsp; &nbsp; b.DyeName,<br>&nbsp; &nbsp; &nbsp; a.lz,<br>&nbsp; &nbsp; &nbsp; b.Amount,<br>&nbsp; &nbsp; &nbsp; isnull(a.lz,0) * isnull(b.Amount,0) * 10 as weight <br>&nbsp; &nbsp; from #TechItem a,bomPaint b<br>&nbsp; &nbsp; where a.TechnicsId=b.TechnicsId<br>&nbsp; &nbsp; &nbsp; and ltrim(rtrim(a.Technics))='透'<br><br>&nbsp;insert into &nbsp;#bomPaint <br>&nbsp; &nbsp; select <br>&nbsp; &nbsp; a.[ID],<br>&nbsp; &nbsp; [ID]=4,<br>&nbsp; &nbsp; b.TechnicsId,<br>&nbsp; &nbsp; '浅' as colorname,<br>&nbsp; &nbsp; b.DyeName,<br>&nbsp; &nbsp; a.lz,<br>&nbsp; &nbsp; b.Amount,<br>&nbsp; &nbsp; isnull(a.lz,0) * isnull(b.Amount,0) * 10 as weight <br>&nbsp; from #TechItem a,bomPaint b<br>&nbsp; where a.TechnicsId=b.TechnicsId<br>&nbsp; &nbsp; and ltrim(rtrim(a.Technics))='浅'<br><br><br>&nbsp; &nbsp; select * from #jhbf<br>&nbsp; &nbsp; select distinct * from #TechItem<br>&nbsp; &nbsp; select distinct * from #bomPaint<br>&nbsp; &nbsp; drop table #jhbf<br>&nbsp; &nbsp; drop table &nbsp;#TechItem<br>&nbsp; &nbsp;drop table #bomPaint
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
738
import
I
I
回复
0
查看
893
import
I
I
回复
0
查看
719
import
I
后退
顶部