Question

I try to create new table in MS SQL server 2008 using SELECT INTO statement. I want every row of the new table has auto-generated ID of UNIQUEIDENTIFIER type as a first column.

Code sample without ID column:

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

Code template with ID (UNIQUEIDENTIFIER) column, I want to have as a result:

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

Desired output is:

==============================================================
| 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 |
==============================================================

Any idea? Except IDENTITY(INT,1,1) or ROW_NUMBER() which is not want i want.

Was it helpful?

Solution

Easy:

select newID() as ID, * 
into #temp
from YourTable
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top