简体   繁体   English

启用排序和分页的 T-SQL 存储过程无法正常工作

[英]T-SQL stored procedure with sorting and paging enabled not working properly

I am using the following code:我正在使用以下代码:

ALTER PROCEDURE [dbo].[usp_get_all_groups] 
    -- Add the parameters for the stored procedure here
    @pStartIndex smallint,
    @pPageSize tinyint,
    @pOrderBy varchar
AS
BEGIN

 SELECT 
       *
       FROM
        (SELECT ROW_NUMBER() OVER (ORDER BY 
        
        CASE WHEN @pOrderBy='GroupId ASC' THEN UserGroups._id + ' ASC'
             WHEN @pOrderBy='GroupId DESC' THEN UserGroups._id + ' DESC'
             WHEN @pOrderBy='GroupCode ASC' THEN UserGroups.GroupCode + ' ASC'
             WHEN @pOrderBy='GroupCode DESC' THEN UserGroups.GroupCode + ' DESC'
        END        
        ) AS Row, * FROM UserGroups)
       AS StudentsWithRowNumbers
         WHERE Row>= @pStartIndex AND Row <= @pStartIndex + @pPageSize
END

When I am executing the stored proc using the following command当我使用以下命令执行存储过程时

DECLARE @return_value int

EXEC    @return_value = [dbo].[usp_get_all_groups]
        @pStartIndex = 0,
        @pPageSize = 15,
        @pOrderBy = N'GroupCode ASC'

SELECT  'Return Value' = @return_value

I am getting these results which is not sorted.我得到了这些未排序的结果。

Row _id GroupCode   Description Type    IsActive
1   1   CS2009  CS 2009 Batch   S   1
2   2   IT2009  IT 2009 Batch   S   1
3   3   ME2009  ME 2009 Batch   S   1
4   4   EC2009  EC 2009 Batch   S   1
5   5   EE2009  EE 2009 Batch   S   1
6   8   CS_F    CS Faculties    F   1
7   9   IT_F    IT Faculties    F   1
8   10  ME_F    ME Faculties    F   1
9   11  EC_F    EC Faculties    F   1
10  12  EE_F    EE Faculties    F   1
11  13  BSC_F   Basic Science Faculties F   1
12  14  Accounts    Accounts    A   1
13  15  Mgmt    Management  M   1
14  16  Lib Library B   1
15  17  TnP Training & Placement    T   1

Can you tell me what else is required?你能告诉我还需要什么吗?


I have tried this, but it is also giving plane unsorted result:我试过这个,但它也给出了平面未排序的结果:

SELECT 
        GroupTable._id,
        GroupTable.GroupCode,
        GroupTable.Type,
        GroupTable.Description
       FROM
        (SELECT ROW_NUMBER() OVER (ORDER BY 
        
        CASE WHEN @pOrderBy='GroupId ASC' THEN CONVERT(varchar(20), '_id ASC') 
             WHEN @pOrderBy='GroupId DESC' THEN CONVERT(varchar(20), '_id DESC') 
             WHEN @pOrderBy='GroupCode ASC' THEN CONVERT(varchar(20), @pOrderBy) 
             WHEN @pOrderBy='GroupCode DESC' THEN CONVERT(varchar(20), @pOrderBy) 
        END        
        ) AS Row, * FROM UserGroups)
       AS GroupTable
         WHERE Row>= @pStartIndex AND Row <= @pStartIndex + @pPageSize
       
       Select COUNT(*) as TotalRows from UserGroups where IsActive= 1

Replace your procedure with this:用这个替换你的程序:

ALTER PROCEDURE [dbo].[usp_get_all_groups] 
    -- Add the parameters for the stored procedure here
    @pStartIndex smallint,
    @pPageSize tinyint,
    @pOrderBy varchar(15)
AS
BEGIN

 SELECT *
 FROM
  (SELECT ROW_NUMBER() OVER (ORDER BY 
      CASE WHEN @pOrderBy='GroupId ASC' THEN UserGroups._id END ASC,  
      CASE WHEN @pOrderBy='GroupId DESC' THEN UserGroups._id END DESC,             
      CASE WHEN @pOrderBy='GroupCode ASC' THEN UserGroups.GroupCode END ASC,
      CASE WHEN @pOrderBy='GroupCode DESC' THEN UserGroups.GroupCode END DESC) AS Row, 
      * FROM UserGroups) AS StudentsWithRowNumbers
  WHERE Row>= @pStartIndex AND Row <= @pStartIndex + @pPageSize
  ORDER BY Row      
