I have the following stored procedure to get next record(for navigation it is used) i have another one for getting prior.The problem is it is getting the same record again and again when i call this SP.Can you tell me what is wrong in the following: I used the distinct, so it is getting the records perfect afater that, something is wrong.Please help thank you very much for the info.****************************************************************CREATE PROCEDURE dbo.USP_GetNextCN (@CNID int, @UserName nvarchar(50), @Deleted bit, @CNRecordReturned int OUTPUT) ASDECLARE @ErrorCode intDECLARE @NextCNID intDECLARE @CNDeleted bitDECLARE @SQLStatement nvarchar(4000)DECLARE @SQLWhere nvarchar(100)SET NOCOUNT ONSET @NextCNID = '-1' -- set a condition to indicate to indicate that current CNID is lastSET @CNRecordReturned = '-1' -- set a condition that will indicate that no next CN record is availableIF @Deleted = 1 SET @SQLWhere = ''ELSE SET @SQLWhere = ' AND A.Deleted = 0 '-- Declare a cursor to hold all the CNIDs this user is authorized to accessDECLARE @CUR_TEMP CURSORSET @SQLStatement = N'SET @CUR_TEMP = CURSOR SCROLL FOR SELECT DISTINCT A.CNID, A.Deleted FROM TAB_ccsNetCN AS A join TAB_ccsNetUserAccess AS B ON A.ProgID = B.ProgID and A.ProjID = B.ProjID and A.ContractID = B.ContractID WHERE B.ccsNetModule = ' + char(39) + 'CN' + char(39) + ' AND B.UserName = ' + char(39) + @UserName + Char(39) + @SQLWhere + ' ORDER BY A.CNID; OPEN @CUR_TEMP'EXEC sp_executesql @SQLStatement,N'@CUR_TEMP CURSOR OUTPUT', @CUR_TEMP OUTPUTFETCH FIRST FROM @CUR_TEMP INTO @NextCNID, @CNDeletedWHILE @NextCNID <> @CNID -- loop through cursor until you find current CNID recordBEGIN FETCH NEXT FROM @CUR_TEMP INTO @NextCNID, @CNDeletedENDIF @CNDeleted = 1 BEGIN WHILE @CNDeleted <> 0 -- loop through cursor until you find an active CNID record BEGIN FETCH NEXT FROM @CUR_TEMP INTO @NextCNID, @CNDeleted IF @@FETCH_STATUS = -1 -- it gets the last record and it is deleted. So the scrolling will be on the other direction BEGIN WHILE @CNDeleted <> 0 -- loop through cursor until you find an active CNID record BEGIN FETCH PRIOR FROM @CUR_TEMP INTO @NextCNID, @CNDeleted END END END ENDELSE BEGIN FETCH NEXT FROM @CUR_TEMP INTO @NextCNID, @CNDeleted --get the next CNID for this user ENDIF @@FETCH_STATUS <> 0 SET @NextCNID = '-1'CLOSE @CUR_TEMPDEALLOCATE @CUR_TEMPSELECT @ErrorCode = @@ErrorIF @ErrorCode = 0BEGIN -- Get CN MASTER AND Child records for the next CN IF @NextCNID <> -1 EXECUTE USP_GetThisCN @NextCNID, @UserName, 0, @CNRecordReturned = @CNRecordReturned OUTENDSET NOCOUNT OFFRETURN @ErrorCodeGO