Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

set identity_insert not works for insert statement on SQL2..

 
   Database Forums (Home) -> Programming RSS
Next:  How to play a flash video in an html page using P..  
Author Message
thomas

External


Since: Jul 20, 2010
Posts: 1



(Msg. 1) Posted: Tue Jul 20, 2010 12:25 am
Post subject: set identity_insert not works for insert statement on SQL2008
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Dear Experts,

I skip all columns name and try a statement "insert into table1 select *
from table2 where ...." but always prompt
cannot insert IDENTITY_INSERT is on

Table1 is the same column structure as table2 (actually I created table2
from running: select * into table2 from table1)

I tried both
SET IDENTITY_INSERT ON , or
SET IDENTITY_INSERT OFF
before insert statement , but also not works.

Could anyone know how to skip typing all columns name when insert from
another table ?

Thanks.

 >> Stay informed about: set identity_insert not works for insert statement on SQL2.. 
Back to top
Login to vote
Erland Sommarskog2

External


Since: May 30, 2004
Posts: 1649



(Msg. 2) Posted: Tue Jul 20, 2010 4:25 am
Post subject: Re: set identity_insert not works for insert statement on SQL2008 [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

thomas (thomas@mail.com) writes:
> I skip all columns name and try a statement "insert into table1 select *
> from table2 where ...." but always prompt
> cannot insert IDENTITY_INSERT is on
>
> Table1 is the same column structure as table2 (actually I created table2
> from running: select * into table2 from table1)
>
> I tried both
> SET IDENTITY_INSERT ON , or
> SET IDENTITY_INSERT OFF
> before insert statement , but also not works.
>
> Could anyone know how to skip typing all columns name when insert from
> another table ?

The error message says:

An explicit value for the identity column in table 'X' can only be
specified when a column list is used and IDENTITY_INSERT is ON.

That is you need to say:

INSERT table1 (a, b, c, ...)
SELECT a, b, c, ...
FROM table2

Overall, INSERT without column lists in production code is considered
bad practice, as is SELECT *.

One way to skip the actual typing, is to find the table in the Object
Explorer and drag the Columns node into the query window.

--
Erland Sommarskog, SQL Server MVP, esquel RemoveThis @sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

 >> Stay informed about: set identity_insert not works for insert statement on SQL2.. 
Back to top
Login to vote
Display posts from previous:   
   Database Forums (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]