SQL row_number()如何与条件一起使用

时间:2019-03-04 09:48:39

标签: sql sql-server stored-procedures

我通过提取行号对存储过程进行了分页。我正在尝试在我的select语句中添加一个条件。但这是行不通的。 我想在两个日期之间选择详细信息

存储过程

ALTER PROCEDURE [dbo].[Selectemployees] @page INT,
 @size INT,@startDate,@endDate

AS
BEGIN
    declare @totalrow int
    DECLARE @offset INT
    DECLARE @newsize INT
    DECLARE @sql NVARCHAR(MAX)

    IF(@page=0)
      BEGIN
        SET @offset = @page
        SET @newsize = @size
       END
    ELSE 
      BEGIN
        SET @page = @page - 1
        SET @offset = @page*@size 

        SET @newsize = @size-1
      END
    SET NOCOUNT ON
    SET @sql = '
     WITH OrderedSet AS
    (
      SELECT *, ROW_NUMBER() OVER (ORDER BY EmpID) AS ''Index''
      FROM employee  where DOJ between @startDate and @endDate
    )  SELECT * FROM OrderedSet WHERE [Index] BETWEEN ' +CONVERT(NVARCHAR(12), 
     @offset) + ' AND ' + CONVERT(NVARCHAR(12),
    (@offset + @newsize)) 
    EXECUTE (@sql) 
    SET @totalrow = (SELECT COUNT(*) FROM employee)
END

0 个答案:

没有答案