mysql 存储过程

Author Avatar
doubledna 2017 年 02 月 20 日

记录 MYSQL 存储过程中的关键语法:
DELIMITER // <wbr /> 声明语句结束符,用于区分;
CREATE <wbr />PROCEDURE <wbr />demo_in_parameter(IN <wbr />p_in <wbr />int) <wbr /> 声明存储过程
BEGIN <wbr />.... END <wbr /> 存储过程开始和结束符号
SET <wbr />@p_in=1 <wbr /> <wbr /> 变量赋值
<div><wbr /></div>
DECLARE <wbr />l_int <wbr />int <wbr />unsigned <wbr />default <wbr />4000000; <wbr /> 变量定义

什么是 mysql 存储例程?

存储例程是存储在数据库服务器中的一组 sql 语句,通过在查询中调用一个指定的名称来执行这些 sql 语句命令。

为什么要使用 mysql 存储过程?

我们都知道应用程序分为两种,一种是基于 web,一种是基于桌面,他们都和数据库进行交互来完成数据的存取工作。假设现在有一种应用程序包含了这两 种,现在要修改其中的一个查询 sql 语句,那么我们可能要同时修改他们中对应的查询 sql 语句,当我们的应用程序很庞大很复杂的时候问题就出现这,不易维 护!另外把 sql 查询语句放在我们的 web 程序或桌面中很容易遭到 sql 注入的破坏。而存储例程正好可以帮我们解决这些问题。

存储过程 (stored procedure)、存储例程(store routine)、存储函数区别

Mysql 存储例程实际包含了存储过程和存储函数,它们被统称为存储例程。

其中存储过程主要完成在获取记录或插入记录或更新记录或删除记录,即完成 select insert delete update 等的工作。而存储函数只完成查询的工作,可接受输入参数并返回一个结果。

创建 mysql 存储过程、存储函数

create procedure 存储过程名(参数)

存储过程体

create function 存储函数名(参数)

下面是存储过程的例子:
mysql> DELIMITER // <wbr />
mysql> CREATE PROCEDURE proc1(OUT s int) <wbr />
<wbr /> <wbr /> -> BEGIN
<wbr /> <wbr /> -> SELECT COUNT(*) INTO s FROM user; <wbr />
<wbr /> <wbr /> -> END
<wbr /> <wbr /> -> // <wbr />
mysql> DELIMITER ;

注:

(1)这里需要注意的是 DELIMITER// 和 DELIMITER; 两句,DELIMITER 是分割符的意思,因为 MySQL 默认以 ";" 为分隔 符,如果我们没有声明分割符,那么编译器会把存储过程当成 SQL 语句进行处理,则存储过程的编译过程会报错,所以要事先用 DELIMITER 关键字申明当 前段分隔符,这样 MySQL 才会将 ";" 当做存储过程中的代码,不会执行这些代码,用完了之后要把分隔符还原。

(2)存储过程根据需要可能会有输入、输出、输入输出参数,这里有一个输出参数 s,类型是 int 型,如果有多个参数用 "," 分割开。

(3)过程体的开始与结束使用 BEGIN 与 END 进行标识。

这样,我们的一个 MySQL 存储过程就完成了,是不是很容易呢? 看不懂也没关系,接下来,我们详细的讲解。

(2). <wbr /> 声明分割符

 <wbr />

其实,关于声明分割符,上面的注解已经写得很清楚,不需要多说,只是稍微要注意一点的是:如果是用 MySQL 的 Administrator 管理工具时,可以直接创建,不再需要声明。

 

(3). <wbr /> 参数

MySQL 存储过程的参数用在存储过程的定义,共有三种参数类型,IN,OUT,INOUT, 形式如:

CREATEPROCEDURE 存储过程名([[IN |OUT |INOUT] <wbr /> 参数名 <wbr /> 数据类形...])

IN <wbr /> 输入参数: 表示该参数的值必须在调用存储过程时指定,在存储过程中修改该参数的值不能被返回,为默认值

OUT <wbr /> 输出参数: 该值可在存储过程内部被改变,并可返回

INOUT <wbr /> 输入输出参数: 调用时指定,并且可被改变和返回

Ⅰ. IN 参数例子

创建: <wbr />

