首页 新闻 会员 周边

如何使用存储过程返回值

0
悬赏园豆:5 [已解决问题] 解决于 2013-02-28 07:57

我的存储过程实际的内容如下:

ALTER proc [dbo].[brpbomson] @cinvcode varchar(50), @dec3 decimal(18,4) output
as

--返回一个BOM的标准成本,可以直接推算到最底层
--第一步建立一个临时表#re,如果已经存在,就删除,重新建
if object_id('tempdb.dbo.#reb') is not null
drop table #reb
CREATE 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 int
SET @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 > 0
begin
--如果level超过了10,表示有问题,因为嵌套15的情况很少见,除非是出错了
if @Level>15
return
SET @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.cinvcode
where a.sta = @Level-1 and a.free1 = 1
end

--第六步,有的时间出现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)
as
begin
declare @dec3 decimal(18,4)
exec brpbomson @cinvcode, @dec3 output
    return @dec3
end

执行函数的语句如下
UPDATE Ainventory
SET Ainventory.Dec3=dbo.brpbomsonfun(Ainventory.cinvcode)
FROM Cbom LEFT JOIN Ainventory ON Cbom.cinvcode=Ainventory.cinvcode

报错如下:
消息 557,级别 16,状态 2,过程 brpbomsonfun,第 6 行
只有函数和扩展存储过程才能从函数内部执行。

不知如何解决,请高人明示

zhengyingcan的主页 zhengyingcan | 初学一级 | 园豆:12
提问于:2013-02-25 10:12
< >
分享
最佳答案
0

存储过程最后加SELECT @dec3或者 returned  @dec3

 

SELECT @dec3=isnull(SUM(a.intb*b.D” 这里只是对变量赋值。不会returned出来。

收获园豆:3
luofer | 菜鸟二级 |园豆:468 | 2013-02-25 10:53

存储过程 后面有

--返回标准成本
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
还是报错了

zhengyingcan | 园豆:12 (初学一级) | 2013-02-25 10:58

@zhengyingcan:

 

应该这么写: 

SELECT @dec3=isnull(SUM(..........

SELECT @dec3 --上一句是赋值,这一句才是返回结果集

--or:

--return @dec3

luofer | 园豆:468 (菜鸟二级) | 2013-02-25 11:56
其他回答(1)
0

直接return 一个值如果不想return 就 output值

收获园豆:2
li-peng | 园豆:954 (小虾三级) | 2013-02-25 15:30

那怎么写呢

支持(0) 反对(0) zhengyingcan | 园豆:12 (初学一级) | 2013-02-25 15:31

@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 
支持(0) 反对(0) li-peng | 园豆:954 (小虾三级) | 2013-02-25 15:36
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册