MS SQL SELECT INTO表自动为每一行生成唯一标识符

时间:2013-06-14 13:49:31

标签: sql-server uniqueidentifier auto-generate select-into

我尝试使用SELECT INTO语句在MS SQL server 2008中创建新表。我希望新表的每一行都有自动生成的UNIQUEIDENTIFIER类型ID作为第一列。

没有ID列的代码示例:

SELECT m.sellThru, m.turnover
INTO #tmp_table
FROM mpi m

具有ID(UNIQUEIDENTIFIER)列的代码模板,我希望得到结果:

SELECT ?? as id, m.sellThru, m.turnover
INTO #tmp_table
FROM mpi m

所需的输出是:

==============================================================
| id                                   | sellThru | turnover |
==============================================================
| 6818F861-6E87-490D-AF5B-CA9A8B56212E |   155.25 |   110.25 |
--------------------------------------------------------------
| 1A2E8ED6-76A3-42F9-8A87-27050A2ECDC1 |    64.79 |     4.36 |
--------------------------------------------------------------
| B0F9F127-56BB-4E87-AC33-626BA13F7065 |   213.89 |    45.89 |
==============================================================

有什么想法吗?除IDENTITY(INT,1,1)ROW_NUMBER()之外,我不想要。

1 个答案:

答案 0 :(得分:2)

简单:

select newID() as ID, * 
into #temp
from YourTable