all groups > sql server programming > november 2004 >
You're in the

sql server programming

group:

Problem with INSERT INTO Temp Table that has IDENTITY Column


Problem with INSERT INTO Temp Table that has IDENTITY Column Guadala Harry
11/1/2004 10:24:02 PM
sql server programming:
You can duplicate this on your machine in about two seconds: To do so, fire
up QA and paste the following code and run it. As is, it will choke. Then
comment out the ORDER BY line and run it again and it does not choke.

USE PUBS
CREATE TABLE #TEMP_TABLE
(
RowNumber [int] IDENTITY,
City varchar(40),
State varchar(40)
)

INSERT INTO #TEMP_TABLE
SELECT city, state
FROM stores
ORDER BY stor_id -- Comment this ORDER BY line out and it works.

SELECT * FROM #TEMP_TABLE

DROP TABLE #TEMP_TABLE

I need to populate a temp table using a SELECT statement that has an ORDER
BY Clause because (1) the data needs to appear in a specific order (as
specified by the ORDER BY clause in the populating SELECT), and (2) a
subsequent operation needs to find a series of ranges of arbitrary lengths
within the set using an integer column that can be assumed to increment
through the range (thus the need for IDENTITY).

The problem is that the temp table has an IDENTITY column - but when a temp
table both (1) has an IDENTITY property and (2) is being populated by a
SELECT statement that has ORDER BY, I get the following error:
[An explicit value for the identity column in table '#TEMP_TABLE' can only
be specified when a column list is used and IDENTITY_INSERT is ON.]

What can I do to get around it?

Thanks!

-GH

Re: Problem with INSERT INTO Temp Table that has IDENTITY Column Guadala Harry
11/1/2004 11:26:09 PM
Bingo! Thanks!




[quoted text, click to view]

Re: Problem with INSERT INTO Temp Table that has IDENTITY Column Uri Dimant
11/2/2004 11:21:27 AM
Hi
This will not be guaranteed that rows will ne inserting in specific ORDER.
Read about table definition in the BOL



[quoted text, click to view]

Re: Problem with INSERT INTO Temp Table that has IDENTITY Column avnrao
11/2/2004 12:41:20 PM
use this..

INSERT INTO #TEMP_TABLE(city,state)
SELECT city, state
FROM stores
ORDER BY stor_id

Av.
http://avdotnet.rediffblogs.com
http://www28.brinkster.com/avdotnet

[quoted text, click to view]

AddThis Social Bookmark Button