我想打印数据库中所有表的名称,但行号如下:
S.No. Table Name
-------------------
1. Products
2. Orders
3. OrderDetails
...
我尝试了以下操作,但这使我可以得到总行数。
-- USE [MyDatabase]; -- implied
SELECT @@ROWCOUNT AS [S.No.],
Name as [Table Name]
FROM Sys.Tables;
我认为我必须做类似的事情:
@Counter int;
SELECT (SELECT ++@Counter AS [S.No.]),
Name AS [Table Name]
FROM Sys.Tables;
但是我不太确定确切的语法。你能帮我吗?
我正在使用Microsoft SQL Server 2014。
答案 0 :(得分:3)
使用row_number()
:
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) as [S.No.],
Name as [Table Name]
FROM Sys.Tables;