zoukankan      html  css  js  c++  java
  • SQL之存储过程

    --定义变量
    declare @a int 
    --变量赋值
    set @a =1
    print @a

    --变量结合查询语句
    --不跟菠萝一个产地的水果信息
    select *from fruit where source not in (
    select source from fruit where name ='菠萝'
    )

    declare @source varchar(50)
    select @source =source from fruit where name ='菠萝'
    select *from fruit where source != @source

    --在查询语句中也可以对一个变量赋值 放在select和from中间
    --set 单纯用来给变量赋值 不具备查询映射的功能
    --当from后面显示多个值又给他赋值之后他会把最后一条去掉

    update fruit set numbers =98 where ids='k001'
    update fruit set numbers =88 where ids='k002'
    update fruit set numbers =78 where ids='k003'
    update fruit set numbers =99 where ids='k004'
    update fruit set numbers =55 where ids='k005'


    --查询菠萝的数量和橘子的数量 比较菠萝和橘子的数量多少

    declare @boluo int,@juzi int
    select @boluo =numbers from fruit where ids ='k002'
    select @juzi =numbers from fruit where ids ='k003'
    if @boluo>@juzi
    begin
    print'菠萝数量多'
    end
    else
    begin 
    print '橘子数量多'
    end


    --累加求和

    declare @a int 
    set @a =10
    declare @sum int,@i int
    set @sum =0
    set @i =1

    while @i<=@a
    begin
    set @sum =@sum+@i
    set @i =@i+1
    end
    print @sum

    --存储过程 相当于c#里面的函数
    --as 和存储过程名之间写输入参数输出参数,不需要指明返回值


    create proc leijia
    @a int

    as
    declare @sum int,@i int
    set @sum =0
    set @i =1

    while @i<=@a
    begin
    set @sum =@sum+@i
    set @i =@i+1
    end
    return @sum

    go
    --exec 调用 需要定义一个返回值接收
    declare @sum int --定义一个返回值接收

    exec @sum =leijia 10--调用 返回值写在exec后面 存储过程名 跟参数 多个参数用逗号隔开
    print @sum

    存储过程输出参数

    alter proc leijia 修改存储过程
    @a int,
    @b int out
    as
    declare @sum int,@i int
    set @sum =0
    set @i =1

    while @i<=@a
    begin
    set @sum =@sum+@i
    set @i =@i+1
    end
    set @b=5
    return @sum

    go
    --exec 调用 需要定义一个返回值接收
    declare @sum int ,@b int--定义一个返回值接收

    exec @sum =leijia 100,@b out--调用 返回值写在exec后面 存储过程名 跟参数 多个参数用逗号隔开
    print @sum
    print @b

  • 相关阅读:
    使用 RMAN 同步数据库
    关于MongoDB分布式高可用集群实现
    Oracle排错总结
    Oracle中Restore和Recovery的区别
    linux下防火墙iptables原理及使用
    RHEL6.5上升级OpenSSH7.4p1
    awk中next以及getline用法示例
    Linux下Redis的安装和部署
    Mongodb集群搭建的三种方式
    天地图服务地址
  • 原文地址:https://www.cnblogs.com/wangfangjia/p/4941271.html
Copyright © 2011-2022 走看看