|
nguyenvantruongthang
Starting Member
12 Posts |
Posted - 2008-04-22 : 03:58:09
|
| I got problem with using custom paging in sql 2005SET ANSI_NULLS OFFGOSET QUOTED_IDENTIFIER OFFGOALTER PROCEDURE [dbo].[searchperson_view_general] @Search nvarchar(2000) ,@OrderBy nvarchar (2000) ,@PageSize int ,@PageIndex int ASDECLARE @PageLowerBound intDECLARE @PageUpperBound intSET @PageLowerBound = @PageSize * @PageIndexSET @PageUpperBound = @PageSize - 1 + @PageLowerBound--Default order by to first columnIF (@OrderBy is null or LEN(@OrderBy) < 1)BEGIN SET @OrderBy = 'p.[person_id]'END-- SQL Server 2005 Pagingdeclare @SQL as nvarchar(4000)SET @SQL = 'WITH PageIndex AS ('SET @SQL = @SQL + ' SELECT distinct'IF @PageSize > 0BEGINSET @SQL = @SQL + ' TOP ' + convert(nvarchar, @PageUpperBound)ENDSET @SQL = @SQL + ' ROW_NUMBER() OVER (ORDER BY ' + @OrderBy + ') as RowIndex 'SET @SQL = @SQL + ', p.[person_id]'SET @SQL = @SQL + ', p.[userType_id]'SET @SQL = @SQL + ', p.[fullName]'SET @SQL = @SQL + ', p.[gender_nm]'SET @SQL = @SQL + ', p.[dateOfBirth] 'SET @SQL = @SQL + ', p.[positionTitle]'SET @SQL = @SQL + ' FROM dbo.[person_view] p 'IF LEN(@Search) > 0BEGIN SET @SQL = @SQL + @SearchENDSET @SQL = @SQL + ' ) SELECT distinct'SET @SQL = @SQL + ' p.person_id'SET @SQL = @SQL + ', p.userType_id'SET @SQL = @SQL + ', p.fullName'SET @SQL = @SQL + ', p.gender_nm'SET @SQL = @SQL + ', (year(getdate()) - year(p.[dateOfBirth])) as [dateOfBirth] 'SET @SQL = @SQL + ', p.positionTitle'SET @SQL = @SQL + ' FROM PageIndex p 'SET @SQL = @SQL + ' WHERE RowIndex > ' + convert(nvarchar, @PageLowerBound)IF @PageSize > 0BEGINSET @SQL = @SQL + ' AND RowIndex <= ' + convert(nvarchar, @PageUpperBound)ENDSET @SQL = @SQL + ' ORDER BY ' + @OrderByexec sp_executesql @SQLI checked my store procedure with parametersexec [hr2b_searchperson_view_general_load] 'LEFT OUTER JOIN qualification ON p.person_id = qualification.person_id WHERE qualification.institutionName like N''%ABC%''',' p.person_id asc ', 25 , 1This is a actual query show : WITH PageIndex AS ( SELECT distinct TOP 49 ROW_NUMBER() OVER (ORDER BY p.person_id asc ) as RowIndex , p.[person_id], p.[userType_id], p.[fullName], p.[gender_nm], p.[dateOfBirth] , p.[positionTitle]FROM person_view pLEFT OUTER JOIN qualification ON p.person_id = qualification.person_id WHERE qualification.institutionName like N'%ABC%' ) SELECT distinctp.person_id, p.userType_id, p.fullName, p.gender_nm, (year(getdate()) - year(p.[dateOfBirth])) as [dateOfBirth] , p.positionTitleFROM PageIndex pWHERE RowIndex > 25 AND RowIndex <= 49 ORDER BY p.person_id asc If I used this query without using DISTINCT it will return extactly number of records which I expected but It is duplicated.Then I tried to use DISTINCT in this query number of records return is less than 25 records . Because it was duplicated records when I used LEFT OUTER JOIN.But my query will be able to use more LEFT OUTER JOIN than this query. Please help me get exactly 25 records?This is my tablesperson_view(person_id, fullname, userType_id, gender_nm, dateOfBirth, positionTitle)Qualification(qualification_id, qualification_nm,institutionName, person_id)Thanks in advance. |
|