0

so I have simple function trying to get two fields from database. I'm trying to use order by for the results however I cannot use ORDER BY in return clause. It tells me

The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.

Is is it possible to use ORDER BY in RETURN statement? I would like to avoid using order by when executing the function.

CREATE FUNCTION goalsGames1 () RETURNS TABLE
AS RETURN(
    SELECT MAX(goals_scored) goals,
           no_games
    FROM Player
    GROUP BY no_games
    ORDER BY no_games DESC )
3
  • The correct place for the ORDER BY is in the SELECT from goalsGames1 Commented Mar 11, 2019 at 10:22
  • Sorry, I have edited the question. Does that mean that only place where can I use order by is in the SELECT from goalsGames1 ? Commented Mar 11, 2019 at 10:23
  • yes that is what it means. The statement that executes the query is the one that is receiving the rows, and therefor only that can order them Commented Mar 11, 2019 at 11:04

4 Answers 4

2

One trick to skip this error is using top as it is mentioned in the error message:

CREATE FUNCTION goalsGames1 () RETURNS TABLE
AS RETURN(
    SELECT Top 100 Percent MAX(goals_scored) goals,
           no_games
    FROM Player
    GROUP BY no_games
    ORDER BY no_games DESC )
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, this is exactly the thing I was searching for.
This does not solve the problem, it merely hides the syntax error. Read the documentation!
@LukášVáclavek, be aware that the ORDER BY may not be honored as @GordonLinoff suggested. You must specify the ORDER BY in the query using a function (or view) to guarantee ordering of results.
Especially Top 100 Percent is optimised out anyway and has no effect. Even for values of TOP that are not optimised out the correct solution is to do it in the select from the function if you want guaranteed results
2

I would like to avoid using order by when executing the function.

If you are using the function and want the results in a particular order, then you need to use ORDER BY.

This is quite clearly stated in the documentation:

The ORDER clause does not guarantee ordered results when a SELECT query is executed, unless ORDER BY is also specified in the query.

Comments

1

use order by intimes of selection your function not in times of creation

so use here in select * from goalsGames1 order by col

and your error tells you where order by is invalid

2 Comments

Thank you for the answer. Does this mean that I cannot use ORDER BY in function definition?
@LukášVáclavek yes you can not
1

You cannot order by inside a function, the idea is to order the resultset returned by the function.

select * 
from   dbo.goalsGames1() 
order by no_games

Even if you would order by inside the function, there is no guaranty that this ordering would be preserved when the resultset is returned. The executing query (select * from functionname) has to be responsible for setting the order, not the function or view.

Who ever receives the rows is the only one that can order them, so in this case, the select * from goalsGames1() is the receiver, and this query has to order the results.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.