1. <wbr /> mysql <wbr />> <wbr />DELIMITER <wbr />//  <wbr />
2. <wbr /> mysql <wbr />> <wbr />CREATE <wbr />PROCEDURE <wbr />demo_in_parameter(IN <wbr />p_in <wbr />int)  <wbr />
3. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />BEGIN <wbr />  <wbr />
4. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />SELECT <wbr />p_in; <wbr />  <wbr />
5. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />SET <wbr />p_in=2; <wbr />  <wbr />
6. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />SELECT <wbr />p_in; <wbr />  <wbr />
7. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />END; <wbr />  <wbr />
8. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />//  <wbr />
9. <wbr /> mysql <wbr />> <wbr />DELIMITER <wbr />; <wbr />
执行结果:
1. <wbr /> mysql <wbr />> <wbr />SET <wbr />@p_in=1;  <wbr />
2. <wbr /> mysql <wbr />> <wbr />CALL <wbr />demo_in_parameter(@p_in);  <wbr />
3. <wbr /> +------+  <wbr />
4. <wbr /> | <wbr />p_in <wbr />|  <wbr />
5. <wbr /> +------+  <wbr />
6. <wbr /> | <wbr /> <wbr /> <wbr />1 <wbr /> <wbr />| <wbr />  <wbr />
7. <wbr /> +------+  <wbr />
8. <wbr />  <wbr />
9. <wbr /> +------+  <wbr />
10.| <wbr />p_in <wbr />|  <wbr />
11.+------+  <wbr />
12.| <wbr /> <wbr /> <wbr />2 <wbr /> <wbr />| <wbr />  <wbr />
13.+------+  <wbr />
14. <wbr />
15.mysql> <wbr />SELECT <wbr />@p_in;  <wbr />
16.+-------+  <wbr />
17.| <wbr />@p_in <wbr />|  <wbr />
18.+-------+  <wbr />
19.| <wbr /> <wbr />1 <wbr /> <wbr /> <wbr /> <wbr />|  <wbr />
20.+-------+  <wbr />
以上可以看出,p_in 虽然在存储过程中被修改,但并不影响 @p_id 的值 <wbr /> Ⅱ.OUT 参数例子 创建:
1. <wbr /> mysql <wbr />> <wbr />DELIMITER <wbr />//  <wbr />
2. <wbr /> mysql <wbr />> <wbr />CREATE <wbr />PROCEDURE <wbr />demo_out_parameter(OUT <wbr />p_out <wbr />int)  <wbr />
3. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />BEGIN <wbr />
4. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />SELECT <wbr />p_out;  <wbr />
5. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />SET <wbr />p_out=2;  <wbr />
6. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />SELECT <wbr />p_out;  <wbr />
7. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />END;  <wbr />
8. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />//  <wbr />
9. <wbr /> mysql <wbr />> <wbr />DELIMITER <wbr />; <wbr />
执行结果:
1. <wbr /> mysql <wbr />> <wbr />SET <wbr />@p_out=1;  <wbr />
2. <wbr /> mysql <wbr />> <wbr />CALL <wbr />sp_demo_out_parameter(@p_out);  <wbr />
3. <wbr /> +-------+  <wbr />
4. <wbr /> | <wbr />p_out <wbr />| <wbr />  <wbr />
5. <wbr /> +-------+  <wbr />
6. <wbr /> | <wbr />NULL <wbr /> <wbr />| <wbr />  <wbr />
7. <wbr /> +-------+  <wbr />
8. <wbr />  <wbr />
9. <wbr /> +-------+  <wbr />
10.| <wbr />p_out <wbr />|  <wbr />
11.+-------+  <wbr />
12.| <wbr /> <wbr /> <wbr />2 <wbr /> <wbr /> <wbr />| <wbr />  <wbr />
13.+-------+  <wbr />
14. <wbr />
15.mysql> <wbr />SELECT <wbr />@p_out;  <wbr />
16.+-------+  <wbr />
17.| <wbr />p_out <wbr />|  <wbr />
18.+-------+  <wbr />
19.| <wbr /> <wbr /> <wbr />2 <wbr /> <wbr /> <wbr />|  <wbr />
20.+-------+  <wbr />
Ⅲ. INOUT 参数例子 创建:
1. <wbr /> mysql <wbr />> <wbr />DELIMITER <wbr />//  <wbr /> <wbr />
2. <wbr /> mysql <wbr />> <wbr />CREATE <wbr />PROCEDURE <wbr />demo_inout_parameter(INOUT <wbr />p_inout <wbr />int)  <wbr /> <wbr />
3. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />BEGIN <wbr />
4. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />SELECT <wbr />p_inout;  <wbr />
5. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />SET <wbr />p_inout=2;  <wbr />
6. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />SELECT <wbr />p_inout; <wbr />  <wbr />
7. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />END;  <wbr />
8. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />// <wbr />  <wbr />
9. <wbr /> mysql <wbr />> <wbr />DELIMITER <wbr />; <wbr />
<wbr /> <wbr /> 执行结果:
1. <wbr /> mysql <wbr />> <wbr />SET <wbr />@p_inout=1;  <wbr />
2. <wbr /> mysql <wbr />> <wbr />CALL <wbr />demo_inout_parameter(@p_inout) <wbr />;  <wbr />
3. <wbr /> +---------+  <wbr />
4. <wbr /> | <wbr />p_inout <wbr />|  <wbr />
5. <wbr /> +---------+  <wbr />
6. <wbr /> | <wbr /> <wbr /> <wbr /> <wbr />1 <wbr /> <wbr /> <wbr /> <wbr />|  <wbr />
7. <wbr /> +---------+  <wbr />
8. <wbr />  <wbr />
9. <wbr /> +---------+  <wbr />
10.| <wbr />p_inout <wbr />| <wbr />  <wbr />
11.+---------+  <wbr />
12.| <wbr /> <wbr /> <wbr /> <wbr />2 <wbr /> <wbr /> <wbr /> <wbr />|  <wbr />
13.+---------+  <wbr />
14. <wbr />
15.mysql <wbr />> <wbr />SELECT <wbr />@p_inout;  <wbr />
16.+----------+  <wbr />
17.| <wbr />@p_inout <wbr />| <wbr />  <wbr />
18.+----------+  <wbr />
19.| <wbr /> <wbr /> <wbr /> <wbr />2 <wbr /> <wbr /> <wbr /> <wbr /> <wbr />|  <wbr />
20.+----------+ <wbr />
<wbr /> (4). <wbr /> 变量 Ⅰ. <wbr /> 变量定义 局部变量声明一定要放在存储过程体的开始 DECLAREvariable_name [,variable_name...] datatype [DEFAULT value]; 其中,datatype 为 MySQL 的数据类型,如:int, float, date,varchar(length) 例如:
1. <wbr /> DECLARE <wbr />l_int <wbr />int <wbr />unsigned <wbr />default <wbr />4000000;  <wbr />
2. <wbr /> DECLARE <wbr />l_numeric <wbr />number(8,2) <wbr />DEFAULT <wbr />9.95;  <wbr />
3. <wbr /> DECLARE <wbr />l_date <wbr />date <wbr />DEFAULT <wbr />'1999-12-31';  <wbr />
4. <wbr /> DECLARE <wbr />l_datetime <wbr />datetime <wbr />DEFAULT <wbr />'1999-12-31 <wbr />23:59:59';  <wbr />
5. <wbr /> DECLARE <wbr />l_varchar <wbr />varchar(255) <wbr />DEFAULT <wbr />'This <wbr />will <wbr />not <wbr />be <wbr />padded'; <wbr />  <wbr />
<wbr /> <wbr /> Ⅱ. <wbr /> 变量赋值 <wbr />SET <wbr /> 变量名 <wbr />= <wbr /> 表达式值 <wbr />[,variable_name = expression ...] <wbr /> Ⅲ. <wbr /> 用户变量 <wbr /> ⅰ. <wbr /> 在 MySQL 客户端使用用户变量
1. <wbr /> mysql <wbr />> <wbr />SELECT <wbr />'Hello <wbr />World' <wbr />into <wbr />@x;  <wbr />
2. <wbr /> mysql <wbr />> <wbr />SELECT <wbr />@x;  <wbr />
3. <wbr /> +-------------+  <wbr />
4. <wbr /> | <wbr /> <wbr /> <wbr />@x <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />|  <wbr />
5. <wbr /> +-------------+  <wbr />
6. <wbr /> | <wbr />Hello <wbr />World <wbr />|  <wbr />
7. <wbr /> +-------------+  <wbr />
8. <wbr /> mysql <wbr />> <wbr />SET <wbr />@y='Goodbye <wbr />Cruel <wbr />World';  <wbr />
9. <wbr /> mysql <wbr />> <wbr />SELECT <wbr />@y;  <wbr />
10.+---------------------+  <wbr />
11.| <wbr /> <wbr /> <wbr /> <wbr /> <wbr />@y <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />|  <wbr />
12.+---------------------+  <wbr />
13.| <wbr />Goodbye <wbr />Cruel <wbr />World <wbr />|  <wbr />
14.+---------------------+  <wbr />
15. <wbr />
16.mysql <wbr />> <wbr />SET <wbr />@z=1+2+3;  <wbr />
17.mysql <wbr />> <wbr />SELECT <wbr />@z;  <wbr />
18.+------+  <wbr />
19.| <wbr />@z <wbr /> <wbr /> <wbr />|  <wbr />
20.+------+  <wbr />
21.| <wbr /> <wbr />6 <wbr /> <wbr /> <wbr />|  <wbr />
22.+------+  <wbr />
ⅱ. <wbr /> 在存储过程中使用用户变量
1. <wbr /> mysql <wbr />> <wbr />CREATE <wbr />PROCEDURE <wbr />GreetWorld( <wbr />) <wbr />SELECT <wbr />CONCAT(@greeting,' <wbr />World');  <wbr />
2. <wbr /> mysql <wbr />> <wbr />SET <wbr />@greeting='Hello';  <wbr />
3. <wbr /> mysql <wbr />> <wbr />CALL <wbr />GreetWorld( <wbr />);  <wbr />
4. <wbr /> +----------------------------+  <wbr />
5. <wbr /> | <wbr />CONCAT(@greeting,' <wbr />World') <wbr />|  <wbr />
6. <wbr /> +----------------------------+  <wbr />
7. <wbr /> | <wbr /> <wbr />Hello <wbr />World <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />|  <wbr />
8. <wbr /> +----------------------------+  <wbr />
ⅲ. <wbr /> 在存储过程间传递全局范围的用户变量
1. <wbr /> mysql> <wbr />CREATE <wbr />PROCEDURE <wbr />p1() <wbr /> <wbr /> <wbr />SET <wbr />@last_procedure='p1';  <wbr />
2. <wbr /> mysql> <wbr />CREATE <wbr />PROCEDURE <wbr />p2() <wbr />SELECT <wbr />CONCAT('Last <wbr />procedure <wbr />was <wbr />',@last_procedure);  <wbr />
3. <wbr /> mysql> <wbr />CALL <wbr />p1( <wbr />);  <wbr />
4. <wbr /> mysql> <wbr />CALL <wbr />p2( <wbr />);  <wbr />
5. <wbr /> +-----------------------------------------------+  <wbr />
6. <wbr /> | <wbr />CONCAT('Last <wbr />procedure <wbr />was <wbr />',@last_proc <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> |  <wbr />
7. <wbr /> +-----------------------------------------------+  <wbr />
8. <wbr /> | <wbr />Last <wbr />procedure <wbr />was <wbr />p1 <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />|  <wbr />
9. <wbr /> +-----------------------------------------------+  <wbr />
<wbr /> <wbr /> 注意: ①用户变量名一般以 @开头 ②滥用用户变量会导致程序难以理解及管理 <wbr /> (5). <wbr /> 注释  <wbr /> MySQL 存储过程可使用两种风格的注释 双模杠:-- 该风格一般用于单行注释 c 风格: <wbr /> 一般用于多行注释 例如: <wbr />
1. <wbr /> mysql <wbr />> <wbr />DELIMITER <wbr />//  <wbr />
2. <wbr /> mysql <wbr />> <wbr />CREATE <wbr />PROCEDURE <wbr />proc1 <wbr />--name存储过程名  <wbr />
3. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />(IN <wbr />parameter1 <wbr />INTEGER) <wbr />  <wbr />
4. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />BEGIN <wbr />  <wbr />
5. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />DECLARE <wbr />variable1 <wbr />CHAR(10); <wbr />  <wbr />
6. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />IF <wbr />parameter1 <wbr />= <wbr />17 <wbr />THEN <wbr />  <wbr />
7. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />SET <wbr />variable1 <wbr />= <wbr />'birds'; <wbr />  <wbr />
8. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />ELSE <wbr />
9. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />SET <wbr />variable1 <wbr />= <wbr />'beasts'; <wbr />  <wbr />
10. <wbr /> <wbr /> <wbr /> <wbr /> -> <wbr />END <wbr />IF; <wbr />  <wbr />
11. <wbr /> <wbr /> <wbr /> <wbr /> -> <wbr />INSERT <wbr />INTO <wbr />table1 <wbr />VALUES <wbr />(variable1);  <wbr />
12. <wbr /> <wbr /> <wbr /> <wbr /> -> <wbr />END <wbr />  <wbr />
13. <wbr /> <wbr /> <wbr /> <wbr /> -> <wbr />//  <wbr />
14.mysql <wbr />> <wbr />DELIMITER <wbr />;  <wbr />
<wbr /> 4. <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />MySQL 存储过程的调用 用 call 和你过程名以及一个括号,括号里面根据需要,加入参数,参数包括输入参数、输出参数、输入输出参数。具体的调用方法可以参看上面的例子。 5. <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />MySQL 存储过程的查询 我们像知道一个数据库下面有那些表,我们一般采用 showtables; 进行查看。那么我们要查看某个数据库下面的存储过程,是否也可以采用呢?答案是,我们可以查看某个数据库下面的存储过程,但是是令一钟方式。 我们可以用 selectname from mysql.proc where db=’数据库名’; 或者 selectroutine_name from information_schema.routines where routine_schema='数据库名'; 或者 showprocedure status where db='数据库名'; 进行查询。 如果我们想知道,某个存储过程的详细,那我们又该怎么做呢?是不是也可以像操作表一样用 describe <wbr /> 表名进行查看呢? 答案是:我们可以查看存储过程的详细,但是需要用另一种方法: SHOWCREATE PROCEDURE <wbr /> 数据库. 存储过程名; 就可以查看当前存储过程的详细。 <wbr /> 6. <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />MySQL 存储过程的修改 ALTER PROCEDURE 更改用 CREATE PROCEDURE <wbr /> 建立的预先指定的存储过程,其不会影响相关存储过程或存储功能。 <wbr /> 7. <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />MySQL 存储过程的删除 删除一个存储过程比较简单,和删除表一样: DROPPROCEDURE 从 MySQL 的表格中删除一个或多个存储过程。 <wbr /> 8. <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />MySQL 存储过程的控制语句 (1). <wbr /> 变量作用域 内部的变量在其作用域范围内享有更高的优先权,当执行到 end。变量时,内部变量消失,此时已经在其作用域外,变量不再可见了,应为在存储 过程外再也不能找到这个申明的变量,但是你可以通过 out 参数或者将其值指派 给会话变量来保存其值。 <wbr /> <wbr />
1. <wbr /> mysql <wbr />> <wbr />DELIMITER <wbr />//  <wbr />
2. <wbr /> mysql <wbr />> <wbr />CREATE <wbr />PROCEDURE <wbr />proc3()  <wbr />
3. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />begin <wbr />
4. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />declare <wbr />x1 <wbr />varchar(5) <wbr />default <wbr />'outer';  <wbr />
5. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />begin <wbr />
6. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />declare <wbr />x1 <wbr />varchar(5) <wbr />default <wbr />'inner';  <wbr />
7. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />select <wbr />x1;  <wbr />
8. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />end;  <wbr />
9. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />select <wbr />x1;  <wbr />
10. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />end;  <wbr />
11. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />//  <wbr />
12.mysql <wbr />> <wbr />DELIMITER <wbr />;  <wbr />
<wbr />  <wbr />(2). <wbr /> 条件语句 Ⅰ. if-then -else 语句  <wbr />
1. <wbr /> mysql <wbr />> <wbr />DELIMITER <wbr />//  <wbr />
2. <wbr /> mysql <wbr />> <wbr />CREATE <wbr />PROCEDURE <wbr />proc2(IN <wbr />parameter <wbr />int)  <wbr />
3. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />begin <wbr />
4. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />declare <wbr />var <wbr />int;  <wbr />
5. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />set <wbr />var=parameter+1;  <wbr />
6. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />if <wbr />var=0 <wbr />then <wbr />
7. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />insert <wbr />into <wbr />t <wbr />values(17);  <wbr />
8. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />end <wbr />if;  <wbr />
9. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />if <wbr />parameter=0 <wbr />then <wbr />
10. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />update <wbr />t <wbr />set <wbr />s1=s1+1;  <wbr />
11. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />else <wbr />
12. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />update <wbr />t <wbr />set <wbr />s1=s1+2;  <wbr />
13. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />end <wbr />if;  <wbr />
14. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />end;  <wbr />
15. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />//  <wbr />
16.mysql <wbr />> <wbr />DELIMITER <wbr />;  <wbr />
Ⅱ. case 语句: <wbr />
1. <wbr /> mysql <wbr />> <wbr />DELIMITER <wbr />//  <wbr />
2. <wbr /> mysql <wbr />> <wbr />CREATE <wbr />PROCEDURE <wbr />proc3 <wbr />(in <wbr />parameter <wbr />int)  <wbr />
3. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />begin <wbr />
4. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />declare <wbr />var <wbr />int;  <wbr />
5. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />set <wbr />var=parameter+1;  <wbr />
6. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />case <wbr />var  <wbr />
7. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />when <wbr />0 <wbr />then <wbr />  <wbr />
8. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />insert <wbr />into <wbr />t <wbr />values(17);  <wbr />
9. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />when <wbr />1 <wbr />then <wbr />  <wbr />
10. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />insert <wbr />into <wbr />t <wbr />values(18);  <wbr />
11. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />else <wbr />  <wbr />
12. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />insert <wbr />into <wbr />t <wbr />values(19);  <wbr />
13. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />end <wbr />case;  <wbr />
14. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />end;  <wbr />
15. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />//  <wbr />
16.mysql <wbr />> <wbr />DELIMITER <wbr />; <wbr />
case
 <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> when var=0 then
 <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> insert into t values(30);
 <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> when var>0 then
 <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> when var<0 then
 <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> else
 <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />
end case
 <wbr />
 <wbr />
<wbr /> (3). <wbr /> 循环语句 Ⅰ. while ···· end while:
1. <wbr /> mysql <wbr />> <wbr />DELIMITER <wbr />//  <wbr />
2. <wbr /> mysql <wbr />> <wbr />CREATE <wbr />PROCEDURE <wbr />proc4()  <wbr />
3. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />begin <wbr />
4. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />declare <wbr />var <wbr />int;  <wbr />
5. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />set <wbr />var=0;  <wbr />
6. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />while <wbr />var<6 <wbr />do  <wbr />
7. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />insert <wbr />into <wbr />t <wbr />values(var);  <wbr />
8. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />set <wbr />var=var+1;  <wbr />
9. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />end <wbr />while;  <wbr />
10. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />end;  <wbr />
11. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />//  <wbr />
12.mysql <wbr />> <wbr />DELIMITER <wbr />; <wbr />
<wbr />while 条件 do -- 循环体 endwhile <wbr /> <wbr /> Ⅱ. repeat···· end repeat: 它在执行操作后检查结果,而 while 则是执行前进行检查。
1. <wbr /> mysql <wbr />> <wbr />DELIMITER <wbr />//  <wbr />
2. <wbr /> mysql <wbr />> <wbr />CREATE <wbr />PROCEDURE <wbr />proc5 <wbr />()  <wbr />
3. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />begin <wbr />  <wbr />
4. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />declare <wbr />v <wbr />int;  <wbr />
5. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />set <wbr />v=0;  <wbr />
6. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />repeat  <wbr />
7. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />insert <wbr />into <wbr />t <wbr />values(v);  <wbr />
8. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />set <wbr />v=v+1;  <wbr />
9. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />until <wbr />v>=5  <wbr />
10. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />end <wbr />repeat;  <wbr />
11. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />end;  <wbr />
12. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />//  <wbr />
13.mysql <wbr />> <wbr />DELIMITER <wbr />;  <wbr />
<wbr />repeat -- 循环体 until 循环条件 <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> endrepeat; <wbr /> Ⅲ. loop ·····endloop: loop 循环不需要初始条件,这点和 while <wbr /> 循环相似,同时和 repeat 循环一样不需要结束条件, leave 语句的意义是离开循环。
1. <wbr /> mysql <wbr />> <wbr />DELIMITER <wbr />//  <wbr />
2. <wbr /> mysql <wbr />> <wbr />CREATE <wbr />PROCEDURE <wbr />proc6 <wbr />()  <wbr />
3. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />begin <wbr />
4. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />declare <wbr />v <wbr />int;  <wbr />
5. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />set <wbr />v=0;  <wbr />
6. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />LOOP_LABLE:loop  <wbr />
7. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />insert <wbr />into <wbr />t <wbr />values(v);  <wbr />
8. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />set <wbr />v=v+1;  <wbr />
9. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />if <wbr />v <wbr />>=5 <wbr />then <wbr />
10. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />leave <wbr />LOOP_LABLE;  <wbr />
11. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />end <wbr />if;  <wbr />
12. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />end <wbr />loop;  <wbr />
13. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />end;  <wbr />
14. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />//  <wbr />
15.mysql <wbr />> <wbr />DELIMITER <wbr />;  <wbr />
<wbr /> <wbr /> Ⅳ. LABLES <wbr /> 标号: 标号可以用在 begin repeat while <wbr /> 或者 loop <wbr /> 语句前,语句标号只能在合法的语句前面使用。可以跳出循环,使运行指令达到复合语句的最后一步。 <wbr /> (4). ITERATE 迭代 Ⅰ. ITERATE:
1. <wbr /> <wbr /> <wbr /> 通过引用复合语句的标号,来从新开始复合语句
2. <wbr /> mysql <wbr />> <wbr />DELIMITER <wbr />//  <wbr />
3. <wbr /> mysql <wbr />> <wbr />CREATE <wbr />PROCEDURE <wbr />proc10 <wbr />()  <wbr />
4. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />begin <wbr />
5. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />declare <wbr />v <wbr />int;  <wbr />
6. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />set <wbr />v=0;  <wbr />
7. <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />LOOP_LABLE:loop  <wbr />
8.  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />if <wbr />v=3 <wbr />then <wbr />  <wbr />
9.  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />set <wbr />v=v+1;  <wbr />
10. <wbr /> <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />ITERATE <wbr />LOOP_LABLE;  <wbr />
11. <wbr /> <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />end <wbr />if;  <wbr />
12. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />insert <wbr />into <wbr />t <wbr />values(v);  <wbr />
13. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />set <wbr />v=v+1;  <wbr />
14. <wbr /> <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />if <wbr />v>=5 <wbr />then <wbr />
15. <wbr /> <wbr />  <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />leave <wbr />LOOP_LABLE;  <wbr />
16. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />end <wbr />if;  <wbr />
17. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />end <wbr />loop;  <wbr />
18. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />end;  <wbr />
19. <wbr /> <wbr /> <wbr /> <wbr /> <wbr />-> <wbr />//  <wbr />
20.mysql <wbr />> <wbr />DELIMITER <wbr />; <wbr />
<wbr /> <wbr /> 9. <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />MySQL 存储过程的基本函数  <wbr /> (1). 字符串类 CHARSET(str) // 返回字串字符集 CONCAT (string2 [,...]) // 连接字串 INSTR (string ,substring) // 返回 substring 首次在 string 中出现的位置, 不存在返回 0 LCASE (string2) // 转换成小写 LEFT (string2 ,length) // 从 string2 中的左边起取 length 个字符 LENGTH (string) //string 长度 LOAD_FILE (file_name) // 从文件读取内容 LOCATE (substring , string [,start_position] ) <wbr /> 同 INSTR, 但可指定开始位置 LPAD (string2 ,length ,pad) // 重复用 pad 加在 string 开头, 直到字串长度为 length LTRIM (string2) // 去除前端空格 REPEAT (string2 ,count) // 重复 count 次 REPLACE (str ,search_str ,replace_str) // 在 str 中用 replace_str 替换 search_str RPAD (string2 ,length ,pad) // 在 str 后用 pad 补充, 直到长度为 length RTRIM (string2) // 去除后端空格 STRCMP (string1 ,string2) // 逐字符比较两字串大小, SUBSTRING (str , position [,length]) // 从 str 的 position 开始, 取 length 个字符, 注:mysql 中处理字符串时,默认第一个字符下标为 1,即参数 position 必须大于等于 1 <wbr />
1. <wbr /> mysql> <wbr />select <wbr />substring('abcd',0,2);  <wbr />
2. <wbr /> +-----------------------+  <wbr />
3. <wbr /> | <wbr />substring('abcd',0,2) <wbr />|  <wbr />
4. <wbr /> +-----------------------+  <wbr />
5. <wbr /> | <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />|  <wbr />
6. <wbr /> +-----------------------+  <wbr />
7. <wbr /> 1 <wbr />row <wbr />in <wbr />set <wbr />(0.00 <wbr />sec)  <wbr />
8. <wbr />  <wbr />
9. <wbr /> mysql> <wbr />select <wbr />substring('abcd',1,2);  <wbr />
10.+-----------------------+  <wbr />
11.| <wbr />substring('abcd',1,2) <wbr />|  <wbr />
12.+-----------------------+  <wbr />
13.| <wbr /> <wbr /> <wbr /> <wbr /> <wbr />ab <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />|  <wbr />
14.+-----------------------+  <wbr />
15.1 <wbr />row <wbr />in <wbr />set <wbr />(0.02 <wbr />sec)  <wbr />
TRIM([BOTH|LEADING|TRAILING FROM]string2) // 去除指定位置的指定字符 UCASE (string2) // 转换成大写 RIGHT(string2,length) // 取 string2 最后 length 个字符 SPACE(count) // 生成 count 个空格 (2). 数学类 ABS (number2) // 绝对值 BIN (decimal_number) // 十进制转二进制 CEILING (number2) // 向上取整 CONV(number2,from_base,to_base) // 进制转换 FLOOR (number2) // 向下取整 FORMAT (number,decimal_places) // 保留小数位数 HEX (DecimalNumber) // 转十六进制 注:HEX() 中可传入字符串,则返回其 ASC-11 码,如 HEX('DEF') 返回 4142143 也可以传入十进制整数,返回其十六进制编码,如 HEX(25) 返回 19 LEAST (number , number2 [,..]) // 求最小值 MOD (numerator ,denominator) // 求余 POWER (number ,power) // 求指数 RAND([seed]) // 随机数 ROUND (number [,decimals]) // 四舍五入,decimals 为小数位数 ] 注:返回类型并非均为整数,如: (1) 默认变为整形值
1. <wbr /> mysql> <wbr />select <wbr />round(1.23);  <wbr />
2. <wbr /> +-------------+  <wbr />
3. <wbr /> | <wbr />round(1.23) <wbr />|  <wbr />
4. <wbr /> +-------------+  <wbr />
5. <wbr /> | <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />1 <wbr />|  <wbr />
6. <wbr /> +-------------+  <wbr />
7. <wbr /> 1 <wbr />row <wbr />in <wbr />set <wbr />(0.00 <wbr />sec)  <wbr />
8. <wbr />  <wbr />
9. <wbr /> mysql> <wbr />select <wbr />round(1.56);  <wbr />
10.+-------------+  <wbr />
11.| <wbr />round(1.56) <wbr />|  <wbr />
12.+-------------+  <wbr />
13.| <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />2 <wbr />|  <wbr />
14.+-------------+  <wbr />
15.1 <wbr />row <wbr />in <wbr />set <wbr />(0.00 <wbr />sec) <wbr />
(2) 可以设定小数位数,返回浮点型数据
1. <wbr /> mysql> <wbr />select <wbr />round(1.567,2);  <wbr />
2. <wbr /> +----------------+  <wbr />
3. <wbr /> | <wbr />round(1.567,2) <wbr />|  <wbr />
4. <wbr /> +----------------+  <wbr />
5. <wbr /> | <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />1.57 <wbr />|  <wbr />
6. <wbr /> +----------------+  <wbr />
7. <wbr /> 1 <wbr />row <wbr />in <wbr />set <wbr />(0.00 <wbr />sec) <wbr />
SIGN (number2) //  <wbr /> (3). 日期时间类 ADDTIME (date2 ,time_interval)// 将 time_interval 加到 date2 CONVERT_TZ (datetime2 ,fromTZ ,toTZ) // 转换时区 CURRENT_DATE ( ) // 当前日期 CURRENT_TIME ( ) // 当前时间 CURRENT_TIMESTAMP ( ) // 当前时间戳 DATE (datetime) // 返回 datetime 的日期部分 DATE_ADD (date2 , INTERVAL d_value d_type) // 在 date2 中加上日期或时间 DATE_FORMAT (datetime ,FormatCodes) // 使用 formatcodes 格式显示 datetime DATE_SUB (date2 , INTERVAL d_value d_type) // 在 date2 上减去一个时间 DATEDIFF (date1 ,date2) // 两个日期差 DAY (date) // 返回日期的天 DAYNAME (date) // 英文星期 DAYOFWEEK (date) // 星期 (1-7) ,1 为星期天 DAYOFYEAR (date) // 一年中的第几天 EXTRACT (interval_name FROM date) // 从 date 中提取日期的指定部分 MAKEDATE (year ,day) // 给出年及年中的第几天, 生成日期串 MAKETIME (hour ,minute ,second) // 生成时间串 MONTHNAME (date) // 英文月份名 NOW ( ) // 当前时间 SEC_TO_TIME (seconds) // 秒数转成时间 STR_TO_DATE (string ,format) // 字串转成时间, 以 format 格式显示 TIMEDIFF (datetime1 ,datetime2) // 两个时间差 TIME_TO_SEC (time) // 时间转秒数 ] WEEK (date_time [,start_of_week]) // 第几周 YEAR (datetime) // 年份 DAYOFMONTH(datetime) // 月的第几天 HOUR(datetime) // 小时 LAST_DAY(date) //date 的月的最后日期 MICROSECOND(datetime) // 微秒 MONTH(datetime) // 月 MINUTE(datetime) // 分返回符号, 正负或 0 SQRT(number2) // 开平方 <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <p align="left">MySql 分页存储过程 </p> <p align="left">MySql 测试版本:5.0.41-community-nt</p> <p align="left"> DROP PROCEDURE IF EXISTS pr_pager; CREATE PROCEDURE pr_pager(<wbr /> <wbr /> <wbr /> <wbr />IN <wbr /> <wbr /> <wbr /> <wbr />p_table_name <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />VARCHAR(1024), <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />IN <wbr /> <wbr /> <wbr /> <wbr />p_fields <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />VARCHAR(1024), <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />IN <wbr /> <wbr /> <wbr /> <wbr />p_page_size <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />INT, <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />IN <wbr /> <wbr /> <wbr /> <wbr />p_page_now <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />INT, <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />IN <wbr /> <wbr /> <wbr /> <wbr />p_order_string <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />VARCHAR(128), <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />IN <wbr /> <wbr /> <wbr /> <wbr />p_where_string <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />VARCHAR(1024), <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> OUT <wbr /> <wbr /> <wbr /> <wbr />p_out_rows <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />INT <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> ) <wbr /> <wbr /> <wbr /> <wbr />NOT DETERMINISTIC <wbr /> <wbr /> <wbr /> <wbr /> SQL SECURITY DEFINER <wbr /> <wbr /> <wbr /> <wbr /> COMMENT '分页存储过程' <wbr /> <wbr /> <wbr /> <wbr /> BEGIN <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />DECLARE m_begin_row INT DEFAULT 0; <wbr /> <wbr /> <wbr /> <wbr />DECLARE m_limit_string CHAR(64); <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />SET m_begin_row = (p_page_now - 1) * p_page_size; <wbr /> <wbr /> <wbr /> <wbr />SET m_limit_string = CONCAT('LIMIT', m_begin_row, ',', p_page_size); <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />SET @COUNT_STRING = CONCAT('SELECT COUNT(*) INTO @ROWS_TOTAL FROM', p_table_name, ' ', p_where_string); <wbr /> <wbr /> <wbr /> <wbr />SET @MAIN_STRING = CONCAT('SELECT', p_fields, 'FROM', p_table_name, '', p_where_string,' ', p_order_string,m_limit_string); <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />PREPARE count_stmt FROM @COUNT_STRING; <wbr /> <wbr /> <wbr /> <wbr />EXECUTE count_stmt; <wbr /> <wbr /> <wbr /> <wbr />DEALLOCATE PREPARE count_stmt; <wbr /> <wbr /> <wbr /> <wbr />SET p_out_rows = @ROWS_TOTAL; <wbr /> <wbr /> <wbr /> <wbr />PREPARE main_stmt FROM @MAIN_STRING; <wbr /> <wbr /> <wbr /> <wbr />EXECUTE main_stmt; <wbr /> <wbr /> <wbr /> <wbr />DEALLOCATE PREPARE main_stmt; <wbr /> <wbr /> <wbr /> <wbr /> END;</p>  <wbr /> <wbr /> 调用 <p align="left">mysql> call pr_pager("t","var",3,3,"","",@result);</p> <p align="left">mysql> call pr_pager("t","var",3,2,"","",@result);</p>