yuvarajupadhyaya

Pagination in sql

Jan 19th, 2023
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 2.34 KB | Source Code | 0 0
  1.  
  2. -- =============================================    
  3. -- Author:  Sudip Thapa    
  4. -- Create date: 9/29/2022    
  5. -- Description: sp to get notification count by notification type    
  6. -- =============================================    
  7. -- usp_GetUserNotification 49446,1,10    
  8. CREATE PROCEDURE usp_GetUserNotification  
  9.     @UserID INT,  
  10.     @PageNo INT,  
  11.     @PageSize INT  
  12. AS  
  13. BEGIN  
  14.  
  15.     SET NOCOUNT ON;  
  16.  
  17.     DECLARE @GradeID INT;  
  18.     SELECT @GradeID = utg.GradeID  
  19.     FROM dbo.UserDetail AS ud (NOLOCK)  
  20.         INNER JOIN dbo.UserTargetGrade AS utg (NOLOCK)  
  21.             ON utg.UserTargetGradeID = ud.UserTargetGradeID  
  22.     WHERE ud.UserID = @UserID;  
  23.  
  24.  
  25.     SELECT COUNT(u.UserNotificationID) OVER () AS TotalCount,  
  26.            [u].[NotificationTypeID],  
  27.            u.UserNotificationID,  
  28.            u.NotificationTypeSpecificID,  
  29.            u.[Notification],  
  30.            CASE  
  31.                WHEN u.Payload IS NULL THEN  
  32.                    ''  
  33.                ELSE  
  34.                    u.Payload  
  35.            END AS PayLoadString  
  36.     FROM dbo.[UserNotifications] AS [u] (NOLOCK)  
  37.         INNER JOIN dbo.UserDetail AS ud (NOLOCK)  
  38.             ON ud.UserID = u.UserID  
  39.         LEFT JOIN dbo.Notices AS n (NOLOCK)  
  40.             ON n.NoticeId = u.NotificationTypeSpecificID  
  41.                AND u.NotificationTypeID = 42  
  42.         LEFT JOIN dbo.NoticeGrade AS ng (NOLOCK)  
  43.             ON ng.NoticeId = n.NoticeId  
  44.         LEFT JOIN dbo.GradeGroupGrade AS ggg (NOLOCK)  
  45.             ON ggg.GradeGroupID = ng.GradeGroupID  
  46.                AND ggg.GradeID = @GradeID  
  47.     WHERE [u].[UserID] = @UserID  
  48.           AND [u].[IsRead] = 0  
  49.           AND [u].[IsActive] = 1  
  50.           AND [u].[IsDeleted] = 0  
  51.           AND  
  52.           (  
  53.               [u].[StartDate] IS NULL  
  54.               OR [u].[StartDate] <= GETUTCDATE()  
  55.           )  
  56.           AND  
  57.           (  
  58.               n.NoticeId IS NULL  
  59.               OR  
  60.               (  
  61.                   n.NoticeId > 0  
  62.                   AND  
  63.                   (  
  64.                       ng.GradeID = @GradeID  
  65.                       OR ggg.GradeGroupGradeID IS NOT NULL  
  66.                   )  
  67.               )  
  68.           )  
  69.     ORDER BY u.AddedOn DESC OFFSET (@PageNo - 1) * @PageSize ROWS FETCH NEXT @PageSize ROWS ONLY;  
  70. END;
Add Comment
Please, Sign In to add comment