• SQL 存储过程 超市小票打印


    create database chaoshils--创建一个数据库
    go
    use chaoshils--使用这个数据库
    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 chaoshi--创建一个超市的表格
    (
      ccode int primary key,--产品编号
      cname varchar(20),--产品名称
      cshu int,--产品数量
      cjin decimal(18,2),--进价
      cshou decimal(18,2),--售价
      cgcode int --供应商编号,供应商表的外键
    )
    
    --向超市表格中添加数据
    insert into chaoshi values(10001,'可口可乐',100,2,3,1001)
    insert into chaoshi values(10002,'花生油',50,40,80,1002)
    insert into chaoshi values(10003,'绿茶',100,8,20,1003)
    insert into chaoshi values(10004,'矿泉水',150,1,2,1004)
    insert into chaoshi values(10005,'红星二锅头',120,3,5,1005)
    
    
    --创建一个小票的表格,不能直接插入数据,在下面存储过程中根据商品的销售情况向里面添加数据
    create table xiaopiao
    (
      xcode int,--产品编号,超市表的外键
      xname varchar(20),--产品名称
      xshu int,--购买的产品数量
      xjiage decimal(18,2),--产品单价
      zjia decimal(18,2)  --产品总价
    )
    
    select*from gongying
    select*from chaoshi
    
    create proc liushui --创建一个存储过程用来记录卖出货物的数量价格添加于小票表中
    @mai int,--返回一个参数,购买的数量 
    @hcode int--购买的商品的编号
    as
    begin
    declare--声明一下使用下面定义的变量
    @name varchar(20),
    @shu int,
    @danjia decimal(18,2),
    @zong decimal(18,2),
    @count int
    select @name=cname from chaoshi where ccode=@hcode--通过返回参数的商品编号在超市中查找商品名称赋值于变量@name中
    select @shu=cshu from chaoshi where ccode=@hcode--通过编号查找此商品现有的而数量赋值于变量@shu中
    select @danjia=cshou from chaoshi where ccode=@hcode--通过编号查找此产品的价格赋值于变量@danjia中
    set @zong=@danjia*@mai--货物价格@danjia乘以返回参数中购买的数量@mai等于总价格,用变量@zong接收一下
    
    select @count=count(*)from chaoshi where ccode=@hcode--查看一下返回的参数商品编号是否存在,存在值为1,不存在为0,接收于变量@count中
    
    if @count>0--如果@count不为0,则存在这种商品
    begin
      if @shu> = @mai--如果超市内商品的数量大于或者等于购买的数量
      begin
       insert into xiaopiao values(@hcode,@name,@mai,@danjia,@zong)--则向xiaopiao表中添加购买信息
       select xcode as 编号,xname as 产品名称,xshu as 购买数量,xjiage as 单价,zjia as 合计 from xiaopiao--把添加到表格中的信息查询一下,列名改为汉字显示
      end
      else if @shu<@mai--如果超市内的商品数量小于购买的数量
      begin
      print'货物不足'  --则,货物不足
      end  
    end
    else if @count=0--如果@count值为0,则没有这件商品
    begin
    print'没有此商品'
    end
    
    end
    go--选中执行此存储过程,只能执行一次
    
    exec liushui 50,10008--此处是执行存储过程liushui并返回两个参数,数量和商品编号,可修改数量以及商品参数选择其他商品
                         --此处商品编号的参数在表中并有此编号的商品,则执行@count=0这一步,没有此商品,因此小票表中就不会添加数
    --据
    select*from xiaopiao--查看小票表中存储的数据
  • 相关阅读:
    layui 标签页切换
    m1配置多个git账户
    sqlserver 获取表和字段的注释方法
    springboot+mybatisPlus 配置多数据源--转载
    nginx 配置静态网页和反向代理
    ORA-01000: maximum open cursors exceeded
    重装系统我们选择FAT还是NTFS?U盘和硬盘格式化两者选谁?
    2、条件表达式
    1、javascript 知识拓展
    1_maven 问题
  • 原文地址:https://www.cnblogs.com/zyg316/p/5585936.html
Copyright © 2020-2023  润新知