mysql存储过程
来源:互联网 发布:nba球员excel数据统计 编辑:程序博客网 时间:2024/06/05 20:54
--删除存储过程
drop procedure 存储过程名;
drop procedure if exists 存储过程名;
--给指定员工涨100元的工资
delimiter //
drop procedure if exists testa//
create procedure testa(in no int)
begin
declare rbalance decimal(16,2);
select balance into rbalance from acct where acct_no=no;
update acct set balance=balance+100 where acct_no=no;
select acct_name'姓名',rbalance'涨前',balance'涨后' from acct where acct_no=no;
end//
--流程控制语句
delimitr //
drop procedure if exists test3;
Create Procedure test3(in s1 int,out s2 int,inout s3 varchar(10))
begin
declare x1 varchar(10) default 'this is x1';
declare x2 varchar(10) default 'this is x2';
set s1=11;
set s2 =22;
set s3 = 'iss3';
if s1=11 and s2=12 then select s1 ,s2;
end if;
if s3='s3' or s1=s2 then select s3;
else select s1,s2,s3;
end if;
case s3
when 's1' then select 'this is s1';
when 's2' then select 'this is s2';
else select 'this is s3';
end case;
while s1>1 do set s1=s1-1;
end while;
select s1;
later:loop set s1=s1-1;
if s1=5 then leave later;
end if;
end loop;
select s1;
end//
MySQL存储过程中有IN,OUT,INOUT类型
-----------------------------------
## IN IN参数只用来向过程传递信息,为默认值。
## MySQL存储过程"in"参数:跟C语言的函数参数的值传递类似,MySQL存储过程内部可能会修改此参数,
## 但in类型参数的修改对调用者(caller)来说是不可见的(not visible)
mysql>use test;
mysql> drop procedure if exists pr_param_in;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> delimiter //
mysql> create procedure pr_param_in(in id int)
-> begin
-> if (id is not null) then
-> set id=id+1;
-> end if;
-> select id as id_inner;
-> end;
-> //
Query OK, 0 rows affected (0.03 sec)
mysql> delimiter ;
mysql> set @id=10;
Query OK, 0 rows affected (0.00 sec)
mysql> call pr_param_in(@id);
+----------+
| id_inner |
+----------+
| 11 |
+----------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> select @id as id_out;
+--------+
| id_out |
+--------+
| 10 |
+--------+
1 row in set (0.00 sec)
## 可以看到用户变量@id传入值为10,执行存储过程后,在过程内部值为:11(id_inner),
## 但外部变量值依旧为:10(id_out)
[sql] view plain copy
在CODE上查看代码片派生到我的代码片
<pre name="code" class="sql">==================================================================================
## OUT OUT参数只用来从过程传回信息。
## MySQL存储过程"out"参数:从存储过程内部传值给调用者。
## 在存储过程内部,该参数初始值为 null,无论调用者是否给存储过程参数设置值。
mysql> drop procedure if exists pr_param_out;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> delimiter //
mysql> create procedure pr_param_out(out id int)
-> begin
-> select id as id_inner_1;
-> if (id is not null) then
-> set id=id+1;
-> select id as id_inner_2;
-> else
-> select 1 into id;
-> end if;
-> select id as id_inner_3;
-> end;
-> //
Query OK, 0 rows affected (0.01 sec)
mysql> delimiter ;
mysql> set @id=10;
Query OK, 0 rows affected (0.00 sec)
mysql> call pr_param_out(@id);
+------------+
| id_inner_1 |
+------------+
| NULL |
+------------+
1 row in set (0.01 sec)
+------------+
| id_inner_3 |
+------------+
| 1 |
+------------+
1 row in set (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
mysql> select @id as id_out;
+--------+
| id_out |
+--------+
| 1 |
+--------+
1 row in set (0.00 sec)
## 可以看出,虽然我们设置了用户定义变量@id为10,传递@id给存储过程后,在存储过程内部,
## id的初始值总是 null(id_inner_1)。最后id值(id_out=1)传回给调用者。
===================================================================================
## INOUT INOUT参数可以向过程传递信息,如果值改变,则可再从过程外调用。
## MySQL存储过程"inout"参数跟out类似,都可以从存储过程内部传值给调用者。
## 不同的是:调用者还可以通过inout参数传递至给存储过程。
mysql> drop procedure if exists pr_param_inout;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> delimiter //
mysql> create procedure pr_param_inout(inout id int)
-> begin
-> select id as id_inner_1;
-> if (id is not null) then
-> set id=id+1;
-> select id as id_inner_2;
-> else
-> select 1 into id;
-> end if;
-> select id as id_inner_3;
-> end;
-> //
Query OK, 0 rows affected (0.01 sec)
mysql> delimiter ;
mysql> set @id=10;
Query OK, 0 rows affected (0.00 sec)
mysql> call pr_param_inout(@id);
+------------+
| id_inner_1 |
+------------+
| 10 |
+------------+
1 row in set (0.00 sec)
+------------+
| id_inner_2 |
+------------+
| 11 |
+------------+
1 row in set (0.00 sec)
+------------+
| id_inner_3 |
+------------+
| 11 |
+------------+
1 row in set (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
mysql> select @id as id_out;
+--------+
| id_out |
+--------+
| 11 |
+--------+
1 row in set (0.00 sec)
## 从结果可以看出:我们把 @id(10)传给存储过程后,存储过程最后又把计算结果值11(id_inner_3)
## 传回给调用者。MySQL存储过程inout参数的行为跟C语言函数中的引用传值类似。
=========================================================================================
通过以上例子:
1) 如果仅仅想把数据传给MySQL存储过程,那就用in类型参数;
2) 如果仅仅从MySQL存储过程返回值,那就用out类型参数;
3) 如果需要把数据传给MySQL存储过程经过计算再传回给我们,那就用inout类型参数。
/*
该代码是创建了一个名叫"p4"的存储过程并设置了s1,s2,s3两个int型一个varchar型参数,还可以是其他数据类型,内部创建了x1,x2两个变量
DELIMITER是修改分隔符的
DELIMITER $$的意思是把默认分隔符";"换成"$$",这样分段写的存储过程就能整个被执行,而不是被当成多条sql语句单独执行
创建完过程再将分隔符改回成";"
两种创建变量并赋值的方式
SET @变量名 = 值;
SELECT 值 INTO @变量名;
使用变量前必须先运行该变量赋值语句
过程内部还可以通过 "DECLARE 变量名 类型(字符串型要加范围) DEFAULT 值;" 来创建变量,但如此创建的变量只能在该过程内部访问
存储过程只有三种类型参数 IN,OUT,INOUT
调用过程时过程有几个参数传几个参数,只是IN型的参数可以传的是变量,可以是常量,而OUT和INOUT型的参数传的必须是变量
传给out,inout参数的变量值会随着在过程中改变在外部也改变,而给in参数的变量外部值则不受过程内变量值改变的影响
*/
DELIMITER $$
DROP PROCEDURE IF EXISTS `p4`$$
CREATE PROCEDURE `p4`(IN s1 INT,OUT s2 INT,INOUT s3 VARCHAR(10))
BEGIN
DECLARE x1 VARCHAR(10) DEFAULT 'this is x1';
DECLARE x2 VARCHAR(10) DEFAULT 'this is x2';
SET s1 = 11;
SET s2 = 22;
SET s3 = 'iss3';
/*
if语法
*/
IF s1 = 11 AND s2 = 12 THEN
SELECT s1,s2;
END IF;
IF s3 = 's3' OR s1 = s2 THEN
SELECT s3;
ELSE
SELECT s1,s2,s3;
END IF;
/*
case语法
*/
CASE s3
WHEN 's1' THEN
SELECT 'this is s1';
WHEN 's2' THEN
SELECT 'this is s2';
ELSE
SELECT 'this is s3';
END CASE;
/*
while循环
*/
WHILE s1>1 DO
SET s1=s1-1;
END WHILE;
SELECT s1;
/*
repeat循环语句
与while不同的是while满足条件就执行,repeat始终执行直到满足条件终止
*/
REPEAT
SET s1 = s1-1;
UNTIL s1=1 END REPEAT;
SELECT s1;
/*
LOOP循环
LOOP没有循环条件,会不停的循环直到遇到 "LEAVE later;" "later"是自定义的LOOP标记
*/
later:LOOP
SET s1 = s1+1;
IF s1 = 5 THEN
LEAVE later;
END IF;
END LOOP;
SELECT s1;
END$$
DELIMITER ;
SET @p_in=1;
SET @p_out=2;
SET @p_inout = 's3';
SELECT 'Hello World1' INTO @p_4;
/*调用存储过程*/
CALL p4(@p_in,@p_out,@p_inout);
SELECT @p_in,@p_out,@p_inout,@p_4;
/*
存储过程如果只有一个语句则语句不用 begin...end包围
存储过程中可以直接使用外部定义的变量
存储过程中用set和select定义的变量就是全局的,执行该过程后内部用set和select定义的变量外部可以访问,其他过程也可以直接使用
*/
CREATE PROCEDURE p1() SET @var='p1';
CREATE PROCEDURE p2() SELECT CONCAT('Last procedure was ',@var);
CALL p1();
CALL p2();
SELECT @var;
CREATE PROCEDURE p3() SELECT CONCAT(@p3_var,' World');
SET @p3_var='Hello';
CALL p3();
/*删除存储过程*/
DROP PROCEDURE p4;
/*或者*/
DROP PROCEDURE IF EXISTS `p4`
/*查看该数据库下有哪些存储过程 test为数据库名*/
SELECT NAME FROM mysql.proc WHERE db='test';
/*或者*/
SHOW PROCEDURE STATUS WHERE db='test';
/*查看存储过程详细信息,包括创建语句*/
SHOW CREATE PROCEDURE p4;
/*存储函数语法*/
delimiter //
create function 函数名(变量 变量类型) returns 返回的数据类型
begin
if(条件) then
return 返回的数据类型 ;
else return 返回的数据类型 ;
end if;
end//
drop procedure 存储过程名;
drop procedure if exists 存储过程名;
--给指定员工涨100元的工资
delimiter //
drop procedure if exists testa//
create procedure testa(in no int)
begin
declare rbalance decimal(16,2);
select balance into rbalance from acct where acct_no=no;
update acct set balance=balance+100 where acct_no=no;
select acct_name'姓名',rbalance'涨前',balance'涨后' from acct where acct_no=no;
end//
--流程控制语句
delimitr //
drop procedure if exists test3;
Create Procedure test3(in s1 int,out s2 int,inout s3 varchar(10))
begin
declare x1 varchar(10) default 'this is x1';
declare x2 varchar(10) default 'this is x2';
set s1=11;
set s2 =22;
set s3 = 'iss3';
if s1=11 and s2=12 then select s1 ,s2;
end if;
if s3='s3' or s1=s2 then select s3;
else select s1,s2,s3;
end if;
case s3
when 's1' then select 'this is s1';
when 's2' then select 'this is s2';
else select 'this is s3';
end case;
while s1>1 do set s1=s1-1;
end while;
select s1;
later:loop set s1=s1-1;
if s1=5 then leave later;
end if;
end loop;
select s1;
end//
MySQL存储过程中有IN,OUT,INOUT类型
-----------------------------------
## IN IN参数只用来向过程传递信息,为默认值。
## MySQL存储过程"in"参数:跟C语言的函数参数的值传递类似,MySQL存储过程内部可能会修改此参数,
## 但in类型参数的修改对调用者(caller)来说是不可见的(not visible)
mysql>use test;
mysql> drop procedure if exists pr_param_in;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> delimiter //
mysql> create procedure pr_param_in(in id int)
-> begin
-> if (id is not null) then
-> set id=id+1;
-> end if;
-> select id as id_inner;
-> end;
-> //
Query OK, 0 rows affected (0.03 sec)
mysql> delimiter ;
mysql> set @id=10;
Query OK, 0 rows affected (0.00 sec)
mysql> call pr_param_in(@id);
+----------+
| id_inner |
+----------+
| 11 |
+----------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> select @id as id_out;
+--------+
| id_out |
+--------+
| 10 |
+--------+
1 row in set (0.00 sec)
## 可以看到用户变量@id传入值为10,执行存储过程后,在过程内部值为:11(id_inner),
## 但外部变量值依旧为:10(id_out)
[sql] view plain copy
在CODE上查看代码片派生到我的代码片
<pre name="code" class="sql">==================================================================================
## OUT OUT参数只用来从过程传回信息。
## MySQL存储过程"out"参数:从存储过程内部传值给调用者。
## 在存储过程内部,该参数初始值为 null,无论调用者是否给存储过程参数设置值。
mysql> drop procedure if exists pr_param_out;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> delimiter //
mysql> create procedure pr_param_out(out id int)
-> begin
-> select id as id_inner_1;
-> if (id is not null) then
-> set id=id+1;
-> select id as id_inner_2;
-> else
-> select 1 into id;
-> end if;
-> select id as id_inner_3;
-> end;
-> //
Query OK, 0 rows affected (0.01 sec)
mysql> delimiter ;
mysql> set @id=10;
Query OK, 0 rows affected (0.00 sec)
mysql> call pr_param_out(@id);
+------------+
| id_inner_1 |
+------------+
| NULL |
+------------+
1 row in set (0.01 sec)
+------------+
| id_inner_3 |
+------------+
| 1 |
+------------+
1 row in set (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
mysql> select @id as id_out;
+--------+
| id_out |
+--------+
| 1 |
+--------+
1 row in set (0.00 sec)
## 可以看出,虽然我们设置了用户定义变量@id为10,传递@id给存储过程后,在存储过程内部,
## id的初始值总是 null(id_inner_1)。最后id值(id_out=1)传回给调用者。
===================================================================================
## INOUT INOUT参数可以向过程传递信息,如果值改变,则可再从过程外调用。
## MySQL存储过程"inout"参数跟out类似,都可以从存储过程内部传值给调用者。
## 不同的是:调用者还可以通过inout参数传递至给存储过程。
mysql> drop procedure if exists pr_param_inout;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> delimiter //
mysql> create procedure pr_param_inout(inout id int)
-> begin
-> select id as id_inner_1;
-> if (id is not null) then
-> set id=id+1;
-> select id as id_inner_2;
-> else
-> select 1 into id;
-> end if;
-> select id as id_inner_3;
-> end;
-> //
Query OK, 0 rows affected (0.01 sec)
mysql> delimiter ;
mysql> set @id=10;
Query OK, 0 rows affected (0.00 sec)
mysql> call pr_param_inout(@id);
+------------+
| id_inner_1 |
+------------+
| 10 |
+------------+
1 row in set (0.00 sec)
+------------+
| id_inner_2 |
+------------+
| 11 |
+------------+
1 row in set (0.00 sec)
+------------+
| id_inner_3 |
+------------+
| 11 |
+------------+
1 row in set (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
mysql> select @id as id_out;
+--------+
| id_out |
+--------+
| 11 |
+--------+
1 row in set (0.00 sec)
## 从结果可以看出:我们把 @id(10)传给存储过程后,存储过程最后又把计算结果值11(id_inner_3)
## 传回给调用者。MySQL存储过程inout参数的行为跟C语言函数中的引用传值类似。
=========================================================================================
通过以上例子:
1) 如果仅仅想把数据传给MySQL存储过程,那就用in类型参数;
2) 如果仅仅从MySQL存储过程返回值,那就用out类型参数;
3) 如果需要把数据传给MySQL存储过程经过计算再传回给我们,那就用inout类型参数。
/*
该代码是创建了一个名叫"p4"的存储过程并设置了s1,s2,s3两个int型一个varchar型参数,还可以是其他数据类型,内部创建了x1,x2两个变量
DELIMITER是修改分隔符的
DELIMITER $$的意思是把默认分隔符";"换成"$$",这样分段写的存储过程就能整个被执行,而不是被当成多条sql语句单独执行
创建完过程再将分隔符改回成";"
两种创建变量并赋值的方式
SET @变量名 = 值;
SELECT 值 INTO @变量名;
使用变量前必须先运行该变量赋值语句
过程内部还可以通过 "DECLARE 变量名 类型(字符串型要加范围) DEFAULT 值;" 来创建变量,但如此创建的变量只能在该过程内部访问
存储过程只有三种类型参数 IN,OUT,INOUT
调用过程时过程有几个参数传几个参数,只是IN型的参数可以传的是变量,可以是常量,而OUT和INOUT型的参数传的必须是变量
传给out,inout参数的变量值会随着在过程中改变在外部也改变,而给in参数的变量外部值则不受过程内变量值改变的影响
*/
DELIMITER $$
DROP PROCEDURE IF EXISTS `p4`$$
CREATE PROCEDURE `p4`(IN s1 INT,OUT s2 INT,INOUT s3 VARCHAR(10))
BEGIN
DECLARE x1 VARCHAR(10) DEFAULT 'this is x1';
DECLARE x2 VARCHAR(10) DEFAULT 'this is x2';
SET s1 = 11;
SET s2 = 22;
SET s3 = 'iss3';
/*
if语法
*/
IF s1 = 11 AND s2 = 12 THEN
SELECT s1,s2;
END IF;
IF s3 = 's3' OR s1 = s2 THEN
SELECT s3;
ELSE
SELECT s1,s2,s3;
END IF;
/*
case语法
*/
CASE s3
WHEN 's1' THEN
SELECT 'this is s1';
WHEN 's2' THEN
SELECT 'this is s2';
ELSE
SELECT 'this is s3';
END CASE;
/*
while循环
*/
WHILE s1>1 DO
SET s1=s1-1;
END WHILE;
SELECT s1;
/*
repeat循环语句
与while不同的是while满足条件就执行,repeat始终执行直到满足条件终止
*/
REPEAT
SET s1 = s1-1;
UNTIL s1=1 END REPEAT;
SELECT s1;
/*
LOOP循环
LOOP没有循环条件,会不停的循环直到遇到 "LEAVE later;" "later"是自定义的LOOP标记
*/
later:LOOP
SET s1 = s1+1;
IF s1 = 5 THEN
LEAVE later;
END IF;
END LOOP;
SELECT s1;
END$$
DELIMITER ;
SET @p_in=1;
SET @p_out=2;
SET @p_inout = 's3';
SELECT 'Hello World1' INTO @p_4;
/*调用存储过程*/
CALL p4(@p_in,@p_out,@p_inout);
SELECT @p_in,@p_out,@p_inout,@p_4;
/*
存储过程如果只有一个语句则语句不用 begin...end包围
存储过程中可以直接使用外部定义的变量
存储过程中用set和select定义的变量就是全局的,执行该过程后内部用set和select定义的变量外部可以访问,其他过程也可以直接使用
*/
CREATE PROCEDURE p1() SET @var='p1';
CREATE PROCEDURE p2() SELECT CONCAT('Last procedure was ',@var);
CALL p1();
CALL p2();
SELECT @var;
CREATE PROCEDURE p3() SELECT CONCAT(@p3_var,' World');
SET @p3_var='Hello';
CALL p3();
/*删除存储过程*/
DROP PROCEDURE p4;
/*或者*/
DROP PROCEDURE IF EXISTS `p4`
/*查看该数据库下有哪些存储过程 test为数据库名*/
SELECT NAME FROM mysql.proc WHERE db='test';
/*或者*/
SHOW PROCEDURE STATUS WHERE db='test';
/*查看存储过程详细信息,包括创建语句*/
SHOW CREATE PROCEDURE p4;
/*存储函数语法*/
delimiter //
create function 函数名(变量 变量类型) returns 返回的数据类型
begin
if(条件) then
return 返回的数据类型 ;
else return 返回的数据类型 ;
end if;
end//
0 0
- 【mysql】mysql存储过程
- MySQL存储过程详解 mysql 存储过程
- MySQL存储过程详解 mysql 存储过程
- MySQL存储过程详解 mysql 存储过程
- MySQL存储过程详解 mysql 存储过程
- MySQL存储过程详解 mysql 存储过程
- MySQL存储过程详解 mysql 存储过程
- MySQL存储过程详解 mysql 存储过程
- MySQL存储过程详解 mysql 存储过程
- MySQL存储过程详解 mysql 存储过程
- MySQL存储过程详解 mysql 存储过程
- MySQL存储过程详解 mysql 存储过程
- MySQL存储过程详解 mysql 存储过程
- MySQL存储过程详解 mysql 存储过程
- MySQL存储过程详解 mysql 存储过程
- MySQL存储过程详解 mysql 存储过程
- MySQL存储过程详解 mysql 存储过程
- MySQL存储过程详解 mysql 存储过程
- 图片加载框架Glide使用教程
- 前端开发中常遇到的浏览器兼容问题小结
- Python闯关2
- Unity3D游戏引擎最详尽基础教程
- BASE64编码简介
- mysql存储过程
- Matlab编程与数据类型 -- 函数M文件的调用
- stl queue 用法
- Untiy 文件放置路径
- mybatis源码解析(三)SqlSession
- Android Handler分析
- java同步Synchronized的使用
- hi javascript
- AngularJs入门之表单开发