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

前端 未结 8 543
无人共我
无人共我 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 07:03

    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
    

提交回复
热议问题