Mysql储存过程2:变量定义与参数传递

#储存过程 中的变量定义
declare 变量名 类型 可选类型 -- 跟建表差不多

create procedure p()
  begin
    declare age int default(18);
    declare number int default 1;
    select age+number;
  end$
/*
mysql> create procedure p()
    ->   begin
    ->     declare age int default(18);
    ->     declare number int default 1;
    ->     select age+number;
    ->   end$
Query OK, 0 rows affected (0.00 sec)

mysql> call p$
+------------+
| age+number |
+------------+
|         19 |
+------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.01 sec)
*/
#储存过程 中, 变量可以运算
#运算结果赋修值给变量用: set age := 运算式
#set age := age + 2
#参数传递

mysql> create procedure p2(number int, age int)
    ->       begin
    ->         select age+number;
    ->       end$
Query OK, 0 rows affected (0.00 sec)

mysql> call p2(1,2)$
+------------+
| age+number |
+------------+
|          3 |
+------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

mysql>
时间: 2024-08-26 13:54:23

Mysql储存过程2:变量定义与参数传递的相关文章

MySQL 储存过程-原理、语法、函数详细说明

Mysql储存过程是一组为了完成特定功能的SQL语句集,经过编译之后存储在数据库中,当需要使用该组SQL语句时用户只需要通过指定储存过程的名字并给定参数就可以调用执行它了,简而言之就是一组已经写好的命令,需要使用的时候拿出来用就可以了.想要快速的了解Mysql储存过程吗,就一同看一下下文的"Mysql储存过程-原理.语法.函数详细说明"吧! 一.Mysql储存过程简介:储存过程是一个可编程的函数,它在数据库中创建并保存.它可以有SQL语句和一些特殊的控制结构组成.当希望在不同的应用程序

Mysql储存过程4:mysql变量设置

默认全局变量是两个@@开头, 可用show variables查看所有默认变量: @@user #declare定义变量只能用在储存过程中 #declare 变量名 数据类型 可选类型 declare num int: declare age int defalut 100; #定义全局变量, 可以用 set声明: #@变量名 #有两种写法, @name := value / @name = value set @age=18; set @age:=18; select @name:=user(

Mysql储存过程1: 设置结束符与储存过程创建

#显示储存过程 show procedure status; #设置结束符 delimiter $; #创建储存过程 create procedure procedure_name() begin --sql语句 end$ create procedure myshow() begin select user(),database(); end$ #调用储存过程 call procedure() call procedure #没有对数时括号可省 mysql> use test$ Databas

Mysql储存过程6: in / out / inout

in 为向函数传送进去的值 out 为函数向外返回的值 intout 传送进去的值, 并且还返回这个值 create procedure q1(in number int,out name varchar(100)) begin if number > 1 then select 'true'; else select 'false'; end if; end$ 调用时: call q1(1, @value); 注意, 第二个参数要为变量定义的型式. 这个函数并没有向外发送改变后的name值,

mysql储存过程

一 介绍 存储过程包含了一系列可执行的sql语句,存储过程存放于MySQL中,通过调用它的名字可以执行其内部的一堆sql.到目前为止,我们上面学的视图.触发器.事务等为我们简化了应用程序级别写sql语句的复杂程度,让我们在应用程序里面写sql更简单方便了,但是我们在应用程序上还是需要自己写sql的,而我们下面要学的存储过程,它是想让我们的应用程序不需要再写sql语句了,所有的sql语句,全部放到mysql里面,被mysql封装成存储过程,说白了它就是一个功能,这个功能对应着一大堆的sql语句,这

mysql 储存过程

创建过程 create procedure demo(in par int)//in out inout 参数名字 参数类型 begin set @a = 123;//会话变量 对当前会话有效,全局的变量 declare a int;//用普通变量要声明类型,只对一个作用域有用 declare a int default 5;//直接声明并赋值 set a = 123;//普通变量 if a = 123 then end if; if a = 12345 then -- else -- end

Mysql储存过程5: while

循环结构 while create procedure name() begin while 条件 do SQL语句 end while; end$ create procedure aa6() begin declare number int default 0; while number < 5 do select number; set number = number + 1; end while; end$ mysql> create procedure aa6()    -> 

Mysql储存过程3:if语句

--if/else语句 if 条件 then SQL语句 else SQL语句elseifSQL语句 end if; create procedure test1( number int ) begin if number > 10 then select user(); else select 'please input a number > 10'; end if; end$ mysql> create procedure test1( -> number int ->

Mysql储存过程8:repeat循环

语法: repeat SQL until 条件 end repeat; 就是相当于其他语言中的: do{ # }while(); mysql> create procedure p1p() -> begin -> declare count int default 0; -> repeat -> select user(); -> set count = count + 1; -> until count>3 end repeat; -> end$ Q