Home  >  Article  >  Database  >  SqlServer 序号列的实现方法

SqlServer 序号列的实现方法

WBOY
WBOYOriginal
2016-06-07 17:58:181262browse

对于 SQL SERVER 2000 及更早的版本,需要使用一个自增列,结合临时表来实现。

对于 SQL SERVER 2000 及更早的版本,需要使用一个自增列,结合临时表来实现。
代码如下:
SELECT [AUTOID] = IDENTITY(int,1,1), * INTO #temp_table FROM 表名;


代码如下:
SELECT * FROM #temp_table;

从 SQL SERVER 2005 开始, SQL SERVER 提供了一个 ROW_NUMBER() 函数,大大简化了工作。

代码如下:
SELECT *,ROW_NUMBER() OVER (ORDER BY 排序字段) AS [AUTOID] FROM 表名;

序号字段的别名,可以在后续的条件中使用。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn