Skip to main content

BCP to export result set into CSV

BCP is used in SQL server either exports data to external files or imports data from external files (.txt, .csv etc). You can execute the bcp utility form command prompt (cmd) and T-SQL, further you can schedule the job to do the bcp task on regular basis, if required.


See an example of BCP using T-SQL:

declare @sqlI2 varchar(8000)

declare @pathI2 varchar(100)

SET @pathI2='C:\Data'+Convert(Varchar(50),GetDate(),105) +'.csv'

select @sqlI2 = 'bcp "SELECT ''Id'' AS ID , ''Name'' AS Name,''Date'' AS Date UNION ALL SELECT CONVERT(Varchar(10),Id) AS ID, Name,CONVERT(Varchar(50),GetDate()) AS Date FROM TEST..Employee" queryout '+@pathI2+' -c -t, -U sa -P sa -S AKSSRV'

exec master..xp_cmdshell @sqlI2


Just set the SQL statement, location of output path, SQL server name, user id and password and execute the code. The output of file is copied to the specified location.

See an example of BCP using command prompt:


Open the command prompt (run -> cmd) and paste the following code:

bcp "SELECT CONVERT(Varchar(10),Id) AS ID,Name,GetDate() AS Date FROM TEST..Employee" queryout C:\Employee.txt -c -S WI125L3B5673\SHL -U sa -P sa


Change the required parameters and press enter the code will be executed.



List of bcp utility Parameters:

Bcp –h command is used to show the list of BCP parameter

C:\ >bcp -h

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"]

More info: http://msdn.microsoft.com/en-us/library/ms162802.aspx

Comments

Popular posts from this blog

Report Model Deployment Using Report Manager

Once you have design the report model and build the project this is the time to deploy on production environment. Similar to Report deployment if you will specify the Report Server and the folder name where your want to deploy the project and deploy; Visual studio environment deploy the project on the report server. But in case it is not possible then have to deploy the Report model through Report Manager Web application (using upload feature of web manager). Connect to the Target server -> Open Report Manager -> Locate the folder where you want to deploy the Report Model -> Click on Upload File Button of Report Manager Locate the Report Model (.smdl) file and click Ok. It will deploy the file on the server. Wait a minute are you getting a problem “ The DataSourceView is missing for the SemanticModel ” or something it just because of data source view is not available. The data source view and report model are the xml based file so you need to merge the both file (.smdl and .ds...

Using Stored Procedure in SSIS OLEDB Source Component

Error: A rowset based on the SQL command was not returned by the OLE DB provider. Cause: Usually it happened when the usp using multiple query calls in the procedure Resolution: Write the below given code at the top of sp in OLEDB source component of SSIS and the code would be something like given below:         SET NOCOUNT ON     SET ANSI_WARNINGS OFF     EXEC <usp_Name>