473,406 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

SQL Server paged recordset problem

I have implemented the Stored Procedure "RowCount" method of controlling
paged recordsets in ASP as shown in this page
http://www.aspfaq.com/show.asp?id=2120.

I have had to make heavy alterations to the code in order for it to work
with my application.

It all worked fine until I tried to sort my data by a field which contained
a NULL value (even in just one record). Before I added additional criteria
for the Unique_ID field the stored procedure returned zero records when
sorting by such a field. I then added the Unique_ID criteria to the ORDER BY
and WHERE clauses (as seen below) and it excludes records.

In short obviously what I want this to do is order by fields that contain
NULL values (e.g. fld4 in sample data below). It currently pages and orders
perfectly in fields which have data in every row.

If anyone reads this and helps me, thanks *very* much in advance.

(P.S. sorry about the horrible state of my code but I've been messing with
it trying to get this fixed for ages)
(P.P.S the reason for passing in the long SQL string as a parameter in the
SP is because the string is generated using complicated loops and I found
this much easier in ASP than doing it in SQL)

******************************************

Here is an example of a call to the procedure from ASP:

EXEC st_paging_rowcount 7, 1, 50, 'SET ROWCOUNT 50 SELECT utbl7.*,
utbl6_1.fld1 AS fld3_Name FROM utbl7 LEFT OUTER JOIN utbl6 utbl6_1 ON
utbl7.fld3 = utbl6_1.Unique_ID WHERE utbl7.fld5 >= @fld1val OR
utbl7.Unique_ID >= @uniqueid ORDER BY utbl7.fld5',5

******************************************

Here is the SQL SP:

CREATE PROCEDURE st_paging_rowcount
@tableid INT,
@pagenum INT = 1,
@perpage INT = 50,
@finalselect NVARCHAR(1500),
@sortfield INT
AS
BEGIN
SET NOCOUNT ON

DECLARE
@ubound INT,
@lbound INT,
@pages INT,
@rows INT,
@querystring1 NVARCHAR(200),
@querystring2 NVARCHAR(200),
@querystring3 NVARCHAR(200)

SELECT @querystring1 = N'SELECT @rows = COUNT(*),
@pages = COUNT(*) / @perpage
FROM utbl' + CAST(@tableid AS NVARCHAR(15)) + ' WITH (NOLOCK)'
--SELECT GOGOGO = @querystring1
EXEC sp_executesql
@querystring1,
N'@rows INT OUTPUT, @pages INT OUTPUT, @perpage INT',
@rows OUTPUT, @pages OUTPUT, @perpage

IF @rows % @perpage != 0 SET @pages = @pages + 1
IF @pagenum < 1 SET @pagenum = 1
IF @pagenum > @pages SET @pagenum = @pages

SET @ubound = @perpage * @pagenum
SET @lbound = @ubound - (@perpage - 1)

SELECT
CurrentPage = @pagenum,
TotalPages = @pages,
TotalRows = @rows

-- this method determines the string values
-- for the first desired row, then sets the
-- rowcount to get it, plus the next n rows

DECLARE @fld1val NVARCHAR(64)
DECLARE @uniqueid INT

SELECT @querystring2 = N'SET ROWCOUNT ' + CAST(@lbound AS NVARCHAR(15))
+ '
SELECT
@fld1val = fld' + CAST(@sortfield AS NVARCHAR(15)) + ', @uniqueid
= Unique_ID
FROM
utbl' + CAST(@tableid AS NVARCHAR(15)) + ' WITH (NOLOCK)
ORDER BY
fld' + CAST(@sortfield AS NVARCHAR(15)) +', Unique_ID '
SELECT test0 = @querystring2

EXEC sp_executesql
@querystring2,
N'@fld1val NVARCHAR(64) OUTPUT, @uniqueid INT OUTPUT',
@fld1val OUTPUT, @uniqueid OUTPUT

--SELECT test = @finalselect

EXEC sp_executesql
@finalselect,
N'@fld1val NVARCHAR(64), @uniqueid INT',
@fld1val, @uniqueid

SET ROWCOUNT 0
END

*********************************************

Here is some sample data from the table (e.g. would like to sort by fld4):

