mytable:
name family ----------------- a aa b bb c cc d dd
我想要的SQL代码输出(按方向选择):
Row name family ----------------- 1 a aa 2 b bb 3 c cc 4 d dd
答案 0 :(得分:8)
使用ROW_NUMBER()
函数,它是SQL Server中的Ranking Functions之一:
WITH CTE
(
SELECT
ROW_NUMBER() OVER(ORDER BY name) AS row,
name,
family
FROM tablename
)
SELECT * FROM CTE;
答案 1 :(得分:1)
试试这个 -
<强>查询:强>
DECLARE @temp TABLE
(
name VARCHAR(20)
, family VARCHAR(50)
)
INSERT INTO @temp (name, family)
VALUES
('a', 'aa'),
('b', 'bb'),
('c', 'cc'),
('d', 'dd')
SELECT
[Row] = ROW_NUMBER() OVER(ORDER BY (SELECT 1))
, name
, family
FROM @temp
<强>输出:强>
Row name family
----- ------- -------
1 a aa
2 b bb
3 c cc
4 d dd