2

I want to generate row numbers in the same order the data are added. The below query is working fine for SQL Server.

SELECT *,ROW_NUMBER() OVER (ORDER BY (SELECT 100)) AS SNO FROM TestTable

I need standard query to achieve the same scenario in Firebird. Can anyone suggest me about this?

11

2 Answers 2

0

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.

0

One way could be usage of ORDER BY RAND():

CREATE TABLE TestTable(i INT);
INSERT INTO TestTable(i) VALUES (10);
INSERT INTO TestTable(i) VALUES (20);
INSERT INTO TestTable(i) VALUES (30);

SELECT TestTable.*,ROW_NUMBER() OVER (ORDER BY RAND()) AS SNO 
FROM TestTable;

db<>fiddle demo - Firebird

db<>fiddle demo - SQL Server

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.