Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 First Attempt With Identity_Insert

Author  Topic 

Jarid.Lawson
Starting Member

7 Posts

Posted - 2009-12-02 : 11:38:42
I am loading a flat file into a table for various updates (setting name fields into Last, First M format from Last First M format, etc.). Most of the time I do not have a distinct data field, so I have thought that Identiy_Insert should work for this. I have never used Identity_Insert before, so I am not sure where my logic is failing.

I load the data into DBO.RAW_RECORDS via Tasks>Import Data. From there I use the following script:

Create Table dbo.ALL_RECORDS
(
records_id Numeric(18,0) Identity(1,1),
monarch_accountno Varchar(250),
monarch_originalname Varchar(250)...
)

Set Identity_Insert DBO.ALL_RECORDS On

Insert Into DBO.ALL_RECORDS
(
monarch_accountno,
monarch_originalname...
)
Select monarch_accountno,
monarch_originalname...
From DBO.RAW_RECORDS

Set Identity_Insert DBO.ALL_RECORDS Off

When I run this script I receive the following error:
quote:
Explicit value must be specified for identity column in table 'ALL_RECORDS' either when IDENTITY_INSERT is set to ON or when a replication user is inserting into a NOT FOR REPLICATION identity column.


Any thoughts?

Jarid Lawson

"A little knowledge is dangerous. So is a lot." - Einstein

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-02 : 11:55:38
You've excluded records_id from your insert statement, hence the error. With identity insert enabled, you must provide an explicit value. If you do not want to provide an explicit value, then turn identity insert off and let SQL Server manage the column for you.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-02 : 11:56:34
Also, do not use numeric(18,0) for the data type of an identity column! Use either int or bigint.

It is one of my biggest pet peeves these days.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

Jarid.Lawson
Starting Member

7 Posts

Posted - 2009-12-02 : 12:06:33
I must be a little thick headed, because I am not getting part of your answer. You said I can either set an expilicit value (i.e. 1) during my insert statement, or I can leave identity insert off. If I used Insert Into DBO.ALL_RECORDS(records_id,column2...) Select(1,coulmn2...) I thought SQL will fail since it is looking at column names. Am I getting this wrong? Also, if I leave Identity_Insert off then are you saying since there is records_id Int Identity(1,1) that it will auto account for this without me manually telling it to auto sequence?

Sorry if I am over asking the same question, but I am still a little knew with some of these SQL features.

Jarid Lawson

"A little knowledge is dangerous. So is a lot." - Einstein
Go to Top of Page

Jarid.Lawson
Starting Member

7 Posts

Posted - 2009-12-02 : 12:13:56
Ok, I tried Insert Into DBO.ALL_RECORDS(records_id,column2...) Select(1,coulmn2...) with Identity_Insert On, and now all of the records are set to 1 in that field. I'm guessing that was not the way to do it correctly. I also tried Insert Into DBO.ALL_RECORDS(records_id,column2...) Select(records_id,coulmn2...) with Identity Insert off, but it failed saying there is no column called records_id on Raw_Records.

Jarid Lawson

"A little knowledge is dangerous. So is a lot." - Einstein
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2009-12-02 : 13:20:26
If you are going to supply the records_id field from your flat file, turn identity insert ON. This will insert the value for the record_id from your flat file.
(i.e. Insert into [Table] (record_id,column2, column3, ect)) select record_id,column2,column3,etc.)


If you want brand new, server managed IDs generated for your records, turn Identity insert OFF, and change your script to EXCLUDE the record_id column
(i.e. Insert into [Table] (column2, column3, ect)) select column2,column3,etc.)


http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

Jarid.Lawson
Starting Member

7 Posts

Posted - 2009-12-02 : 13:27:37
That worked perfectly!!! Thank you!!!

Jarid Lawson

"A little knowledge is dangerous. So is a lot." - Einstein
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-02 : 13:54:57
Sorry I wasn't exactly clear, glad that Don stepped in to assist.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -