Tuesday, March 10, 2009

BCP function

You can use this BCP function easily to import/export from the MS SQL database.

usage: bcp {dbtable | query} {in | out | queryout | format} datafile
[-m maxerrors] [-f formatfile] [-e errfile]
[-F firstrow] [-L lastrow] [-b batchsize]
[-n native type] [-c character type] [-w wide character type]
[-N keep non-text native] [-V file format version] [-q quoted identifier]
[-C code page specifier] [-t field terminator] [-r row terminator]
[-i inputfile] [-o outfile] [-a packetsize]
[-S server name] [-U username] [-P password]
[-T trusted connection] [-v version] [-R regional enable]
[-k keep null values] [-E keep identity values]
[-h "load hints"]

Here's an example to export the data from the database.
- with trusted connection
bcp "dbname.dbo.tablename" out "dev_tablename.bcp" -e error.log -c -S servername -T -k

- with username and password
bcp "dbname.dbo.tablename" out "dev_tablename.bcp" -e error.log -c -S servername -U username -P password -k


Here's an example to import the data to the database.
- with trusted connection
bcp "dbname.dbo.tablename" in "dev_tablename.bcp" -e error.log -c -S servername -T -k

- with username and password
bcp "dbname.dbo.tablename" in "dev_tablename.bcp" -e error.log -c -S servername -U username -P password -k

- with username and password and order clause
bcp "dbname.dbo.tablename" in "dev_tablename.bcp" -e error.log -c -S servername -U username -P password -k -h "ORDER (TheDate DESC)"

No comments: