Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a stored procedure which returns result from two tables using outer join and where conditions. It has order by clause as well. I want to add paging to it so that only requested number of records are returned. How can I do it? I need to supply pagenumber, totalnumber of records, current page etc ? My stored procedure is:

CREATE PROCEDURE [dbo].[hr_SearchVacanciesForService]

    @SearchText NVARCHAR(50) = NULL,
    @DutyStationID INT = NULL,
    @VacancyCategoryIDs VARCHAR(1000) = NULL,
    @Language INT = 1
AS

SELECT *
FROM dbo.hr_Vacancies LEFT OUTER JOIN dbo.hr_DutyStations ON dbo.hr_Vacancies.DutyStationID = dbo.hr_DutyStations.DutyStationID 
    LEFT OUTER JOIN dbo.hr_Companies    
        ON dbo.hr_Vacancies.CompanyID = dbo.hr_Companies.CompanyID 
WHERE dbo.hr_Vacancies.Deleted = 0 
        AND (dbo.hr_Vacancies.JobTitleLang1 LIKE @LoacalSeacrchText 
        OR dbo.hr_Vacancies.JobTitleLang2 LIKE @LoacalSeacrchText 
        OR dbo.hr_Vacancies.DescriptionLang1 LIKE @LoacalSeacrchText 
        OR dbo.hr_Vacancies.DescriptionLang2 LIKE @LoacalSeacrchText    
    AND (dbo.hr_Vacancies.DutyStationID = @DutyStationID OR @DutyStationID IS NULL OR @DutyStationID = 0)
    ORDER BY HavePriority DESC, StartDate DESC, dbo.hr_Vacancies.VacancyID DESC
share|improve this question
Have a look here: codeproject.com/Articles/6936/…. – Daniel Dusek Mar 28 at 8:17

1 Answer

Use option with CTE and OVER() clause

CREATE PROCEDURE [dbo].[hr_SearchVacanciesForService]
@SearchText NVARCHAR(50) = NULL,
@DutyStationID INT = NULL,
@VacancyCategoryIDs VARCHAR(1000) = NULL,
@Language INT = 1,
@NumberOfPages int
AS
;WITH cte AS
 (
  SELECT *, ROW_NUMBER() OVER (ORDER BY HavePriority DESC, StartDate DESC, dbo.hr_Vacancies.VacancyID DESC) AS Pages
  FROM dbo.hr_Vacancies LEFT OUTER JOIN dbo.hr_DutyStations ON dbo.hr_Vacancies.DutyStationID = dbo.hr_DutyStations.DutyStationID 
                        LEFT OUTER JOIN dbo.hr_Companies ON dbo.hr_Vacancies.CompanyID = dbo.hr_Companies.CompanyID 
  WHERE dbo.hr_Vacancies.Deleted = 0 
    AND (dbo.hr_Vacancies.JobTitleLang1 LIKE @LoacalSeacrchText 
    OR dbo.hr_Vacancies.JobTitleLang2 LIKE @LoacalSeacrchText 
    OR dbo.hr_Vacancies.DescriptionLang1 LIKE @LoacalSeacrchText 
    OR dbo.hr_Vacancies.DescriptionLang2 LIKE @LoacalSeacrchText    
    AND (dbo.hr_Vacancies.DutyStationID = @DutyStationID OR @DutyStationID IS NULL OR @DutyStationID = 0)
  )
  SELECT *, COUNT(*) OVER() AS totalOfPages
  FROM cte
  WHERE Pages BETWEEN 1 AND ISNULL(@NumberOfPages, Pages)

Example using OVER() clause with expressions:

SELECT ... ROW_NUMBER() OVER (ORDER BY
CASE WHEN dbo.hr_Vacancies.Priority = 0 
     THEN 0 ELSE 
CASE WHEN CONVERT(DATETIME,CONVERT(CHAR(10),dbo.hr_Vacancies.PriorityDateExpired,101)) > CONVERT(DATETIME,CONVERT(CHAR(10),GETDATE(),101)) OR dbo.hr_Vacancies.PriorityDateExpired IS NULL 
     THEN 1 ELSE 0 END END DESC, your_second_expression_StartDate DESC) 

If you want to show records from 20 to 30:

CREATE PROCEDURE [dbo].[hr_SearchVacanciesForService]

    @SearchText NVARCHAR(50) = NULL,
    @DutyStationID INT = NULL,
    @VacancyCategoryIDs VARCHAR(1000) = NULL,
    @Language INT = 1,
    @StartPage int = NULL,
    @EndPage int = NULL
AS
;WITH cte AS
 (
  SELECT *, ROW_NUMBER() OVER (ORDER BY your_case_expressionForColumnHavePriority DESC, your_case_expressionForColumnStartDate DESC, dbo.hr_Vacancies.VacancyID DESC) AS Pages
  FROM dbo.hr_Vacancies LEFT OUTER JOIN dbo.hr_DutyStations ON dbo.hr_Vacancies.DutyStationID = dbo.hr_DutyStations.DutyStationID 
                        LEFT OUTER JOIN dbo.hr_Companies ON dbo.hr_Vacancies.CompanyID = dbo.hr_Companies.CompanyID 
  WHERE dbo.hr_Vacancies.Deleted = 0 
          AND (dbo.hr_Vacancies.JobTitleLang1 LIKE @LoacalSeacrchText 
          OR dbo.hr_Vacancies.JobTitleLang2 LIKE @LoacalSeacrchText 
          OR dbo.hr_Vacancies.DescriptionLang1 LIKE @LoacalSeacrchText 
          OR dbo.hr_Vacancies.DescriptionLang2 LIKE @LoacalSeacrchText    
      AND (dbo.hr_Vacancies.DutyStationID = @DutyStationID OR @DutyStationID IS NULL OR @DutyStationID = 0)
  )
  SELECT *, COUNT(*) OVER() AS totalOfPages
  FROM cte
  WHERE Pages BETWEEN ISNULL(@StartPage, Pages) AND ISNULL(@EndPage, Pages)
share|improve this answer
Thanks for your response. What If I need to select few columns and not * like 15 columns, Do I need to repeat then in both selects ? inside with and after with ? – DotnetSparrow Mar 28 at 8:40
No only in first SELECT statement. – Alexander Fedorenko Mar 28 at 8:41
It gives error on havepriority : ;WITH cte AS ( SELECT ROW_NUMBER() OVER (ORDER BY HavePriority DESC, StartDate DESC, dbo.hr_Vacancies.VacancyID DESC) AS Pages, ... error is Msg 207, Level 16, State 1, Procedure hr_SearchVacanciesForService, Line 33 Invalid column name 'HavePriority'.. where as this column exists – DotnetSparrow Mar 28 at 8:49
Column HavePriority and StartDate exists in your tables? – Alexander Fedorenko Mar 28 at 9:00
yes, they exist – DotnetSparrow Mar 28 at 9:31
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.