Unique_ID Date_Added Who_Added Locked fld1
fld2 fld3 fld4
----------- --------------------------- ----------- ----------- ------------------------------
------------------------------
3 2005-09-16 16:12:30.200 1 0 Smith
John 1 NULL
4 2005-09-16 16:12:41.013 1 0 Jones
Chris 1 NULL
6 2005-09-16 16:13:10.187 1 0 Stamov
Stilian 1 NULL
7 2005-09-16 16:19:15.437 1 0 Lewicki
Steve 1 Colchester
8 2005-09-16 16:19:36.937 1 0 James
Phil 1 NULL
9 2005-09-16 16:20:35.327 1 0 Leroy
Didier 1 NULL
Sep 19 '05 #1
3 1927

"Steve" <st***@hello.com> wrote in message news:43********@x-privat.org...
I have implemented the Stored Procedure "RowCount" method of controlling
paged recordsets in ASP as shown in this page
http://www.aspfaq.com/show.asp?id=2120.

I have had to make heavy alterations to the code in order for it to work
with my application.

It all worked fine until I tried to sort my data by a field which
contained a NULL value (even in just one record). Before I added
additional criteria for the Unique_ID field the stored procedure returned
zero records when sorting by such a field. I then added the Unique_ID
criteria to the ORDER BY and WHERE clauses (as seen below) and it excludes
records.

In short obviously what I want this to do is order by fields that contain
NULL values (e.g. fld4 in sample data below). It currently pages and
orders perfectly in fields which have data in every row.

If anyone reads this and helps me, thanks *very* much in advance.

(P.S. sorry about the horrible state of my code but I've been messing with
it trying to get this fixed for ages)
(P.P.S the reason for passing in the long SQL string as a parameter in the
SP is because the string is generated using complicated loops and I found
this much easier in ASP than doing it in SQL)

******************************************

Here is an example of a call to the procedure from ASP:

EXEC st_paging_rowcount 7, 1, 50, 'SET ROWCOUNT 50 SELECT utbl7.*,
utbl6_1.fld1 AS fld3_Name FROM utbl7 LEFT OUTER JOIN utbl6 utbl6_1 ON
utbl7.fld3 = utbl6_1.Unique_ID WHERE utbl7.fld5 >= @fld1val OR
utbl7.Unique_ID >= @uniqueid ORDER BY utbl7.fld5',5

******************************************

Here is the SQL SP:

CREATE PROCEDURE st_paging_rowcount
@tableid INT,
@pagenum INT = 1,
@perpage INT = 50,
@finalselect NVARCHAR(1500),
@sortfield INT
AS
BEGIN
SET NOCOUNT ON

DECLARE
@ubound INT,
@lbound INT,
@pages INT,
@rows INT,
@querystring1 NVARCHAR(200),
@querystring2 NVARCHAR(200),
@querystring3 NVARCHAR(200)

SELECT @querystring1 = N'SELECT @rows = COUNT(*),
@pages = COUNT(*) / @perpage
FROM utbl' + CAST(@tableid AS NVARCHAR(15)) + ' WITH (NOLOCK)'
--SELECT GOGOGO = @querystring1
EXEC sp_executesql
@querystring1,
N'@rows INT OUTPUT, @pages INT OUTPUT, @perpage INT',
@rows OUTPUT, @pages OUTPUT, @perpage

IF @rows % @perpage != 0 SET @pages = @pages + 1
IF @pagenum < 1 SET @pagenum = 1
IF @pagenum > @pages SET @pagenum = @pages

SET @ubound = @perpage * @pagenum
SET @lbound = @ubound - (@perpage - 1)

SELECT
CurrentPage = @pagenum,
TotalPages = @pages,
TotalRows = @rows

-- this method determines the string values
-- for the first desired row, then sets the
-- rowcount to get it, plus the next n rows

DECLARE @fld1val NVARCHAR(64)
DECLARE @uniqueid INT

SELECT @querystring2 = N'SET ROWCOUNT ' + CAST(@lbound AS NVARCHAR(15))
+ '
SELECT
@fld1val = fld' + CAST(@sortfield AS NVARCHAR(15)) + ', @uniqueid
= Unique_ID
FROM
utbl' + CAST(@tableid AS NVARCHAR(15)) + ' WITH (NOLOCK)
ORDER BY
fld' + CAST(@sortfield AS NVARCHAR(15)) +', Unique_ID '
SELECT test0 = @querystring2

EXEC sp_executesql
@querystring2,
N'@fld1val NVARCHAR(64) OUTPUT, @uniqueid INT OUTPUT',
@fld1val OUTPUT, @uniqueid OUTPUT

