Paging Through Stored Procedure


Stored Procedure written in MYSQL which displays total no. of records along with page serial numbers, when table name and page size are passed as the parameter. When executed it will display two record sets, first record set displays total no. of records and the second result set displays the page serial number.

Hi all,
We are well familiarized with the concept of grid view paging, suppose if we want to achieve the same technique with stored procedure, then this will help u a lot.

This Stored Procedure in MYSQL displays total no. of records along with page serial numbers, when table name and page size are passed as the parameter.


DELIMITER $$

CREATE PROCEDURE `proc_pageCnt`(tn varchar(100),cn int)
begin

set @tableNm = tn; -- Table Name;
set @cnt = cn; -- page size;
set @tot = 0;

set @st1 = CONCAT('select count(*) from ',@tableNm,' into @tot');
-- creates the query for fetching total no. of records from the given table(any valid table)
PREPARE stmt2 FROM @st1;
EXECUTE stmt2;
-- executes the created query
DEALLOCATE PREPARE stmt2;
select cast(@tot as unsigned) as 'Total_Row_count';
-- converts the value to the integer type

set @st1 = CONCAT('select ceiling(((select count(*) from ',@tableNm,')/@cnt)) into @tot');
-- calculate no. of rows to be displayed
PREPARE stmt2 FROM @st1;
EXECUTE stmt2;
DEALLOCATE PREPARE stmt2;

set @count = 0;
set @st1 = CONCAT('SELECT @count:=@count+1 AS `count` FROM ', @tableNm, ' limit ', @tot);
-- creates the query for displaying the rows
PREPARE stmt2 FROM @st1;
EXECUTE stmt2;
DEALLOCATE PREPARE stmt2;

end $$


the above stored procedure can be executed as follows



create table EmployeeDetails (id int, empNm varchar(25));
insert into EmployeeDetails values(1,'emp1');
insert into EmployeeDetails values(2,'emp2');
insert into EmployeeDetails values(3,'emp3');
insert into EmployeeDetails values(4,'emp4');
insert into EmployeeDetails values(5,'emp5');
insert into EmployeeDetails values(6,'emp6');
insert into EmployeeDetails values(7,'emp7');
insert into EmployeeDetails values(8,'emp8');
insert into EmployeeDetails values(9,'emp9');
insert into EmployeeDetails values(10,'emp10');

call proc_pageCnt('EmployeeDetails',5);
call proc_pageCnt('EmployeeDetails where empNm like "e%" ',5);



Here first parameter is the table name, the second parameter is the page size.

When this code is executed it will display two record sets, first record set displays total no. of records present in that table and the second result set displays the page serial no. based on the page size passed as parameter.

For example if the table contains 21 records, and if the page size is 5, then it will display 1,2,3,4,5. If the page size is 10, then it ll display 1,2,3.

Note: U can refer to my previous article Executing Multiple queries in single shot for executing multiple queries in single shot


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: