먼저 테이블을 생성해 보겠습니다.
mysql> create table LimitWithStoredProcedure -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(10) -> ); Query OK, 0 rows affected (0.47 sec)
삽입 명령을 사용하여 테이블에 일부 레코드를 삽입하십시오.
쿼리는 다음과 같습니다
mysql> insert into LimitWithStoredProcedure(Name) values('John'); Query OK, 1 row affected (0.15 sec) mysql> insert into LimitWithStoredProcedure(Name) values('Chris'); Query OK, 1 row affected (0.15 sec) mysql> insert into LimitWithStoredProcedure(Name) values('Maxwell'); Query OK, 1 row affected (0.28 sec) mysql> insert into LimitWithStoredProcedure(Name) values('Bob'); Query OK, 1 row affected (0.24 sec) mysql> insert into LimitWithStoredProcedure(Name) values('David'); Query OK, 1 row affected (0.20 sec) mysql> insert into LimitWithStoredProcedure(Name) values('Carol'); Query OK, 1 row affected (0.18 sec) mysql> insert into LimitWithStoredProcedure(Name) values('James'); Query OK, 1 row affected (0.29 sec) mysql> insert into LimitWithStoredProcedure(Name) values('Jace'); Query OK, 1 row affected (0.13 sec) mysql> insert into LimitWithStoredProcedure(Name) values('Robert'); Query OK, 1 row affected (0.20 sec) mysql> insert into LimitWithStoredProcedure(Name) values('Mike'); Query OK, 1 row affected (0.07 sec) mysql> insert into LimitWithStoredProcedure(Name) values('Sam'); Query OK, 1 row affected (0.25 sec) mysql> insert into LimitWithStoredProcedure(Name) values('Peter'); Query OK, 1 row affected (0.14 sec) mysql> insert into LimitWithStoredProcedure(Name) values('Ramit'); Query OK, 1 row affected (0.18 sec) mysql> insert into LimitWithStoredProcedure(Name) values('Tony'); Query OK, 1 row affected (0.16 sec)
select 문을 사용하여 테이블의 모든 레코드를 표시합니다.
쿼리는 다음과 같습니다
mysql> select *from LimitWithStoredProcedure;
다음은 출력입니다.
+----+---------+ | Id | Name | +----+---------+ | 1 | John | | 2 | Chris | | 3 | Maxwell | | 4 | Bob | | 5 | David | | 6 | Carol | | 7 | James | | 8 | Jace | | 9 | Robert | | 10 | Mike | | 11 | Sam | | 12 | Peter | | 13 | Ramit | | 14 | Tony | +----+---------+ 14 rows in set (0.00 sec)
다음은 LIMIT 표현식의 변수를 사용하는 저장 프로시저입니다.
mysql> DELIMITER // mysql> CREATE PROCEDURE Sp_limit(IN beg INTEGER, IN end INTEGER ) -> BEGIN -> PREPARE myStatement FROM -> "select *from LimitWithStoredProcedure LIMIT ?,? "; -> SET @beginning = beg; -> SET @ending = end ; -> EXECUTE myStatement USING @beginning, @ending; -> DEALLOCATE PREPARE myStatement; -> END // Query OK, 0 rows affected (0.25 sec) mysql> DELIMITER ;
CALL 명령을 사용하여 저장 프로시저를 호출합니다.
쿼리는 다음과 같습니다
mysql> call Sp_limit(4,7);
다음은 출력입니다.
+----+--------+ | Id | Name | +----+--------+ | 5 | David | | 6 | Carol | | 7 | James | | 8 | Jace | | 9 | Robert | | 10 | Mike | | 11 | Sam | +----+--------+ 7 rows in set (0.00 sec) Query OK, 0 rows affected (0.03 sec)