Bulk import on SQL with Format File

Quick fire way to get new data into a SQL table using bulk load:


C:\>bcp thedatabase.dbo.thetable  format nul -t -n -f thetable.fmt -U  yourusername  -P yourpassword


USE thedatabase
GO
BULK INSERT thetable
 
   FROM 'C:\thetabelnewdata.dat' 
   WITH (FORMATFILE = 'C:\thetable.fmt');
GO

SELECT * FROM thetable

go

How does the .fmt file look? (http://msdn.microsoft.com/en-us/library/ms191516.aspx) Lots of different values, but here is one example

10.0
9
1 SQLCHAR 0 1 "\"" 0 first_double_quote SQL_Latin1_General_CP1_CI_AS
2 SQLCHAR 0 20 "\",\"" 1 ip_from ""
3 SQLCHAR 0 20 "\",\"" 2 ip_to ""
4 SQLCHAR 0 2 "\",\"" 3 country_code SQL_Latin1_General_CP1_CI_AS
5 SQLCHAR 0 64 "\",\"" 4 country_name SQL_Latin1_General_CP1_CI_AS
6 SQLCHAR 0 128 "\",\"" 5 region_name SQL_Latin1_General_CP1_CI_AS
7 SQLCHAR 0 128 "\",\"" 6 city_name SQL_Latin1_General_CP1_CI_AS
8 SQLCHAR 0 20 "\",\"" 7 latitude ""
9 SQLCHAR 0 20 "\"\r\n" 8 longitude ""


Which loads data for into this table

CREATE TABLE [ip2location_db5_newfile](
[ip_from] float NOT NULL,
[ip_to] float NOT NULL,
[country_code] varchar(2) NOT NULL,
[country_name] varchar(64) NOT NULL,
[region_name] varchar(128) NOT NULL,
[city_name] varchar(128) NOT NULL,
[latitude] float NOT NULL,
[longitude] float NOT NULL,
)
GO

(used for storing data purchased from http://www.ip2location.com/)




Comments

Popular posts from this blog

cf_sql_timestamp vs cf_sql_date vs getdate()

Global SQL Procedure, System Objects and sp_ms_marksystemobject

Ghost Records, Card Recon and PCI Compliance