--SELECT test = @finalselect

EXEC sp_executesql
@finalselect,
N'@fld1val NVARCHAR(64), @uniqueid INT',
@fld1val, @uniqueid

SET ROWCOUNT 0
END

*********************************************

Here is some sample data from the table (e.g. would like to sort by fld4):

Unique_ID Date_Added Who_Added Locked fld1 fld2
fld3 fld4
----------- --------------------------- ----------- ----------- ------------------------------
------------------------------
3 2005-09-16 16:12:30.200 1 0 Smith John
1 NULL
4 2005-09-16 16:12:41.013 1 0 Jones
Chris 1 NULL
6 2005-09-16 16:13:10.187 1 0 Stamov
Stilian 1 NULL
7 2005-09-16 16:19:15.437 1 0 Lewicki
Steve 1 Colchester
8 2005-09-16 16:19:36.937 1 0 James Phil
1 NULL
9 2005-09-16 16:20:35.327 1 0 Leroy
Didier 1 NULL


The WHERE and ORDER BY clauses should be:

WHERE utbl7.fld5 >= @fld1val
OR (utbl7.fld5 = @fld1val AND utbl7.Unique_ID > @uniqueid)
ORDER BY utbl7.fld5, utbl7.Unique_ID
Sep 19 '05 #2
Joe
"Chris Hohmann" <no****@thankyou.com> wrote in message
news:Ol**************@tk2msftngp13.phx.gbl...

The WHERE and ORDER BY clauses should be:

WHERE utbl7.fld5 >= @fld1val
OR (utbl7.fld5 = @fld1val AND utbl7.Unique_ID > @uniqueid)
ORDER BY utbl7.fld5, utbl7.Unique_ID


Hi Chris, thanks for the suggestion but I tried what you suggested and it
returns zero records when sorting by columns containing NULL values (still
works perfectly for columns filled with data).

The reason for this is that the SP determines the first value on the page as
NULL (@fld1val). This has the effect that the WHERE clause becomes "WHERE
utbl7.fld5 >= NULL OR (utbl7.fld5 = NULL AND utbl7.Unique_ID > @uniqueid)",
thus returning zero records. I realise there is just a flaw in my logic
somewhere but I just can't seem to get my head round it.

Any further suggestions?

My updated code follows:

*****************************

SP call:

EXEC st_paging_rowcount 7, 1, 50, 'SET ROWCOUNT 50 SELECT utbl7.*,
utbl6_1.fld1 AS fld3_Name FROM utbl7 LEFT OUTER JOIN utbl6 utbl6_1 ON
utbl7.fld3 = utbl6_1.Unique_ID WHERE utbl7.fld5 >= @fld1val OR (utbl7.fld5 =
@fld1val AND utbl7.Unique_ID > @uniqueid) ORDER BY utbl7.fld5,
utbl7.Unique_ID',5
*************

SP:

CREATE PROCEDURE st_paging_rowcount
@tableid INT,
@pagenum INT = 1,
@perpage INT = 50,
@finalselect NVARCHAR(1500),
@sortfield INT
AS
BEGIN
SET NOCOUNT ON

DECLARE
@ubound INT,
@lbound INT,
@pages INT,
@rows INT,
@querystring1 NVARCHAR(200),
@querystring2 NVARCHAR(200),
@querystring3 NVARCHAR(200)

SELECT @querystring1 = N'SELECT @rows = COUNT(*),
@pages = COUNT(*) / @perpage
FROM utbl' + CAST(@tableid AS NVARCHAR(15)) + ' WITH (NOLOCK)'
--SELECT GOGOGO = @querystring1
EXEC sp_executesql
@querystring1,
N'@rows INT OUTPUT, @pages INT OUTPUT, @perpage INT',
@rows OUTPUT, @pages OUTPUT, @perpage

IF @rows % @perpage != 0 SET @pages = @pages + 1
IF @pagenum < 1 SET @pagenum = 1
IF @pagenum > @pages SET @pagenum = @pages

SET @ubound = @perpage * @pagenum
SET @lbound = @ubound - (@perpage - 1)

SELECT
CurrentPage = @pagenum,
TotalPages = @pages,
TotalRows = @rows

-- this method determines the string values
-- for the first desired row, then sets the
-- rowcount to get it, plus the next n rows

