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