I wrote few articles for BULK INSERT, BCP but in all of those articles I have imported data from flat file to SQL Server table. But now in this article I will show you how you can export data from SQL SERVER to flat file like TXT or CSV etc.
BTW, if you want to refer my past article for BULK INSERT and BCP, please follow below given link.
For Bulk Insert:
Now let us move to our core topic of the article.
I will create one table in SQL Server and insert some rows in it, which I will be transfer from SQL Server’s table to text file.
–create one table for demo
CREATE TABLE emps
INSERT INTO emps
SELECT ‘Ritesh’,‘MIS’,‘echem’ UNION ALL
SELECT ‘Bihag’, ‘MIS’, ‘CT’ UNION ALL
SELECT ‘Rajan’, ‘account’,‘Marwadi’ UNION ALL
SELECT ‘Alka’,‘account’,‘tata’ UNION ALL
SELECT ‘Pinal’,‘DBA’,‘sqlautho’ UNION ALL
Now let me open Command Prompt from where I can run BCP.EXE utility to export my data from SQL Server to Flat File.
Run following command in your Command Prompt (start typing command from BCP, not from “C:\>”)
C:\>BCP adventureworks.dbo.emps out c:\empTmp.txt –T –t, -r\n –c
That’s it, you are done. You will get one file name “empTmp.txt” in your “C” drive. For understanding argument of BCP command, please refer my previous article at:
Reference: Ritesh Shah