DECLARE @fld1val NVARCHAR(64)
DECLARE @uniqueid INT

SELECT @querystring2 = N'SET ROWCOUNT ' + CAST(@lbound AS NVARCHAR(15))
+ '
SELECT
@fld1val = fld' + CAST(@sortfield AS NVARCHAR(15)) + ', @uniqueid
= Unique_ID
FROM
utbl' + CAST(@tableid AS NVARCHAR(15)) + ' WITH (NOLOCK)
ORDER BY
fld' + CAST(@sortfield AS NVARCHAR(15)) +', Unique_ID '
--SELECT test0 = @querystring2

EXEC sp_executesql
@querystring2,
N'@fld1val NVARCHAR(64) OUTPUT, @uniqueid INT OUTPUT',
@fld1val OUTPUT, @uniqueid OUTPUT
SELECT firstpageval = @fld1val, uniqueid = @uniqueid

--SELECT test = @finalselect

EXEC sp_executesql
@finalselect,
N'@fld1val NVARCHAR(64), @uniqueid INT',
@fld1val, @uniqueid

SET ROWCOUNT 0
END

Sep 19 '05 #3

"Joe" <jo*@hotmail.com> wrote in message
news:e3**************@TK2MSFTNGP09.phx.gbl...
"Chris Hohmann" <no****@thankyou.com> wrote in message
news:Ol**************@tk2msftngp13.phx.gbl...

The WHERE and ORDER BY clauses should be:

WHERE utbl7.fld5 >= @fld1val
OR (utbl7.fld5 = @fld1val AND utbl7.Unique_ID > @uniqueid)
ORDER BY utbl7.fld5, utbl7.Unique_ID


Hi Chris, thanks for the suggestion but I tried what you suggested and it
returns zero records when sorting by columns containing NULL values (still
works perfectly for columns filled with data).

The reason for this is that the SP determines the first value on the page
as NULL (@fld1val). This has the effect that the WHERE clause becomes
"WHERE utbl7.fld5 >= NULL OR (utbl7.fld5 = NULL AND utbl7.Unique_ID >
@uniqueid)", thus returning zero records. I realise there is just a flaw
in my logic somewhere but I just can't seem to get my head round it.

Any further suggestions?


WHERE utbl7.fld5 >= @fld1val
OR (utbl7.fld5 = @fld1val AND utbl7.Unique_ID > @uniqueid)
OR (utbl7.fld5 IS NULL AND @fld1val IS NULL AND utbl7.Unique_ID >
@uniqueid)
ORDER BY utbl7.fld5, utbl7.Unique_ID
Sep 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Kathryn | last post by:
Hiya I have a problem with using some client side and server side scripting together in an ASP. I'm using VBScript. What I'm trying to achieve is this - - Page loads up and some server side...
26
by: David W. Fenton | last post by:
A client is panicking about their large Access application, which has been running smoothly with 100s of thousands of records for quite some time. They have a big project in the next year that will...
1
by: Yangtsi River | last post by:
Hi, I am retrive record from an Access database and want them displayed page by page, I used oledbdatareader to retrive and assigned the datasource of datagrid control to this reader. but the...
1
by: Steve Mauldin | last post by:
I have Two ASP.Net applications running on a windows 2000 Server box. One is www.abc.com and one is www.dfg.com . They have the same code in them to connect to the same SQL Server 2000 database...
11
by: noleander | last post by:
Ive got a time-critical application that runs on Windows XP and Unix. I've got some timing issues that Ive traced to virtual memory getting paged in and out. I think I can solve the problem if...
0
by: fergaloc | last post by:
Hi there. Our .net framework 1.1 application is a complex media player that plays back video, flash, web, TV and pictures full screen. It runs on Windows XP SP2. It has 12 DLLs and runs to about...
4
by: ipez75 | last post by:
Hello everyone, I have a web application written in asp 6.0, my problem is that I execute a sql server store procedure and I get an empty recordset, while executing the same sp on query anlyzer I...
17
by: Anil Gupte | last post by:
I am using the following to try to connect to the database, but it does not seem to be working. Dim sConnString sConnString = "Provider=SQLNCLI.1;Integrated Security=SSPI;Persist Security...
6
by: jsacrey | last post by:
Hello everybody, I've got a bit of a situation that I could use some guidance with if possible. I work for an auditing firm where my users audit electronic shipping data for customers to see if...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.