变量,存储过程

  • 变量
  • 存储过程和函数
  • MySQL学习列表

变量

mysql存储过程怎么声明varchar mysql存储过程定义变量_mysql

系统变量

变量由系统提供,不是用户定义,属于服务器层面。

1.使用语法

1.查看所有系统变量
show global / 【session 】variables;#查看全局变量或会话变量
2. 查看满足条件的系统变量
如 show global / 【session】 variables like '%char%';
3. 查看指定的某个系统变量
如 select @@global/ 【session】. 系统变量名
4. 为某个系统变量赋值
set global /【 session】系统变量名 = 值 ;或  set @@global/ 【session】. 系统变量名 = 值;

2.作用域

  • 全局变量:服务器每次启动将为所有的全局变量赋初始值,针对于所有的会话(连接)有效,但是重启后则无效。
  • 会话变量:仅仅对于当前会话(连接)有效。

自定义变量

变量由用户自定义,不是由系统定义。
1.使用

①用户变量
  1) 声明并初始化
         set @用户变量名=值;   或    set @用户变量名 := 值;  或    select @用户变量名 := 值;
  2) 赋值
         set @用户变量名=值;   或    set @用户变量名 := 值;  或    select @用户变量名 := 值;
         或 select 字段 into @变量名 from 表名;
  3) 使用(比如:查看,比较,运算等)
         select @变量名

 ②局部变量
     1) 声明
            declare 局部变量名 类型; 或   declare 局部变量名 类型 default 值;
     2) 赋值
            set 局部变量名=值;   或    set 局部变量名 := 值;  或    select @局部变量名 := 值;
            或  select 字段 into 局部变量名 from 表名;
     3) 使用
            select 局部变量名;

2.作用域

  • 用户变量:对于当前会话(连接)有效,同于会话变量的作用域。
  • 局部变量:仅仅在定义它的begin end中有效,应用在begin end中的第一句话。

用户变量和局部变量对比

作用域

定义和使用的位置

语法

用户变量

当前会话

会话中的任何地方

必须加@符号,不用限定类型

局部变量

begin end中

只能在begin end中,且为第一句话

一般不用加@符号 ,需要限定类型

3.例子

#用户变量
set @m = 1;
select @n := 2;
set @mysum =@m + @n;
select @mysum;  #输出为3

存储过程和函数

提高代码的重用性,简化操作,减少了连接数据库服务器的连接次数和编译次数。

存储过程
一组预先变好的SQL语句的集合,能够完成批处理。
1. 存储过程的创建

create procedure 存储过程名(参数列表)
         begin
               方法体(一组SQL语句)
         end

注意:

①参数列表包含三部分:参数模式 参数名 参数类型 例如,in stuname varchar(20)
参数模式:in —该参数可以作为输入,也就是改参数需要调用方传入值;
out —该参数可以作为输出,即作为返回值;
inout —该参数可以作为输入和输出,该参数既可以需要传入值,又可以返回值;

②如果存储过程体仅仅只有一句话,begin end可以省略。

③存储过程体的每条SQL必须加分号,存储过程体的结尾可以使用 delimiter 重新设置。
语法:
delimiter 结束标记

2.调用

call 存储过程名(实参列表); 3. 删除

drop procedure 存储过程名; 4. 查看

show create procedure 存储过程名;

存储过程代码举例

  1. 空参列表存储过程
drop procedure if exists p1;#如果存在,则先删除
delimiter $ #结束标记
create procedure p1()
begin
  insert into booktype  values(8,'a'),(9,'b'),(10,'c'),(11,'d');
end $

call p1()$  #调用
  1. 创建带in模式参数的存储过程
drop procedure if exists p1;
delimiter $
create procedure p2(in bn varchar(25))
begin
  select b.name from booktype b where id =
  ( 
		select btypeId from book where bname = bn
  );
end $

call p2('平凡的世界')$  #调用
  1. 创建带多个in模式参数的存储过程
drop procedure if exists p1;
delimiter $
create procedure p1(in id int,in n varchar(25))
begin
  declare res int default 0;
  select  count(*) into res
  from booktype where booktype.name =  n 
  and  booktype.id = id;
  select if (res > 0,'成功','失败') as '查看';
end $

call p1(4,'a')$  #调用
  1. 创建带out模式的存储过程
drop procedure if exists p1;
delimiter $
create procedure p1(in id int,out n varchar(25))
begin
     select bname into n #重点
     from book 
     where bid = id;
end $

#调用和接收返回值
call p1(4,@bn)$
select @bn;

# 多个返回值类型
drop procedure if exists p1;
delimiter $
create procedure p1(in bid int,out n varchar(25),out m varchar(25))
begin
     select b.bname,bt.name into n, m
     from book b
     inner join booktype bt
     on b.btypeId=bt.id
     where b.bid =bid;
end $

call p1(4,@bn,@bm)$
select @bn,@bm;
  1. 带inout模式参数的存储:和C++引用有点像
delimiter $
create procedure p2(inout a int ,inout b int)
begin
    set a =a*2;
     set b =b*2;
end$

#先定义变量,再调用
set @m=12;
set @n=5;
call p2(@m,@n);
select @m,@n;