tyming0920 2017-01-05 07:06 采纳率: 0%
浏览 2936

有一大段SQL语句,如何封装成一个存储过程,以方便其他程序调用?如何将其中的'a.b.c00' 部分用变量代替?

Create Table #Mutidata ( FIndex int IDENTITY,FEntryID INT, FBomInterid int, FItemID int null, FNeedQty decimal(28,14) default(0) null, FBOMLevel int null, FItemType int null, FParentID int default(0)null, FRate decimal(28,14) default(0) null, FHistory int default(0) null, FHaveMrp smallint default(0) null, FLevelString varchar(200) null, FBom int, FMaterielType int default(371) null,FOperID int default(0))
Create Table #MutiParentItem( FIndex int IDENTITY,FEntryID INT default(0), FBomInterid int, FItemID int null, FNeedQty decimal(28,14) default(0) null, FBOMLevel int null, FItemType int null, FParentID int default(0)null, FRate decimal(28,14) default(0) null, FHistory int default(0) null, FHaveMrp smallint default(0) null, FLevelString varchar(200) null , FBom int, FMaterielType int default(371) null,FOperID int default(0))
Create Table #Errors ( FIndex int IDENTITY, FType smallint default(0), FErrText varchar(355) )
go
Insert into #mutiParentItem (fbominterid,FItemID,FNeedQty,FBOMLevel,FParentID,FItemType,FBom) Select a.finterid, t1.FItemID,a.fqty, 0,0,t1.FErpClsID,t1.FItemID
From icbom a,t_ICItem t1 Where t1.FItemID = a.fitemid and a.fstatus=1 and t1.fnumber='a.b.c00' --如何将其中的'a.b.c00' 部分用变量代替(这个是一个物料代码)
declare @P1 int
set @P1=0
declare @P2 char(400)
set @P2=' '
exec PlanMutiBomExpandEx 50, 1, '01 1 1900 12:00:00:000AM', '01 1 2100 12:00:00:000AM', @P1 output, @P2 output
go
select a.FIndex,a.FitemID,a.FItemType, a.FBOMLevel,a.FLevelString FLevel,b.fnumber FNumber,b.fname FName,isnull(b.FModel,'') FModel,
a.FNeedQty FQty,d.FScrap,isnull(i.fname,'') FUseStatus,isnull(k.fprice,0) fprice,a.FNeedQty*isnull(k.fprice,0) 金额
from #Mutidata a
inner join t_icitem b on a.fitemid=b.fitemid
inner join icbomchild d on a.FBomInterid=d.finterid and a.FOperID=d.FOperID AND a.FEntryID=d.FEntryID and a.FItemID=d.FItemID
left outer join t_submessage i on b.fusestate=i.finterid
left outer join(select distinct fitemid,sum(a.famount)/sum(a.fqty) fprice
from icstockbillentry a
left join icstockbill b on a.finterid=b.finterid and b.ftrantype=1 and b.frob=1
where b.fdate between '2007-01-01' and '2016-12-31'
group by fitemid
union all
select distinct fitemid,sum(a.famount)/sum(a.fqty) fprice
from icstockbillentry a
left join icstockbill b on a.finterid=b.finterid and b.ftrantype=1 and b.frob=1
where b.fdate=(select max(z.fdate) from icstockbill z,icstockbillentry x where x.fitemid=a.fitemid and z.finterid=x.finterid and z.ftrantype=1 and z.frob=1 )
and FItemID not in(select distinct fitemid from icstockbillentry a
left join icstockbill b on a.finterid=b.finterid and b.ftrantype=1 and b.frob=1
where b.fdate between '2007-01-01' and '2016-12-31')
group by fitemid
)k on k.fitemid=a.fitemid
where 1=1

union
select a.FIndex,a.FitemID,a.FItemType, a.FBOMLevel,a.FLevelString FLevel,b.fnumber FNumber,b.fname FName,isnull(b.FModel,'') FModel,
a.FNeedQty FQty,d.FScrap,isnull(i.fname,'') FUseStatus,isnull(k.fprice,0),a.FNeedQty*isnull(k.fprice,0) 金额
from #Mutidata a
inner join t_icitem b on a.fitemid=b.fitemid
inner join iccustbomchild d on a.FBomInterid=d.finterid and a.FItemID=d.FItemID and a.FOperID=d.FOperID AND a.FEntryID=d.FEntryID
left outer join t_submessage i on b.fusestate=i.finterid
left outer join(select distinct fitemid,sum(a.famount)/sum(a.fqty) fprice
from icstockbillentry a
left join icstockbill b on a.finterid=b.finterid and b.ftrantype=1 and b.frob=1
where b.fdate between '2007-01-01' and '2016-12-31'
group by fitemid
union all
select distinct fitemid,sum(a.famount)/sum(a.fqty) fprice
from icstockbillentry a
left join icstockbill b on a.finterid=b.finterid and b.ftrantype=1 and b.frob=1
where b.fdate=(select max(z.fdate) from icstockbill z,icstockbillentry x where x.fitemid=a.fitemid and z.finterid=x.finterid and z.ftrantype=1 and z.frob=1 )
and FItemID not in(select distinct fitemid from icstockbillentry a
left join icstockbill b on a.finterid=b.finterid and b.ftrantype=1 and b.frob=1
where b.fdate between '2007-01-01' and '2016-12-31')
group by fitemid
)k on k.fitemid=a.fitemid
where 1=1
union
select a.FIndex,a.FitemID,a.FItemType, a.FBOMLevel,a.FLevelString FLevel,b.fnumber FNumber,b.fname FName,isnull(b.FModel,'') FModel,
a.FNeedQty FQty,0 FScrap,isnull(i.fname,'') FUseStatus,isnull(k.fprice,0),a.FNeedQty*isnull(k.fprice,0) 金额
from #Mutidata a
inner join t_icitem b on a.fitemid=b.fitemid
inner join icbom d on a.FBomInterid=d.finterid and a.FItemID=d.FItemID
left outer join t_submessage i on b.fusestate=i.finterid
left outer join(select distinct fitemid,sum(a.famount)/sum(a.fqty) fprice
from icstockbillentry a
left join icstockbill b on a.finterid=b.finterid and b.ftrantype=1 and b.frob=1
where b.fdate between '2007-01-01' and '2016-12-31'
group by fitemid
union all
select distinct fitemid,sum(a.famount)/sum(a.fqty) fprice
from icstockbillentry a
left join icstockbill b on a.finterid=b.finterid and b.ftrantype=1 and b.frob=1
where b.fdate=(select max(z.fdate) from icstockbill z,icstockbillentry x where x.fitemid=a.fitemid and z.finterid=x.finterid and z.ftrantype=1 and z.frob=1 )
and FItemID not in(select distinct fitemid from icstockbillentry a
left join icstockbill b on a.finterid=b.finterid and b.ftrantype=1 and b.frob=1
where b.fdate between '2007-01-01' and '2016-12-31')
group by fitemid
)k on k.fitemid=a.fitemid
where 1=1
order by FIndex desc
go
drop table #Mutidata
drop table #MutiParentItem
drop table #Errors

  • 写回答

3条回答 默认 最新

  • 纯菜鸟 2017-01-05 07:19
    关注

    那你就在数据库Procedures里新建一个存储过程被 重复的数值或字符 可以在开始时定义常量的。

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?