You can't use row_number() over (order by (select 100))
with Firebird, because Firebird - as required by the SQL standard - requires a from
clause for a select
. The equivalent in Firebird would be row_number() over (order by (select 100 from rdb$database))
.
The best solution would be to use an actual column for the order by
to ensure a deterministic order.
When looking at the SQL:2016 standard, then an order by
is not required for row_number()
(but it is for rank()
and dense_rank()
). Unfortunately, it looks like Microsoft applied that requirement for row_number()
as well, possibly for uniformity with the rank-functions, and maybe because row_number()
without an order does not make a lot of sense. Using row_number() over ()
with SQL Server yields an error "The function 'row_number' must have an OVER clause with ORDER BY.", but works with Firebird.
SQL Server also enforces that the order by in a window function is not a numeric column reference. Using row_number() over (order by 1)
with SQL Server yields an error "Windowed functions, aggregates and NEXT VALUE FOR functions do not support integer indices as ORDER BY clause expressions.", but works with Firebird (although the 1
is taken as a literal 1, and not as column reference, contrary to an order by
on select level).
SQL Server also does not support using constants or literals in the order by in a window function. Using row_number() over (order by '1')
with SQL Server yields an error "Windowed functions, aggregates and NEXT VALUE FOR functions do not support constants as ORDER BY clause expressions.", but works with Firebird.
I did find a trick that worked for both Firebird 3 and SQL Server 2017, but it is a dirty hack:
row_number() over (order by current_user)
This works because SQL Server doesn't consider current_user
as a constant, but as a function, which means it doesn't fall under the 'no constants allowed'-rule.
Be aware that this trick may yield inconsistent row numbers (eg in Firebird multiple window functions evaluated with different constants will yield different values, and the window function is evaluated before an order by
on select level), and you may want to consider if you shouldn't simply track a row index in your application.
OVER(....)
clause. Or tryOVER( ORDER BY 1 )
firebirdsql.org/file/documentation/release_notes/html/en/3_0/…SELECT *,ROW_NUMBER() OVER () SNO FROM TestTable
- see firebird.1100200.n4.nabble.com/… - however the Window Functions that you use in YOUR example query were introduced in SQL 2003 AFAIR, not SQL 92