|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Today @ 11:32 AM
Points: 94,
Visits: 564
|
|
Hi, I am having trouble inserting the multiple rows into a table with identity column. The error i get is...
An explicit value for the identity column in table can only be specified when a column list is used and identity insert is ON
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Yesterday @ 1:23 PM
Points: 154,
Visits: 1,228
|
|
| It always helps if you include your table and the code you're using so we can see the actual issue. However, it appears that you are inserting into a table that has an identity column and you're trying specify a value. By default identity columns are automatically updated and you can't manually set the value unless you set the IDENTITY_INSERT on for the table you're trying to update.
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Tuesday, September 18, 2012 7:54 PM
Points: 56,
Visits: 156
|
|
Do not include your identity column to you insert statement.
insert into myTable(identitycol, col1, col2, coln) values(1, 'value 1', 'value 2', 'value n') ===>WRONG!
insert into myTable(col1, col2, coln) values('value 1', 'value 2', 'value n') ===>RIGHT!
"Often speak with code not with word, A simple solution for a simple question"
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Tuesday, July 10, 2012 7:31 AM
Points: 219,
Visits: 87
|
|
Identity coloumn will be always auto gaenerated no need to insert the data for those column .
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Tuesday, January 15, 2013 11:11 AM
Points: 1,945,
Visits: 2,782
|
|
This is only one reason why good SQL programmers never use this proprietary non-relational "feature" in their code.
Back to foundation. SQL is a set-oriented language. When you do an INSERT you put in a set, all at once. That set can be zero or more rows.
But IDENTITY is a sequential count of insertion attempts. PHYSICAL insertion attempts. Not a LOGICAL concept at all. So how do you number these rows?
Please read a book on RDBMS; your mindset is still in magnetic tape files and not RDBMS yet.
Books in Celko Series for Morgan-Kaufmann Publishing Analytics and OLAP in SQL Data and Databases: Concepts in Practice Data, Measurements and Standards in SQL SQL for Smarties SQL Programming Style SQL Puzzles and Answers Thinking in Sets Trees and Hierarchies in SQL
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 11:48 AM
Points: 37,712,
Visits: 29,967
|
|
CELKO (1/5/2012) This is only one reason why good SQL programmers never use this proprietary non-relational "feature" in their code.
Blah, blah, blah.
Gail Shaw Microsoft Certified Master: SQL Server 2008, MVP SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
We walk in the dark places no others will enter We stand on the bridge and no one may pass
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, May 02, 2013 1:20 PM
Points: 1,235,
Visits: 5,389
|
|
CELKO (1/5/2012) But IDENTITY is a sequential count of insertion attempts. PHYSICAL insertion attempts. Not a LOGICAL concept at all. So how do you number these rows?
You're confusing what IDENTITY is with how it's generated. IDENTITY is a number that is unique for a given table. PERIOD. How it is generated is irrelevant. I don't know anyone that writes code that depends on how an identity column is generated.
Drew
J. Drew Allen Business Intelligence Analyst Philadelphia, PA
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Yesterday @ 7:18 PM
Points: 10,989,
Visits: 10,532
|
|
drew.allen (1/5/2012) IDENTITY is a number that is unique for a given table. PERIOD. Not without something to enforce that uniqueness it isn't. 
CREATE TABLE #T (id int IDENTITY(1,1) NOT NULL) INSERT #T DEFAULT VALUES SELECT * FROM #T DBCC CHECKIDENT(#T, RESEED, 0) INSERT #T DEFAULT VALUES SELECT * FROM #T SET IDENTITY_INSERT #T ON INSERT #T (id) VALUES (1) SET IDENTITY_INSERT #T OFF SELECT * FROM #T DROP TABLE #T
Paul White SQL Server MVP SQLblog.com @SQL_Kiwi
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: 2 days ago @ 1:31 AM
Points: 1,385,
Visits: 1,085
|
|
Man, kind of abrasive, no, Mr. Celko?! I thought that this is supposed to be a supportive forum. I can understand your frustrations, but we can direct people to do research without being so abrasive.
- Chris
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 11:48 AM
Points: 37,712,
Visits: 29,967
|
|
CGSJohnson (1/12/2012) Man, kind of abrasive, no, Mr. Celko?! I thought that this is supposed to be a supportive forum. I can understand your frustrations, but we can direct people to do research without being so abrasive.
Hell, that's positively warm and cuddly compared to some of Joe's comments. Joe holds the opinion that if he insults newcomers it'll motivate them to study and learn to do things properly. Personally I think it just drives them away from possible improvement and results in them not learning and not seeking out learning.
Joe's attitude is quite common on Oracle boards and forums, very rare on SQL Server ones.
Gail Shaw Microsoft Certified Master: SQL Server 2008, MVP SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
We walk in the dark places no others will enter We stand on the bridge and no one may pass
|
|
|
|