Tuesday 15 April 2014

BCP Command



      Using Bulk Copy Program (BCP) Utility, quickly we import and export large amounts of data. We can run the BCP command from the command prompt.

      Here i am going to explain, how to import data from a flat file to a data base table.

Source File:
File Path: D:\Programs\Product.txt


Target table:
ServerName      : KiranY
UserName        : sa
Password          : 123
DataBaseName : Sample

Table Name: PRODUCT

Table Creation Script:
          First we need to load source data into staging table. The stg table column data type should be character type. While the time of target table load (from stg table) we can convert to required data types.

                        CREATE TABLE STG_PRODUCT
                        (
                        PID VARCHAR(50)
                        ,PNAME VARCHAR(50)
                        ,PRICE VARCHAR(50)
                        )

BCP Command:
Syntax:
bcp <datbasename>..<tablename> in <pathname of the file with extension> -S<servername> -U<username> -P<password> -C -t"<column delimiter>" -r<row delimiter>

Command:
 bcp SAMPLE..STG_PRODUCT in D:\Programs\Product.txt -SKIRANY -Usa -P123 -c -t"," -r\n





Output: