Monday, March 15, 2010

Upload csv file in sql server using bcp

If you want to upload text files into sql database you must create an fmt file with file configuration. "fmt" file must have the same configuration with table you upload in.

create fmt file

exec master.dbo.xp_cmdshell 'bcp "DATABASE.dbo.TABLE" format null -f C:\FOLDER\FILE.fmt -c -T -U username -S ip
-e C:\FOLDER\err.txt'

where FILE.fmt reprezents the fmt file that you create
ERR.txt - error log file

The upload code is different int sql 2000 and 2005

--2005

select *
FROM OPENROWSET(BULK 'C:\FOLDER\FILE.csv',
FORMATFILE = 'c:\FOLDER\FILE.fmt') as tmp1


--2000

BULK INSERT ABR_FRAMEWORK.dbo.ABR_CRB_ERR_TRANSMISIE FROM 'c:\FILE\FILE.csv'
WITH (FORMATFILE = 'c:\FOLDER\FILE.fmt')

No comments:

Post a Comment