50

I need to select a bunch of data into a temp table to then do some secondary calculations; To help make it work more efficiently, I would like to have an IDENTITY column on that table. I know I could declare the table first with an identity, then insert the rest of the data into it, but is there a way to do it in 1 step?

1
  • 3
    IDENTITY columns make things consistent for multiple users. PRIMARY KEY's make things efficient. Use both for maximum effect.
    – Amy B
    Sep 23, 2008 at 20:28

7 Answers 7

95

Oh ye of little faith:

SELECT *, IDENTITY( int ) AS idcol
  INTO #newtable
  FROM oldtable

http://msdn.microsoft.com/en-us/library/aa933208(SQL.80).aspx

3
  • 1
    But of course, you can't have two. Just don't select the ident col as part of the query Sep 23, 2008 at 20:54
  • Need to be careful if using an ORDER BY clause and expecting the "idcol" to be in the same order. support.microsoft.com/kb/273586
    – mg1075
    Oct 4, 2011 at 18:28
  • select cast (CurrentID as int) as CurrentID you should like this if oldtable has an identity column
    – Savas Adar
    Feb 5, 2014 at 17:58
6

You commented: not working if oldtable has an identity column.

I think that's your answer. The #newtable gets an identity column from the oldtable automatically. Run the next statements:

create table oldtable (id int not null identity(1,1), v varchar(10) )

select * into #newtable from oldtable

use tempdb
GO
sp_help #newtable

It shows you that #newtable does have the identity column.

If you don't want the identity column, try this at creation of #newtable:

select id + 1 - 1 as nid, v, IDENTITY( int ) as id into #newtable
     from oldtable
4

Good Question & Matt's was a good answer. To expand on the syntax a little if the oldtable has an identity a user could run the following:

SELECT col1, col2, IDENTITY( int ) AS idcol
INTO #newtable
FROM oldtable

That would be if the oldtable was scripted something as such:

CREATE TABLE [dbo].[oldtable]
(
    [oldtableID] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
    [col1] [nvarchar](50) NULL,
    [col2] [numeric](18, 0) NULL,
)
2

If you want to include the column that is the current identity, you can still do that but you have to explicitly list the columns and cast the current identity to an int (assuming it is one now), like so:

select cast (CurrentID as int) as CurrentID, SomeOtherField, identity(int) as TempID 
into #temp
from myserver.dbo.mytable
0
1

To make things efficient, you need to do declare that one of the columns to be a primary key:

ALTER TABLE #mytable
ADD PRIMARY KEY(KeyColumn)

That won't take a variable for the column name.

Trust me, you are MUCH better off doing a: CREATE #myTable TABLE (or possibly a DECLARE TABLE @myTable) , which allows you to set IDENTITY and PRIMARY KEY directly.

3
  • It depends; inserts into an indexed table are (with few exceptions) slower than an unindexed table due to B+ tree maintenance; this app may not have sufficient data loaded into the temp table to make an index worthwhile. Totally depends on the situation, and we have insufficient information to know Sep 23, 2008 at 20:58
  • He wants an "identity" to "make things efficient". We can conclude that he'd like to access the items by an id that doesn't already exist. It's easy to sort by an id that doesn't already exist.
    – Amy B
    Sep 23, 2008 at 21:53
  • he hasn't defined efficient -- my thinking was that he wanted an identity not for speed, but for ease-of-use. But if it's a performance thing, just index one of the existing column Sep 23, 2008 at 23:19
0

If after the *, you alias the id column that is breaking the query a secondtime... and give it a new name... it magically starts working.

select IDENTITY( int ) as TempID, *, SectionID as Fix2IDs
into #TempSections
from Files_Sections
-4

IIRC, the INSERT INTO command uses the schema of the source table to create the temp table. That's part of the reason you can't just try to create a table with an additional column. Identity columns are internally tied to a SQL Server construct called a generator.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.