Unix Technical Forum

Select-into with identity

This is a discussion on Select-into with identity within the SQL Server forums, part of the Microsoft SQL Server category; --> I have the following query that has been working so far. I modified it slightly to work from another ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 05:00 AM
php newbie
 
Posts: n/a
Default Select-into with identity

I have the following query that has been working so far. I modified
it slightly to work from another source table (new_products). Now,
SQL Server complains that the identity is inherited already:

"Cannot add identity column, using the SELECT INTO statement, to table
'dbo.my_products', which already has column 'id' that inherits the
identity property."

Any suggestions for a work aorund?

This is the query:
SELECT IDENTITY(INT,1,1) as seq_number, prod_number, prod_name
INTO my_products
FROM ( SELECT prod_number, prod_name
FROM new_products
WHERE ...
ORDER BY ...) sub_table
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 05:00 AM
David Portas
 
Posts: n/a
Default Re: Select-into with identity

Only one IDENTITY column is allowed per table and apparently your
New_Products table already has an IDENTITY column. In a SELECT INTO
statement you can avoid copying the IDENTITY property from a column by
turning it into an expression. For example, if the prod_number is
IDENTITY:

SELECT IDENTITY(INT,1,1) AS seq_number,
prod_number+0 AS prod_number,
prod_name
INTO my_products ...

Don't use ORDER BY in the INSERT. ORDER BY isn't permitted in a derived
table unless you use TOP. ORDER BY achieves nothing useful in an INSERT
statement anyway. The IDENTITY won't necessarily follow the same
sequence as the ORDER BY.

--
David Portas
SQL Server MVP
--

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:39 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com