存储过程练习 超市管理系统

思维导图:

create database  chaoshilc--创建一个数据库
go
use chaoshilc--使用这个数据库
go

create table gongying--创建一个供应商的表格
(
  gcode int primary key identity(1001,1),--供应商编号,主键
  gname varchar(20),--名称
  gsdh varchar(20),--电话
  gsdz varchar(20)--地址
)

--向供应商表格中添加数据
insert into gongying values('可口可乐','232323','山东青岛')
insert into gongying values('花生油','323232','山东济南')
insert into gongying values('绿茶','565656','山东日照')
insert into gongying values('矿泉水','656565','山东潍坊')
insert into gongying values('红星二锅头','878787','北京')

create table cangku--创建一个仓库的表格
(
  ccode int primary key identity(100001,1),--产品编号,主键
  cname varchar(20),--产品名称
  cshu int,--产品数量
  cjin decimal(18,2),--进价
  cgcode int--供应商编号,供应商表格中gcode的外键
)

--向仓库表格中添加数据
insert into cangku values('可口可乐',1600,2,1001)
insert into cangku values('花生油',800,40,1002)
insert into cangku values('绿茶',1300,8,1003)
insert into cangku values('矿泉水',1200,1,1004)
insert into cangku values('红星二锅头',1500,2,1005)


create table chaoshi--创建一个超市的表格
(
  cccode int,--产品编号,仓库表格中ccode的外键
  ccname varchar(20),--产品名称
  ccshu int,--产品数量
  cshou decimal(18,2)--售价
)

--向超市表格中添加数据
insert into chaoshi values(100001,'可口可乐',50,3)
insert into chaoshi values(100002,'花生油',30,80)
insert into chaoshi values(100003,'绿茶',100,20)
insert into chaoshi values(100004,'矿泉水',60,2)
insert into chaoshi values(100005,'红星二锅头',70,5)

select*from gongying
select*from cangku
select*from chaoshi


--1.跟苹果公司apple建立合作关系,编号为1006,电话是888888,地址是美国
insert into gongying values('苹果公司','888888','美国')--直接添加一条苹果公司的数据到供应商表中

--2.新进iphone6s和ipd5各10部,进价3000,3500,拿到超市各2部,售价为4500,5000

--因为手机和平板分别10部,超市两部,则仓库中留下8部,分别把两件商品添加到仓库8部,超市2部
insert into cangku values('iphone6s',8,3000,1006)
insert into cangku values('ipad',8,3500,1006)
insert into chaoshi values(100006,'iphone6s',2,4500)
insert into chaoshi values(100007,'ipd',2,5000)
--3.卖掉可口可乐10瓶,(需要判断超市货够不够,若不够加上仓库的货看看够不够,若够就卖掉,拿两瓶放超市,
--若不够有多少卖多少,通知可口可乐公司需要进货)
create proc jilu--创建一个名为jilu的存储过程,下面返回一个参数为卖掉的数量
@mai int
as
begin
    declare @ping int,@shu int--定义两个变量用来接收超市和仓库现有的100001商品的数量
    select @ping=ccshu from chaoshi where cccode=100001--查询超市现有的这件商品的数量用@ping接收
    select  @shu=cshu from cangku where ccode=100001--查询仓库现有这件商品的数量用@shu接收

    if @ping>@mai--如果超市现有的商品数量大于要卖掉的数量
    begin
        update chaoshi set ccshu=@ping-@mai where cccode=100001--则update更新的意思,超市卖掉@mai后,设置超市现在商品数量为@ping-@mai
    end

    else if  @ping<@mai--如果超市现有商品数量小于卖掉数量,则去仓库看一下
    begin
        if @ping+@shu>@mai--如果超市加仓库的数量大于卖掉数量,则看一下卖掉后仓库的剩余
        begin
            if (@ping+@shu)-@mai>=2--如果仓库剩余大于等于2
            begin
                update chaoshi set ccshu=2 where cccode=100001--则拿两瓶去超市,update更新超市现有数量为2
                update cangku set cshu=@shu+@ping-@mai-2 where ccode=100001--因为仓库剩余的拿走两瓶去超市,则更新仓库现在的数量为卖掉后剩余的减去2
            end
            else if(@ping+@shu)-@mai<2 and (@ping+@shu)-@mai>0--如果仓库剩余小于2大于0,则全部拿去超市
            begin
                update chaoshi set ccshu=@shu+@ping-@mai where cccode=100001--更新现在超市的这件商品的数量为超市和仓库原有的总量减去卖掉的数量
                update cangku set cshu=0 where ccode=100001--仓库现在没有了这件商品,则数量为0
            end
        end
        else if @ping+@shu<@mai--如果超市加仓库的数量小于要卖的数量,则有多少卖多少,卖掉后仓库与超市这件商品的数量为0
        begin
            update chaoshi set ccshu=0 where cccode=100001--更新超市表中这件商品数量为0
            update cangku set cshu=0 where ccode=100001--更新仓库表中这件商品数量为0
            print'需要进货'--提醒一下需要进货
        end
    end 
end
go--选中执行一下这个存储过程使其生效,只能执行一次

exec jilu 10--执行jilu这个存储过程返回一个参数10 ,在jilu里@mai这个变量中,代表要卖掉的数量

 
--4.跟1001公司关系破裂,所有有关1001公司的产品需要全部退货
delete from chaoshi where cccode=100001--把超市表中1001这个公司的商品数据删除,表中这个公司商品编号为100001
delete from cangku where ccode=100001--把仓库表中的数据也删除
delete from gongying where gcode=1001--把公司表中这个公司信息删除

--5.进货:100003号商品新进30
create proc shangbu--创建一个shangpu的存储过程
@s int--变量@s接收下面调用时给的参数值,新进商品的的数量
as
begin
declare @p int--声明一个变量@p
select @p=cshu from cangku where ccode=100003 --查询仓库中100003号商品现有的数量赋值到变量@s中
set @p=@s+@p --设置新进商品数量@s加上原有商品数量@p重新给@p赋值,现在@p里是进货后产品数量
update cangku set cshu=@p where ccode=100003--更新一下现在仓库中100002号商品数量,把@p中的值赋值到仓库数量中
end
go
exec shangbu 30--执行shangpu这个存储过程,参数为30,新进商品的的数量,接收于变量@s中
select cshu from cangku where ccode=100003--查询现在仓库中100003这件商品的数量
原文地址:https://www.cnblogs.com/zyg316/p/5585910.html