我的存储过程实际的内容如下:ALTER proc [dbo].[brpbomson] @cinvcode varchar(50), @dec3 decimal(18,4) outputas--返回一个BOM的标准成本,可以直接推算到最底层--第一步建立一个临时表#re,如果已经存在,就删除,重新建if object_id('tempdb.dbo.#reb') is not nulldrop table #rebCREATE TABLE #reb (id int NULL, cinvcode varchar(50) NULL, intb float NULL,free1 bit NULL, free2 bit NULL, free3 bit NULL, --是否批次管理 sta int NULL,--层次,一定要pcinvcode varchar(50) NULL)--父编码-- 第二步 定义一个变量DECLARE @Level intSET @Level = 1--第三步,追加查询把订单资料追加到临时表insert into #reb(a.id,a.cinvcode,a.intb,b.free1,b.free2,free3,pcinvcode,sta)select b.irow,b.cinvcode,b.de1/b.de2*(1.00+b.de3/100.00-b.de4/100.00) as sumquan,c.free1,c.free2,c.free3,'',@Level from Cbom a left join Cbomson b on a.bomid=b.bomid left join Ainventory c on b.cinvcode=c.cInvCode where a.cinvcode like @cinvcode order by b.irow --循环增加while @@rowcount > 0begin--如果level超过了10,表示有问题,因为嵌套15的情况很少见,除非是出错了if @Level>15returnSET @Level = @Level + 1 --追加查询,把上一层性质是自制属性的,添加进去.(1+d.de3/100-d.de4/100) de3为增加损耗率,de4为扣除率insert into #reb(id,cinvcode,intb,free1,free2,pcinvcode,free3,sta)select a.id,d.cinvcode, a.intb*1.00*d.de1/d.de2*(1.00+d.de3/100.00-d.de4/100.00) as sumquan,b.free1,b.free2,a.cinvcode,b.free3,sta=@Level from #reb a left join cbom c on a.cinvcode = c.cinvcode left join cbomson d on c.bomid=d.bomid left join ainventory b on d.cinvcode=b.cinvcodewhere a.sta = @Level-1 and a.free1 = 1end--第六步,有的时间出现NULL,不知道为什么会这样子,以后找到原因会删掉这一条delete #reb where cinvcode is null--第七步,得到BRP运算结果select a.id as irow,a.cinvcode,b.cInvName,b.cInvStd,b.ccomunitname,a.intb as sumquan,'' as remark from #reb a left join Ainventory b on a.cinvcode=b.cInvCode order by a.cinvcode--返回标准成本SELECT @dec3=isnull(SUM(a.intb*b.Dec1),0) FROM #reb a left join Ainventory b on a.cinvcode=b.cInvCode where a.free2=1函数的内容如下:ALTER function [dbo].[brpbomsonfun] (@cinvcode varchar(50)) returns decimal(18,4)asbegindeclare @dec3 decimal(18,4)exec brpbomson @cinvcode, @dec3 output return @dec3end执行函数的语句如下UPDATE AinventorySET Ainventory.Dec3=dbo.brpbomsonfun(Ainventory.cinvcode)FROM Cbom LEFT JOIN Ainventory ON Cbom.cinvcode=Ainventory.cinvcode报错如下:消息 557,级别 16,状态 2,过程 brpbomsonfun,第 6 行只有函数和扩展存储过程才能从函数内部执行。不知如何解决,请高人明示
4 回答
慕哥6287543
TA贡献1831条经验 获得超10个赞
@zhengyingcan:
---创建存储过程 if exists(select 1 from sysobjects where name='sp_myReVaue') drop procedure sp_myReVaue go create procedure sp_myReVaue as return 111 go ---执行存储过程 DECLARE @reV int exec @reV=sp_myReVaue select @reV
- 4 回答
- 0 关注
- 641 浏览
添加回答
举报
0/150
提交
取消