2014-11-11---------存储过程实例,光棍节

 1 -----------------------------------------
 2 --创建一个货物表:编号,货物名称,单位,价格,库存数量,备注。并输入条数据
 3 
 4 drop table kucun
 5 create table kucun
 6 (
 7     code int primary key,--编号列为主键
 8     name varchar(20),
 9     danwei varchar(10),
10     jiage money,
11     shengyu int,
12     beizhu varchar(50)
13 )
14 go
15 select *from kucun
16 insert into kucun values (1,'笔记本','',3,10,'尚宇 A5办公胶订本')
17 insert into kucun values (2,'中性笔','',1,20,'白雪 0.5黑色中性笔')
18 insert into kucun values (3,'保温杯','',15,8,'中宏 300ML保温杯')
19 insert into kucun values (4,'台式电脑','',2500,20,'联想商务电脑')
20 insert into kucun values (5,'键鼠','',30,15,'双飞燕键鼠套装')
21 insert into kucun values (6,'笔记本电脑','',4000,5,'DELL Inspiron15 3000(戴尔灵越)')
22 insert into kucun values (7,'无线鼠标','',80,7,'双飞燕无线光学鼠标')
23 insert into kucun values (8,'优盘','',70,10,'Kingston 16G')
24 insert into kucun values (9,'无线路由器','',80,6,'TP-Link 150M 双天线无线路由器')
25 insert into kucun values (10,'插排','',50,10,'公牛插排3M')
26 insert into kucun values (11,'未知','',10000,10,'这你也敢买')
27 update kucun set danwei='' where code=6
28 ---------------------------------------------
29 select *from kucun
30 --入库存储过程
31 --create proc jinchuProc
32 alter proc jinchuProc
33 @code int,
34 @name varchar(20),
35 @danwei varchar(10),
36 @jiage decimal(18,2),
37 @shengyu int,
38 @beizhu varchar(100)
39 as
40 begin
41     declare @count int
42     select @count=count(*) from kucun where code=@code
43     if @count=0 and @shengyu>0--仓库里没有的,新进来的货物
44     begin
45         insert into kucun values(@code,@name,@danwei,@jiage,@shengyu,@beizhu)
46         return 1
47     end
48     if @count=1 and @shengyu>0--仓库里已有的,进货,需要改变数量
49     begin
50         declare @ckshengyu int
51         select @ckshengyu=shengyu from kucun where code=@code
52         update kucun set shengyu=@shengyu+@ckshengyu where code=@code
53         return 2
54     end
55     if @shengyu<0 and @count=1--出货并且仓库里有这个货
56     begin
57         declare @kshengyu int
58         select @kshengyu=shengyu from kucun where code=@code
59         if @kshengyu>=(-@shengyu)
60         begin
61             update kucun set shengyu=@kshengyu+@shengyu where code=@code
62             return 0
63         end
64         else
65         begin
66             print '不够出货'
67             return -1
68         end
69     end
70     if @shengyu<0 and @count=0
71     begin
72         print '没有这个货物'
73         return -2
74     end
75 end
76 declare @i int
77 --exec @i=jinchuproc 3,'保温杯','',15,10,'中宏 300ML保温杯'
78 --exec @i=jinchuproc 3,'保温杯','',15,-10,'中宏 300ML保温杯'
79 --exec @i=jinchuproc 12,'文件夹','',5,10,'得力牌文件夹'
80 --exec @i=jinchuproc 13,'鼻烟壶','',30,-11,'华光陶瓷骨质瓷'
81 exec @i=jinchuproc 14,'鼻烟壶','',30,-11,'华光陶瓷骨质瓷'
82 print @i
83 select *from kucun
原文地址:https://www.cnblogs.com/9999w/p/4089648.html