Advertisement
Shuva_Dev

Stored Procedure

Jan 19th, 2025
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 2.63 KB | None | 0 0
  1. USE [AspnetB11]
  2. GO
  3. /****** Object:  StoredProcedure [dbo].[GetCourseEnrollments]    Script Date: 12/27/2024 11:17:31 PM ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. ALTER   PROCEDURE [dbo].[GetCourseEnrollments]
  9. @PageIndex INT,
  10. @PageSize INT,
  11. @OrderBy nvarchar(50),
  12. @CourseName nvarchar(250) = '%',
  13. @StudentName nvarchar(250) = '%',
  14. @FeeFrom DECIMAL = NULL,
  15. @FeeTo DECIMAL = NULL,
  16. @Total INT output,
  17. @TotalDisplay INT output
  18.  
  19. AS
  20. BEGIN
  21.  
  22.     DECLARE @SQL nvarchar(2000);
  23.     DECLARE @countSql nvarchar(2000);
  24.     DECLARE @paramList nvarchar(MAX);
  25.     DECLARE @countparamList nvarchar(MAX);
  26.     DECLARE @xTotalDisplay INT;
  27.  
  28.     SELECT @Total = COUNT(*) FROM CourseStudents;
  29.  
  30.     SET @countsql = 'select @xTotalDisplay = count(*) from CourseStudents cs inner join
  31.                     Courses c on cs.CourseId = c.Id inner join
  32.                     Students s on cs.StudentId = s.Id  where 1 = 1 ';
  33.  
  34.                     IF @CourseName IS NOT NULL
  35.                     SET @countsql = @countsql + ' AND c.Name LIKE ''%'' + @xCourseName + ''%'''
  36.  
  37.                     IF @StudentName IS NOT NULL
  38.                     SET @countsql = @countsql + ' AND s.Name LIKE ''%'' + @xStudentName + ''%'''
  39.  
  40.                     IF @FeeFrom IS NOT NULL
  41.                     SET @countsql = @countsql + ' AND Fee >= @xFeeFrom'
  42.  
  43.                     IF @FeeTo IS NOT NULL
  44.                     SET @countsql = @countsql + ' AND Fee <= @xFeeTo'
  45.  
  46.     SET @SQL = 'select c.Name as CourseName, s.Name as StudentName, c.Fee from CourseStudents cs inner join
  47.                 Courses c on cs.CourseId = c.Id inner join
  48.                 Students s on cs.StudentId = s.Id where 1 = 1 ';
  49.  
  50.                 IF @CourseName IS NOT NULL
  51.                 SET @SQL = @SQL + ' AND c.Name LIKE ''%'' + @xCourseName + ''%'''
  52.  
  53.                 IF @StudentName IS NOT NULL
  54.                 SET @SQL = @SQL + ' AND s.Name LIKE ''%'' + @xStudentName + ''%'''
  55.  
  56.                 IF @FeeFrom IS NOT NULL
  57.                 SET @SQL = @SQL + ' AND Fee >= @xFeeFrom'
  58.  
  59.                 IF @FeeTo IS NOT NULL
  60.                 SET @SQL = @SQL + ' AND Fee <= @xFeeTo'
  61.  
  62.  
  63.  
  64.                 SET @SQL = @SQL + ' Order by '+@OrderBy+' OFFSET @xPageSize * (@xPageIndex - 1)
  65.                 ROWS FETCH NEXT @xPageSize ROWS ONLY';
  66.  
  67.  
  68.     SELECT @countparamlist = '@xCourseName nvarchar(250),
  69.         @xStudentName nvarchar(250),
  70.         @xFeeFrom decimal,
  71.         @xFeeTo decimal,
  72.         @xTotalDisplay int output' ;
  73.  
  74.     EXEC sp_executesql @countsql, @countparamlist,
  75.                         @CourseName,
  76.                         @StudentName,
  77.                         @FeeFrom,
  78.                         @FeeTo,
  79.                         @xTotalDisplay = @TotalDisplay output;
  80.  
  81.  
  82.     SELECT @paramlist = '@xCourseName nvarchar(250),
  83.         @xStudentName nvarchar(250),
  84.         @xFeeFrom decimal,
  85.         @xFeeTo decimal,
  86.         @xPageIndex int,
  87.         @xPageSize int';
  88.  
  89.     EXEC sp_executesql @SQL , @paramlist ,
  90.         @CourseName,
  91.         @StudentName,
  92.         @FeeFrom,
  93.         @FeeTo,
  94.         @PageIndex,
  95.         @PageSize;
  96.  
  97.     print @countsql;
  98.     print @SQL;
  99. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement