AJPatankar
asked on
SQL 2005 RowCount Paging Stored Procedure
How would I make the following SQL 2005 code into an efficient stored procedure:
WITH TestRows AS
(
SELECT *, ROW_NUMBER() OVER(ORDER BY ID) AS 'RowNum' FROM TEST %s
)
SELECT * FROM TestRows WHERE RowNum BETWEEN %d AND %d
%s is replaced by a where clause defined at runtime. E.g. %s = WHERE ID = 1 or %s = "WHERE User = 'bob' AND Result='Pass'"
%d is replaced by numbers defining the rows to turn, e.g. 0 and 499 or 500 and 999 respectively
I did the following which works
PROCEDURE [dbo].[sp_TestPage]
-- Add the parameters for the stored procedure here
@Where varchar(255),
@low varchar,
@high varchar
AS
BEGIN
DECLARE @sql varchar(1000)
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
set @sql = 'WITH TestRows AS ( SELECT *, ROW_NUMBER() OVER(ORDER BY ID) AS RowNum FROM TEST ' + @Where + ')
SELECT * FROM TestRows WHERE RowNum BETWEEN ' + @low + ' AND ' + @high
exec(@sql)
END
But as I am effectively creating the sql dynamically in the stored procedure (as the WHERE clause is NEVER known at until runtime) it actually runs marginally slower than sending over the raw sql!
Many Thanks,
WITH TestRows AS
(
SELECT *, ROW_NUMBER() OVER(ORDER BY ID) AS 'RowNum' FROM TEST %s
)
SELECT * FROM TestRows WHERE RowNum BETWEEN %d AND %d
%s is replaced by a where clause defined at runtime. E.g. %s = WHERE ID = 1 or %s = "WHERE User = 'bob' AND Result='Pass'"
%d is replaced by numbers defining the rows to turn, e.g. 0 and 499 or 500 and 999 respectively
I did the following which works
PROCEDURE [dbo].[sp_TestPage]
-- Add the parameters for the stored procedure here
@Where varchar(255),
@low varchar,
@high varchar
AS
BEGIN
DECLARE @sql varchar(1000)
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
set @sql = 'WITH TestRows AS ( SELECT *, ROW_NUMBER() OVER(ORDER BY ID) AS RowNum FROM TEST ' + @Where + ')
SELECT * FROM TestRows WHERE RowNum BETWEEN ' + @low + ' AND ' + @high
exec(@sql)
END
But as I am effectively creating the sql dynamically in the stored procedure (as the WHERE clause is NEVER known at until runtime) it actually runs marginally slower than sending over the raw sql!
Many Thanks,
ASKER
Thanks Ritesh but I can't quite get that to fit. Your solution accepts paramters for the where clause. However, my application could have anything in the where clause, this needs to be passed in as a string and concatenated in somehow. How would I achieve this with your solutions?
Thanks again,
Thanks again,
don't you have fixed fields in WHERE condition. this is something unusual.
ASKER
My table has ~100 columns and my application has drop down/checkbox filter criteria so I am using the result of these user interface selections to determine the where clause
filter.jpg
filter.jpg
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
your case may help others too so that I post the same script in my article too.
http://www.sqlhub.com/2009/10/paging-stored-procedure-in-sql-server.html
http://www.sqlhub.com/2009/10/paging-stored-procedure-in-sql-server.html
ASKER
Absolutely superb. Much better than my original. Many Thanks!
http://www.sqlhub.com/2009/10/paging-stored-procedure-in-sql-server.html