使用 MySQL 创建存储过程并设置限制以仅显示特定数量的记录
mysqlmysqli database
首先我们创建一个表 −
mysql> create table DemoTable1368 -> ( -> ClientId int, -> ClientName varchar(20) -> ); Query OK, 0 rows affected (0.58 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable1368 values(101,'Adam'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1368 values(102,'Bob'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1368 values(103,'John'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1368 values(104,'Sam'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1368 values(105,'Mike'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1368 values(106,'Carol'); Query OK, 1 row affected (0.11 sec)
使用 select 语句显示表中的所有记录 −
mysql> select * from DemoTable1368;
这将产生以下输出 −
+----------+------------+ | ClientId | ClientName | +----------+------------+ | 101 | Adam | | 102 | Bob | | 103 | John | | 104 | Sam | | 105 | Mike | | 106 | Carol | +----------+------------+ 6 rows in set (0.00 sec)
以下是创建存储过程并使用 MySQL LIMIT 来限制要显示的记录数的查询 −
mysql> DELIMITER // mysql> CREATE PROCEDURE limit_Demo(IN limitValue int) -> BEGIN -> DECLARE limValue int; -> SET limValue =limitValue ; -> SELECT * FROM DemoTable1368 LIMIT limValue; -> END -> // Query OK, 0 rows affected (0.17 sec) mysql> DELIMITER ;
现在您可以使用 call 命令调用存储过程 −
mysql> CALL limit_Demo(3);
这将产生以下输出 −
+----------+------------+ | ClientId | ClientName | +----------+------------+ | 101 | Adam | | 102 | Bob | | 103 | John | +----------+------------+ 3 rows in set (0.00 sec) Query OK, 0 rows affected (0.03 sec)