0

表:

姓氏
-----------------
啊啊啊
bb
抄送
dddd

我想要的 SQL 代码输出(按方向选择):

行名族
-----------------
1个
2 bb
3毫升
4 天
4

2 回答 2

9

使用该ROW_NUMBER()函数,它是SQL Server中的Ranking Functions之一:

WITH CTE
(
   SELECT
     ROW_NUMBER() OVER(ORDER BY name) AS row, 
     name, 
     family
   FROM tablename
)
SELECT * FROM CTE;
于 2013-05-21T12:11:02.560 回答
2

试试这个——

询问:

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
于 2013-05-21T12:22:11.690 回答