0

In SQL 2016 I had some functions that contained selects with order by [position] instead of [column name]. They stopped working in 2022. Now they return the error The ORDER BY position number 2 is out of range of the number of items in the select list. The interesting thing is that the Order by [position] is still supported in 2022, and that if I take the content of the function and run it as a script it works fine.

See sample below. Please, don't try to make sense of the content of the query. It is just a test and I used msdb tables that everybody has so you can try it yourself.

Create the following function

create FUNCTION [dbo].[TestingOrderBy]() returns INT
  as
  begin
    declare @VR int, @Order int

    select top 1
    @VR = [version_revision],
    @Order = case when L.[id] > 1000 then 1 else 99 end
   from msdb.[dbo].[msdb_version] L
    order by 2 asc, L.[version_major] desc

    return @VR
  end

When you execute it in SQL 2022 as shown below, you will receive an error. You will not receive an error if you execute it in SQL 2016. The error is The ORDER BY position number 2 is out of range of the number of items in the select list

select dbo.TestingOrderBy()

Now, if you simply execute the content of the function, as the following code does, it runs fine.

declare @VR int, @Order int

    select top 1
    @VR = [version_revision],
    @Order = case when L.[id] > 1000 then 1 else 99 end
   from msdb.[dbo].[msdb_version] L
    order by 2 asc, L.[version_major] desc

    select @VR

Whyyyy?????

  • in ssms with Sql serer 2022 this produces the same error, independ of function or not and it is correct, as you can only return scalar values with 1 dimension, such an order by makes no sense at all as you have only 1 row as result set anyway, it is consequent to remove this possibility – nbk Jul 18 '23 at 22:16
  • This errors in 2022 and 2019 as I would expect it to, in 2017 & 2016 it seems to not produce the error. Regardless, you should repeat the expression or move the expression to a cross apply [such as this](https://dbfiddle.uk/08aH3muh); assigning a variable that's not returned seems a bit silly though, really you only need [this](https://dbfiddle.uk/44BAdL6k) – Stu Jul 18 '23 at 22:23
  • @nbk, I have SQL Server 2022 and the last block of code does not produce any error. Also, the order by is necessary so the top 1 returns the record that I want. And the values of that record are the ones I am assigning to the variables. Stu, I know I can write this query differently, I wanted to understand why as script is fine and as a content of a function is not. – Cristina Jul 19 '23 at 16:23
  • M$ has programmed it out, use the case expression again , it will be like all other queries – nbk Jul 19 '23 at 17:29
  • Probably a bug in the parser, or an improvement, regardless, not much to do now. Anyways, your query is not correct anyway, because assigning multi-value to @variables in select is not guaranteed to work correctly, so you might as well fix it. – siggemannen Jul 30 '23 at 15:25

0 Answers0