pyram
pyram

Reputation: 935

Start inserting from the second column on bulk insert

I have a table with 4 columns. I'm trying to bulk insert from a .dat file to a table:

CREATE PROCEDURE insertIntoTable3
    @path nvarchar(500) 
AS 

declare @sql varchar(max)

select @sql='BULK INSERT Test1..Ao7 FROM ''';
select @sql=@sql+ @path ;
select @sql=@sql+''' with (FIELDTERMINATOR = ''|'' ,  ROWTERMINATOR = ''\n'' )';

exec(@sql)

The .dat file has data for 3 columns, like:

rr  qwerwq  1
vv  asdf    1

I don't want to insert in the first column. I want to insert to the second, third and fourth columns.

Is there a way to start inserting from the second column on in the table?

Upvotes: 1

Views: 3122

Answers (1)

Aaron Bertrand
Aaron Bertrand

Reputation: 280320

You can use a staging table, bulk insert there, and then insert only the columns you want into the destination.

Or you can use a format file to skip columns.

Upvotes: 2

Related Questions