使用MySQL创建存储过程并设置显示指定数量记录的限制
首先我们创建一个表 –
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)
阅读更多:MySQL 教程