INSERT INTO a temp table, and have an IDENTITY field created, without first declaring the temp table?

前端 未结 8 533
无人共我
无人共我 2021-02-02 06:16

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

8条回答
  •  温柔的废话
    2021-02-02 06:50

    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,
    )
    

提交回复
热议问题