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

未关闭
#1,379 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
20/100
Issue 类型
功能
描述清晰度
需要澄清
活跃度
停滞
技术栈
sql
领域
databases

调研方向

issue 中没有指定文件、测试或入口点。首先检查此 samples repository 是否包含适用的 SQL Server 示例或 feature-request 流程;要完成这项工作,需要为所提议的函数提供实现路径,而 payload 未对此进行说明。

由索引模型根据 Issue 内容生成。

描述

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

主要语言
PowerShell
星标
11.2k
派生
9.1k
平均合并
2 天 7 小时
30 天内合并 PR
14

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

microsoft/sql-server-samples 的其他 Issue

查看 microsoft/sql-server-samples 的全部 Issue

相似的 Issue

更多 Databases Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。