Question : bulk insert - skip a row

I am successfully using the bulk insert tool to insert data from a comma delimited file into a database, using this command:

bulk insert bts.dbo.BTS_CDR_RAW
from 'D:\joeProd\bts_cdr\working\unzip\USE.BTS1082656348'
with (
FIELDTERMINATOR = ',',ROWTERMINATOR = '\n' , TABLOCK)


My question, is: what is the easiest way to "skip" the first column of the database I am inserting into. (I don't want to put anything in the first column of the databse which is the primary key.)

The one example row of the file would be: "one,two,three"
and my database consists of four columns: pk, firstrow,secondrow,thirdrow.

I think that inserting using a format file allows this, but is there an easier way? If not, how do I use a format file to skip the first row?

-broke

Answer : bulk insert - skip a row

You can also create a view that omits the column(s) and BULK INSERT into the view.

create table tbcp (col1 varchar(10) null, cola varchar(10) null, colb varchar(10) null)

create view v_tbcp as select cola, colb from tbcp

exec master..xp_cmdshell 'echo aaa,bbb>c:\temp\tbcp.csv'
exec master..xp_cmdshell 'echo ccc,ddd>>c:\temp\tbcp.csv'

BULK INSERT OD.dbo.v_tbcp FROM 'c:\temp\tbcp.csv'
with (FIELDTERMINATOR = ',',ROWTERMINATOR = '\n' , TABLOCK)

>Note: Bulk Insert through a view may result in base table default
 values being ignored for NULL columns in the data file.

select * from tbcp

col1       cola       colb      
---------- ---------- ----------
NULL       aaa        bbb
NULL       ccc        ddd

Random Solutions  
 
programming4us programming4us