Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Stored procedure using variable in LIMIT expression?
Let us firs create a table
mysql> create table LimitWithStoredProcedure -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(10) -> ); Query OK, 0 rows affected (0.47 sec)
Insert some records in the table using insert command.
The query is as follows
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)
Display all records from the table using select statement.
The query is as follows
mysql> select *from LimitWithStoredProcedure;
The following is the output
+----+---------+ | 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)
Here is your stored procedure, using variable in LIMIT expression
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 the stored procedure with the help of CALL command.
The query is as follows
mysql> call Sp_limit(4,7);
The following is the output
+----+--------+ | 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)
Advertisements