Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Cannot work after page it

Posted on 2009-05-18
2
Medium Priority
?
221 Views
Last Modified: 2012-05-07
I have a sql statement, it can work properly as below SQL1.

I need to page it though a page function PAGE. The PAGE works properly for all the other sqls.

But the new page SQL2 select no results.

Any suggestion?

SQL1:
select ack, Date, [Time], Region, Station, Panel, Phase, VoltageLevel, [Value],  Comments
 from
gensum left join comment ON gensum.commentkey=comment.comment_id where ((Date>'2008-05-18' or (Date='2008-05-18' and Time>'00:00:00')) and (Date<'2009-05-18' or (Date='2009-05-18' and Time<'18:36:06')))

SQL2
returnpage 'select ack, Date, [Time], Region, Station, Panel, Phase, VoltageLevel,
[Value],
Comments
 from
gensum left join comment ON gensum.commentkey=comment.comment_id where ((Date>''2008-05-18'' or (Date=''2008-05-18'' and Time>''00:00:00'')) and (Date<''2009-05-18'' or (Date=''2009-05-18'' and Time<''18:36:06'')))', 'Date asc, Time asc, gensumid asc', 1, 26
PAGE function:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[ReturnPage](@Select varchar(8000), @OrderBy varchar(8000), 
                            @StartRow int, @EndRow int)
AS
BEGIN
declare @ColList varchar(8000);
declare @Where varchar(8000);
declare @i int;  
declare @i2 int;
declare @tmp varchar(8000);
declare @dec varchar(8000);
declare @f varchar(1000);
declare @d varchar(1000);
declare @Symbol char(20);
declare @SQL varchar(8000);
declare @Sort varchar(8000);
 
set @Sort = @OrderBy + ', '
set @dec = ''
set @Where  = ''
set @SQL = ''
set @i = charindex(',' , @Sort)
 
while @i != 0
 begin
  set @tmp = left(@Sort,@i-1)
  set @i2 = charindex(' ', @tmp)
  set @f = ltrim(rtrim(left(@tmp,@i2-1)))
  set @d = ltrim(rtrim(substring(@tmp,@i2+1,100)))
  set @Sort = rtrim(ltrim(substring(@Sort,@i+1,100)))
  set @i = charindex(',', @Sort)
  set @symbol = case when @d = 'ASC' then '>' else '<' end + 
                case when @i=0 then '=' else '' end
 
  set @dec = @dec + 'declare @' + @f + ' sql_variant; '
  set @ColList = isnull(replace(replace(@colList,'>','='),'<','=') + ' and ','') +
                 @f + @Symbol + ' @' + @f
  set @Where = @Where + ' OR (' + @ColList + ') '
  set @SQL = @SQL + ', @' + @f + '= ' + @f
 end
 
set @SQL = @dec + ' ' +
           'SET ROWCOUNT ' + convert(varchar(10), @StartRow) + '; ' +
           'SELECT ' + substring(@SQL,3,7000) + ' from (' + @Select + ') a ORDER BY ' +
           @OrderBy + '; ' + 'SET ROWCOUNT ' + 
           convert(varchar(10), 1 + @EndRow - @StartRow) + '; ' +
           'select * from (' + @Select + ') a WHERE ' + 
           substring(@Where,4,7000) + ' ORDER BY ' + @OrderBy + '; SET ROWCOUNT 0;'
 
exec(@SQL)
END

Open in new window

0
Comment
Question by:turbot_yu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 32

Accepted Solution

by:
Brendt Hess earned 2000 total points
ID: 24497536
You may be better off with a function using the SQL 2005 ROW_NUMBER() functionality.  Consider your original query to your paging function:

returnpage 'select ack, Date, [Time], Region, Station, Panel, Phase, VoltageLevel,
[Value],
Comments
 from
gensum left join comment ON gensum.commentkey=comment.comment_id where ((Date>''2008-05-18'' or (Date=''2008-05-18'' and Time>''00:00:00'')) and (Date<''2009-05-18'' or (Date=''2009-05-18'' and Time<''18:36:06'')))', 'Date asc, Time asc, gensumid asc', 1, 26

This could be subsumed as a stored procedure, using ROW_NUMBER(), and be easily executed - example:

CREATE PROCEDURE ReturnPage
    @Fields varchar(2000),
    @FromWhere varchar(4000),
    @Order varchar(1024),
    @Page int,
    @RowsPerPage int
AS
    DECLARE @sql varchar(8000)
    SET @sql = 'WITH rs AS (SELECT ' + @Fields + ', ROW_NUMBER() OVER (ORDER BY ' + @Order + ') AS wkrnx ' + @FromWhere + ') SELECT ' + @Fields + ' FROM rs WHERE wkrnx BETWEEN ' + Cast((Page - 1) * @RowsPerPage + 1 as varchar(10)) + ' AND ' + Cast(Page * RowsPerPage as Varchar(10))'

EXEC (@sql)

EXEC ReturnPage, 'ack, Date, [Time], Region, Station, Panel, Phase, VoltageLevel, [Value],  Comments', 'FROM gensum
LEFT JOIN comment
      ON gensum.commentkey=comment.comment_id
WHERE (Date>'2008-05-18' or (
            Date='2008-05-18'
            and Time>='00:00:00'            -- What handles time 0 exactly?  Use gt or eq here
            )
      ) and (Date<'2009-05-18' or (
            Date='2009-05-18'
            and Time<'18:36:06'
            )
      )', 'Date asc, Time asc, gensumid asc', 1, 26

Granted, you must break up the source query a bit more, but the execution should be quicker, and the understandability of the code is increased quite a bit.
0
 
LVL 32

Assisted Solution

by:Brendt Hess
Brendt Hess earned 2000 total points
ID: 24497548
I wish I could edit comments.  Sigh

The section of SQL through EXEC (@sql) is the stored procedure.  The SQL below that is an invocation of that stored procedure.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question