DMA checking : Order by specifies integer ordinal

sakuraime 2,341 Reputation points
2020-10-05T03:21:25.527+00:00

29997-orderby.jpg

Anyone can give any example of this situation ?
Actually I don't think it's a big problem . as I have seen so many codes are written like this

And also it said " A sort column can include an expression , but when the database is in SQL COMPATIBILITY 90 or higher , the expression cannot resolve to a constant"?

what does it means ?

SQL Server Other
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Olaf Helper 47,436 Reputation points
    2020-10-05T08:19:16.32+00:00

    Anyone can give any example of this situation ?

    Order by ordinal means to sort by column number, not by column name.

    select *
    from sys.objects
    order by 8
    

    Here the order is by column 8 = "create_date", as long as the column order don't change. Therefore you should always sort by column name, not by it's ordinal.


  2. CathyJi-MSFT 22,396 Reputation points Microsoft External Staff
    2020-10-05T08:22:58.267+00:00

    Hi @sakuraime ,

    > A sort column can include an expression , but when the database is in SQL COMPATIBILITY 90 or higher , the expression cannot resolve to a constant. what does it means ?

    Please check if this thread could help you.

    Best regards,
    Cathy Ji


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2020-10-05T22:02:54.807+00:00

    And also it said " A sort column can include an expression , but when the database is in SQL COMPATIBILITY 90 or higher , the expression cannot resolve to a constant"?

    >

    what does it means ?

    >

    What it says. This runs on SQL 2000

    SELECT * FROM Orders ORDER BY 'nisse'
    

    And, yes, that is a piece of nonsense, and it was a bug that it was accepted. In later versions, something like that yields an error.

    As for ORDER BY 1, 2, 3, I believe this is defined in the ANSI standard. I tend to use it myself for ad hoc queries, but it is not very good in production code because of the maintenance issue it may cause.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.