END

You can't dynamically assign asc and desc to a non-dynamic expression.您不能将 asc 和 desc 动态分配给非动态表达式。

There seems to be the misconception that the expression似乎有一种误解,即表达式

ORDER BY UserGroups._id + ' DESC'

will cause SQL Server to apply a descending order.将导致 SQL Server 应用降序。 What it really does is simply append the string literal " DESC" to the column value and then sort the result in ascending order.它真正做的只是将字符串文字“ DESC”附加到列值,然后按升序对结果进行排序。

You need to dynamically create the whole SELECT statement in the procedure, applying the ORDER BY twice as sketched in marc_s' answer, and execute the statement using sp_executesql.您需要在过程中动态创建整个 SELECT 语句,按照 marc_s 的答案中的草图应用 ORDER BY 两次,并使用 sp_executesql 执行该语句。 sp_executesql also allows you to pass the @ parameters. sp_executesql 还允许您传递@ 参数。

you are missing你失踪了

@pOrderBy varchar(20) because of this your @pOrderBy has only one char 'G'

also check this link may help you也检查这个链接可能对你有帮助

You're not ordering your SELECT statement .... neither the inner SELECT from UserGroups has an ORDER BY , nor does the outer SELECT .... you need to provide the ORDER BY on the SELECT , too!你没有订购你的SELECT语句...... UserGroups的内部SELECT也没有ORDER BY ,外部的SELECT ......你也需要在SELECT上提供ORDER BY (not just in your ROW_NUMBER() function's OVER() clause) (不仅仅是在您的ROW_NUMBER()函数的OVER()子句中)

SELECT 
    *
FROM
   (SELECT 
       ROW_NUMBER() OVER (ORDER BY 
        CASE WHEN @pOrderBy='GroupId ASC' THEN UserGroups._id + ' ASC'
             WHEN @pOrderBy='GroupId DESC' THEN UserGroups._id + ' DESC'
             WHEN @pOrderBy='GroupCode ASC' THEN UserGroups.GroupCode + ' ASC'
             WHEN @pOrderBy='GroupCode DESC' THEN UserGroups.GroupCode + ' DESC'
        END) AS Row, * 
    FROM UserGroups) AS StudentsWithRowNumbers
WHERE 
     Row >= @pStartIndex AND Row <= @pStartIndex + @pPageSize
ORDER BY
    CASE 
       WHEN @pOrderBy='GroupId ASC' THEN UserGroups._id + ' ASC'
       WHEN @pOrderBy='GroupId DESC' THEN UserGroups._id + ' DESC'
       WHEN @pOrderBy='GroupCode ASC' THEN UserGroups.GroupCode + ' ASC'
       WHEN @pOrderBy='GroupCode DESC' THEN UserGroups.GroupCode + ' DESC'
    END     

The ORDER BY inside the OVER() clause is only used to compute the Row values - it does not order the resulting data set.ORDER BYOVER()子句用于计算Row值-如果命令所得到的数据集。

Maybe this will help (if both _id and GroupCode are the same type):也许这会有所帮助(如果 _id 和 GroupCode 是同一类型):

DECLARE @pOrderBy VARCHAR(100),
        @pStartIndex smallint,
        @pPageSize tinyint
SET @pOrderBy='GroupId DESC'
SET @pStartIndex=0
SET @pPageSize=15
SELECT 
    GroupTable._id,
    GroupTable.GroupCode,
    GroupTable.Type,
    GroupTable.Description
FROM
(SELECT ROW_NUMBER() OVER (ORDER BY 
    CASE @pOrderBy
        WHEN 'GroupId ASC'
        THEN UserGroups._id
        WHEN 'GroupCode ASC'
        THEN UserGroups.GroupCode
    END ASC,
    CASE @pOrderBy
        WHEN 'GroupId DESC'
        THEN UserGroups._id
        WHEN 'GroupCode DESC'
        THEN UserGroups.GroupCode
    END DESC    
    ) AS Row, * FROM UserGroups)
AS GroupTable
WHERE 
    Row>= @pStartIndex 
    AND Row <= @pStartIndex + @pPageSize

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM