Bulk Insert and Identity Column

Who is online?  0 guests and 0 members
Home  »  Forums   »  microsoft business intelligence   »  sql server   » Bulk Insert and Identity Column

Bulk Insert and Identity Column

Topic RSS Feed

Posts under the topic: Bulk Insert and Identity Column

Posted: 1/10/2012

Jedi Youngling 59  points  Jedi Youngling
  • Joined on: 3/23/2011
  • Posts: 26

Hi,

   I have a table with column name "ID" which the primary key of the table 

with identity specification on. I m populating this table through bulk insert 

from a csv file which does no have the id column value. what will be the syntax 

for bulk insert when id column value is not present in the csv file.

 

Thanks


Posted: 1/10/2012

Padawan 1392  points  Padawan
  • Joined on: 1/3/2010
  • Posts: 250

Are you using dataflow? If so, and since you do not have that column in csv source file, you just need to leave that column unmaped.

Take a look to Mike Davis's post.


Posted: 1/10/2012

Jedi Youngling 59  points  Jedi Youngling
  • Joined on: 3/23/2011
  • Posts: 26

No. I am doing it using SQL script. Not through SSIS. 

I want to knw if the bulk insert script script can be modified 

to handle this.


Posted: 1/10/2012

Padawan 1392  points  Padawan
  • Joined on: 1/3/2010
  • Posts: 250

Yes, you can.

The syntax is show below:

USE AdventureWorks2008R2;
GO
DELETE HumanResources.myDepartment;
GO
BULK INSERT HumanResources.myDepartment
   FROM 'C:\myDepartment-n.csv'
   WITH (
      KEEPIDENTITY,
      FORMATFILE='C:\myDepartment-f-n-x.Xml'
   );
GO
SELECT * FROM HumanResources.myDepartment

I'm affraid that without a formatfile you can't get it done. Take a look on how to create formatfile.

Are you familiar with bcp? I've used bcp for some time without problems.

Let us know if you did it. And if so, mark the post as answer.

Good Luck


Posted: 1/10/2012

Jedi Youngling 59  points  Jedi Youngling
  • Joined on: 3/23/2011
  • Posts: 26

Thanks for the info. Will go through it.. One more thing i wanted

to know is if this whole process of formatting the file and then loading 

the data to the table be automated?


Posted: 1/10/2012

Padawan 1392  points  Padawan
  • Joined on: 1/3/2010
  • Posts: 250
Answered  Answered

The problem is that your column file does not match the table column. Bulk Insert needs to know how to map columns even more if columns are in diferent order.

If you dont want a format file and want to turn this all process automatic, try to use a View. Yes, create a view without the identity column and BULK INSERT into that view.


Posted: 1/10/2012

Jedi Youngling 59  points  Jedi Youngling
  • Joined on: 3/23/2011
  • Posts: 26

Creating a view and inserting data into it works fine for me.

Thanks Smile


Posted: 1/10/2012

Padawan 1392  points  Padawan
  • Joined on: 1/3/2010
  • Posts: 250

Glad to hear that. In fact turns things easir than maintain a format file.


Page 1 of 1 (8 items)