首页 > 其他 > 详细

带返回值的存储过程

时间:2019-05-29 22:04:35      阅读:73      评论:0      收藏:0      [点我收藏+]

使用return关键字进行返回

遇到return关键字存储过程中的后续代码无条件不执行,既退出了当前的存储过程

根据返回值对存储过程的结果做出相应的处理

例子:

 1 --创建带返回值的存储过程
 2 
 3 /*
 4     向母婴用品中添加一条商品信息
 5 
 6 */
 7 use E_Market
 8 go
 9 if exists(select * from sysobjects where name=usp_InsertCommodityReturn)
10 drop proc usp_InsertCommodityReturn
11 
12 go
13 create proc usp_InsertCommodityReturn
14     @SortName varchar(50),  --类别名称
15     @CommodityName varchar(100),  --商品名称
16     @inprice money,   --进货价
17     @outprice money,  --销售价
18     @Amount int  --库存liang
19     
20 as
21     declare @sortid int
22     select @sortid=SortId from CommoditySort where SortName=@SortName
23     --根据类别名称判断类别编号是否存在
24     if @sortid is null
25         begin
26             return -1    --用-1代表类别名称不正确
27         end
28     --向商品信息表添加一条信息
29     insert into CommodityInfo(SortId,CommodityName,InPrice,OutPrice,Amount)
30     values(@SortId,@CommodityName,@inprice,@outprice,@Amount)
31     if @@ERROR > 0
32         begin
33             return 0   --用0代表插入信息失败
34         end
35     else 
36         begin
37             return @@identity
38         end
39 go
40 
41 --使用带返回值的存储过程,返回值,有三个,0,-1.商品编号
42 --使用显示调用
43 declare @Result int --接收存储过程的返回值
44 exec @Result=usp_InsertCommodityReturn @sortName=汇吃美食,@commodityName=好吃点,@inprice=3.5,@outprice=7.6,@amount=100
45 if @Result=-1
46     begin
47         print 对不起,输入的类别名称不存在!
48     end
49 else if @Result=0
50     begin
51         print插入信息失败!
52     end
53 else
54     begin
55         print 添加商品成功!商品编号为: + convert(varchar(5),@Result)
56     end
57 go
58 
59 select * from CommoditySort
60 select * from CommodityInfo

 

带返回值的存储过程

原文:https://www.cnblogs.com/zhangxudong-cnblogs/p/10946281.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!