microsoft / microsoft/sql-server-samples

Provide a Built-in Function for Sequential Row Numbers in Query Output

Open
#1,379 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PowerShell
Stars
11.2k
Forks
9.1k
Avg merge
2d 7h
Merged PRs (30d)
14

Description

Description:

SQL Server should introduce a robust built-in function to generate a sequential row number for any query output, without requiring an explicit ORDER BY or window functions like ROW_NUMBER().

Currently, SQL Server users often rely on:

  1. ROW_NUMBER() OVER (ORDER BY (SELECT 1)) – which is not always stable due to execution plan optimizations and parallelism.
  2. Manually assigning row numbers in loops – which is inefficient for large datasets.
  3. SSMS automatically assigning row numbers in result sets – but this behavior is unavailable in T-SQL queries.

Proposed Solution:
Introduce a built-in, lightweight function like:

SELECT SEQ_NUMBER(), * FROM MyQuery;

Where SEQ_NUMBER() assigns a sequential number based on the final result set order, similar to how SSMS automatically numbers rows in its output.

Benefits:
✅ Works without needing an explicit ORDER BY.
✅ Eliminates reliance on ROW_NUMBER() OVER (ORDER BY (SELECT 1)), which can be unstable in parallel execution.
✅ Reduces unnecessary sorting overhead when users only need a sequence number.
✅ Aligns with SSMS behaviour, where query results are always displayed with row numbers.

This feature would greatly simplify reporting queries, temporary data processing, and procedural workflows where a sequence is needed without an ordering constraint.

Example Use Case:
SELECT SEQ_NUMBER(), CustomerName, OrderAmount
FROM Orders
WHERE OrderDate > '2024-01-01';

Expected Output:

SEQ_NUMBER() | CustomerName | OrderAmount

1 | John Doe | 500.00
2 | Jane Smith | 700.00
3 | Mike Ross | 300.00

Would love to hear thoughts from the SQL Server team and the community on this!

Jagdish M

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No files, tests, or entry points are named in the issue. First check whether this samples repository contains an applicable SQL Server sample or feature-request process; completion would require an implementation path for the proposed function, which the payload does not identify.

Written by the indexing model from the issue text.

Assessment

Tech stack
sql
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.