zoukankan      html  css  js  c++  java
  • sql 存储过程、事务、视图、触发器

    1存储过程
    1.1存储的格式
    create proc name
    as
    begin
    语句
    end
    1.2执行存储
    exec name
    1.3无参数
    --创建存储过程
    if (exists (select * from sys.objects where name = 'sstudent'))
    drop proc sstudent
    go
    creates student
    as
    select * from student;

    --调用、执行存储过程
    exec student;
    1.4带参数
    --带参存储过程
    create proc  stu

    @startId int,

    @endId int
    as
    select * from student where id between @startId and @endId
    go

    exec stu 2, 4;
    1.5 输出参数
    create proc get
    @id int, --默认输入参数
    @name varchar(20) out, --输出参数
    @age varchar(20) output--输入输出参数
    as
    select @name = name, @age = age from student where id = @id and sex = @age;
    go

    --
    declare @id int,
    @name varchar(20),
    @temp varchar(20);
    set @id = 7;
    set @temp = 1;
    exec get @id, @name out, @temp output;
    select @name, @temp;
    print @name + '#' + @temp;


    2视图
    2.1创建视图
    (1)使用视图向导

    (2)使用语句
    create view cang--创建视图的语法
    as --as和go中间放上我所需要的一个查询语句
    查询语句
    go
    2.2查看信息
    select *from view_name

    3事务
    3.1开始事务
    begin tran name
    3.2提交事务
    commit tran
    3.3回滚事务
    rollback tran


    4触发器
    4.1 delect
    create trigger 触发的表名,delect
    on 触发的表名
    instead of delect
    as
    语句
    go
    instead of 替换
    for 执行完成delect再触发
    after执行完delect再触发
    4.2 update
    同上,将delect换成update
    4.3 insert
    同上,将delect换成update

    练习
    use lianxi
    go
    create table jin
    (jcode int primary key,--序号
    jname varchar(20), --商品名称
    )
    create table chu
    (
    ccode int primary key,--序号
    cname varchar(20), --商品名称
    cshu decimal(18,2), --出货数量
    cjia decimal(18,2), --单价
    cj int,
    )

    create table piao
    (
    pcode int primary key identity(101,1),--序号
    pcname varchar(20), --商品名称
    pshu decimal(18,2), --数量
    pjia decimal(18,2) , --单价
    pzong decimal(18,2),
    )
    go

    insert into jin values(101,'摩托罗拉公司')
    insert into jin values(102,'苹果公司')
    insert into jin values(103,'诺基亚公司')
    insert into jin values(104,'索尼公司')

    insert into chu values(1001,'iphone 3gs',10,600,102)
    insert into chu values(1002,'iphone 4',15,998,102)
    insert into chu values(1003,'iphone 5',20,2800,102)
    insert into chu values(1004,'iphone 5s',22,3300,102)
    insert into chu values(1005,'VB',10,300,101)
    insert into chu values(1006,'Mileston',7,600,101)
    insert into chu values(1007,'N95',8,350,103)
    insert into chu values(1008,'5320XM',10,450,103)
    insert into chu values(1009,'L36H',14,180,104)
    insert into chu values(1010,'L39H',10,2780,102)
    go
    select *from jin
    delete from chu
    select *from chu
    --在没有此货物并且为进货时,添加上这一行信息;
    --在没有此货物并且为出货时,打印没有此货物!

    if (exists (select * from sys.objects where name = 'cunchu'))
    drop proc cunchu
    go
    create proc cunchu
    @ccode int,
    @cname varchar(20),
    @cshu decimal(18,2),
    @cjia decimal(18,2),
    @cj int
    as
    begin
    declare @shu decimal(18,2)
    declare @count int
    select @count=COUNT(*) from chu where ccode=@ccode
    select @shu=cshu from chu where ccode=@ccode
    if @COUNT>0
    begin
    if @cshu >0
    update chu set cshu=cshu+@cshu where ccode=@ccode
    if @cshu <0
    if @cshu+ @shu >0
    begin
    update chu set cshu=cshu+@cshu where ccode=@ccode
    declare @zong decimal(18,2)
    declare @xcshu decimal(18,2)
    set @xcshu=-@cshu
    set @zong=@cjia*@cshu
    insert into piao values(@cname,@xcshu,@cjia,@zong)
    end
    if @cshu+ @shu <=0
    print '请及时补充货物'
    end
    else
    begin
    if @cshu>0
    insert into chu values(@ccode,@cname,@cshu,@cjia,@cj)
    if @cshu<0
    print '没有此货物'
    end
    end
    go

    select *from chu
    exec cunchu @ccode=1001,@cname='iphone 3gs',@cshu=10,@cjia=600,@cj=102
    exec cunchu @ccode=1001,@cname='iphone 3gs',@cshu=-5,@cjia=600,@cj=102
    select *from piao
    exec cunchu @ccode=1021,@cname='iphone 3gs',@cshu=10,@cjia=600,@cj=102
    exec cunchu @ccode=1022,@cname='iphone 3gs',@cshu=-10,@cjia=600,@cj=102

  • 相关阅读:
    无标题
    UVA 11987 并查集删点
    屯题 (bestcoder #62~#75)
    codeforces 293E Close Vertices 点分治+滑窗+treap
    hdu4670 Cube number on a tree 点分治
    hdu4812 D Tree 点分治
    poj2112 Boatherds 点分治
    HDU 4866 Shooting 二分+主席树
    poj1741 Tree 点分治
    关于点分治。。。
  • 原文地址:https://www.cnblogs.com/zhang-dandan-1/p/5842655.html
Copyright © 2011-2022 走看看