Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 199639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:02:27+00:00 2026-05-11T17:02:27+00:00

I want to be able to insert data from a table with an identity

  • 0

I want to be able to insert data from a table with an identity column into a temporary table in SQL Server 2005.

The TSQL looks something like:

-- Create empty temp table
SELECT *
INTO #Tmp_MyTable
FROM MyTable
WHERE 1=0
...
WHILE ...
BEGIN
    ...
    INSERT INTO #Tmp_MyTable
    SELECT TOP (@n) *
    FROM MyTable
    ...

END

The above code created #Tmp_Table with an identity column, and the insert subsequently fails with an error “An explicit value for the identity column in table ‘#Tmp_MyTable’ can only be specified when a column list is used and IDENTITY_INSERT is ON.”

Is there a way in TSQL to drop the identity property of the column in the temporary table without listing all the columns explicitly? I specifically want to use “SELECT *” so that the code will continue to work if new columns are added to MyTable.

I believe dropping and recreating the column will change its position, making it impossible to use SELECT *.

Update:

I’ve tried using IDENTITY_INSERT as suggested in one response. It’s not working – see the repro below. What am I doing wrong?

-- Create test table
CREATE TABLE [dbo].[TestTable](
    [ID] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) NULL,
 CONSTRAINT [PK_TestTable] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)
) 
GO
-- Insert some data
INSERT INTO TestTable
(Name)
SELECT 'One'
UNION ALL
SELECT 'Two'
UNION ALL
SELECT 'Three'
GO
-- Create empty temp table
SELECT *
INTO #Tmp
FROM TestTable
WHERE 1=0

SET IDENTITY_INSERT #Tmp ON -- I also tried OFF / ON
INSERT INTO #Tmp
SELECT TOP 1 * FROM TestTable

SET IDENTITY_INSERT #Tmp OFF 
GO
-- Drop test table
DROP TABLE [dbo].[TestTable]
GO

Note that the error message “An explicit value for the identity column in table ‘#TmpMyTable’ can only be specified when a column list is used and IDENTITY_INSERT is ON.” – I specifically don’t want to use a column list as explained above.

Update 2
Tried the suggestion from Mike but this gave the same error:

-- Create empty temp table
SELECT *
INTO #Tmp
FROM (SELECT
      m1.*
      FROM TestTable                 m1
          LEFT OUTER JOIN TestTable  m2 ON m1.ID=m2.ID
      WHERE 1=0
 ) dt

INSERT INTO #Tmp
SELECT TOP 1 * FROM TestTable

As for why I want to do this: MyTable is a staging table which can contain a large number of rows to be merged into another table. I want to process the rows from the staging table, insert/update my main table, and delete them from the staging table in a loop that processes N rows per transaction. I realize there are other ways to achieve this.

Update 3

I couldn’t get Mike’s solution to work, however it suggested the following solution which does work: prefix with a non-identity column and drop the identity column:

SELECT CAST(1 AS NUMERIC(18,0)) AS ID2, *
INTO #Tmp
FROM TestTable
WHERE 1=0
ALTER TABLE #Tmp DROP COLUMN ID

INSERT INTO #Tmp
SELECT TOP 1 * FROM TestTable

Mike’s suggestion to store only the keys in the temporary table is also a good one, though in this specific case there are reasons I prefer to have all columns in the temporary table.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:02:28+00:00Added an answer on May 11, 2026 at 5:02 pm

    IF you are just processing rows as you describe, wouldn’t it be better to just select the top N primary key values into a temp table like:

    CREATE TABLE #KeysToProcess
    (
         TempID    int  not null primary key identity(1,1)
        ,YourKey1  int  not null
        ,YourKey2  int  not null
    )
    
    INSERT INTO #KeysToProcess (YourKey1,YourKey2)
    SELECT TOP n YourKey1,YourKey2  FROM MyTable
    

    The keys should not change very often (I hope) but other columns can with no harm to doing it this way.

    get the @@ROWCOUNT of the insert and you can do a easy loop on TempID where it will be from 1 to @@ROWCOUNT

    and/or

    just join #KeysToProcess to your MyKeys table and be on your way, with no need to duplicate all the data.

    This runs fine on my SQL Server 2005, where MyTable.MyKey is an identity column.

    -- Create empty temp table
    SELECT *
    INTO #TmpMikeMike
    FROM (SELECT
          m1.*
          FROM MyTable                 m1
              LEFT OUTER JOIN MyTable  m2 ON m1.MyKey=m2.MyKey
          WHERE 1=0
     ) dt
    
    INSERT INTO #TmpMike
    SELECT TOP 1 * FROM MyTable
    
    SELECT * from #TmpMike
    

    EDIT
    THIS WORKS, with no errors…

    -- Create empty temp table
    SELECT *
    INTO #Tmp_MyTable
    FROM (SELECT
              m1.*
              FROM MyTable                 m1
                  LEFT OUTER JOIN MyTable  m2 ON m1.KeyValue=m2.KeyValue
              WHERE 1=0
         ) dt
    ...
    WHILE ...
    BEGIN
        ...
        INSERT INTO #Tmp_MyTable
        SELECT TOP (@n) *
        FROM MyTable
        ...
    
    END
    

    however, what is your real problem? Why do you need to loop while inserting “*” into this temp table? You may be able to shift strategy and come up with a much better algorithm overall.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 154k
  • Answers 154k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer global-set-key expects an interactive command. (lambda () (interactive) (forward-line 5))… May 12, 2026 at 10:29 am
  • Editorial Team
    Editorial Team added an answer No, it is not. The using keyword is used to… May 12, 2026 at 10:29 am
  • Editorial Team
    Editorial Team added an answer What you want is something based on the one reader… May 12, 2026 at 10:29 am

Related Questions

I have a somewhat ridiculous question regarding DDD, Repository Patterns and ORM. In this
I want to be able to read from an unsorted source text file (one
I have a log that is really huge. (millions of rows) LogTable ------- ID
I am using JPA with Hibernate underneath, and I am having trouble getting merge

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.