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