BCP: Execute a Stored Procedure and Extract Data to a .txt File with Pipe Delimited Format with a Pipe Delimited Header

I am creating a data extract. I am using BCP to execute a SQL Server Stored Procedure that produces a pipe delimited text file.

I am required to have pipe delimited headers. My question is how do I create a pipe delimited header in my pipe delimited text file? I've been reading that SSIS is a good option but unfortunately I do not have access to it so I am looking for another method.

Pipe Delimited Text File with Pipe Delimited Header
(Not exact SQL but should correctly convey the concept)

declare @sql varchar(8000)
select @sql = 'bcp "execute yourdatabase.dbo.usp_userdetails " queryout  c:\logs\bcpheadertest.txt -c -t"|" -T -S' +  @@servername exec master..xp_cmdshell @sql

create table userdetails
userid int identity(1,1),
login varchar(10),
fullname varchar(100),
address varchar(1000)

 insert into userdetails(login,fullname,address)
 values ('atest','a test','123 anywhere, aw 00000'),
             ('btest','b test','456 anywhere, aw 00000'),
           ('ctest','c test','789 anywhere, aw 00000')

create procedure dbo.usp_userdetails

select userid,login,fullname,address
from userdetails

Thanks in advance.

Rob M.Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
BCP does not support generating headers. You either

1) need to add them as content in your sproc as first line:

CREATE PROCEDURE dbo.usp_userdetails
        @ForExport BIT = 0

    IF ( @ForExport = 1 )
            SELECT   Q.*
            FROM     (   SELECT 0 AS Src ,
                                'userid' AS [userid] ,
                                'login' AS [login] ,
                                'fullname' AS fullname ,
                                'address' AS [address]
                         UNION ALL
                         SELECT 1 AS Src ,
                                U.userid ,
                                U.[login] ,
                                U.fullname ,
                         FROM   userdetails U ) Q
            ORDER BY Q.Src;
            SELECT U.userid ,
                   U.[login] ,
                   U.fullname ,
            FROM   userdetails U;

Open in new window

This is sometimes not advisable, cause it will involve implicit casts. Then you

2) need to add the header after the file creation via scripting. Either using batch or PowerShell. As batch:

@Echo Off


Set CSV_HEADER_FILE=C:\Temp\export.header.tmp
Set CSV_DATA_FILE=C:\Temp\export.data.tmp
Set CSV_FILE=C:\Temp\export.csv

Echo Header,goes,here> "%CSV_HEADER_FILE%"
SqlCmd -Q "EXECUTE database.schema.yoursproc;" -o "%CSV_DATA_FILE%" -s "," -W -u -h -1 -S YOURSERVER\INSTANCE
PowerShell -Command "Get-Content %CSV_HEADER_FILE%, %CSV_DATA_FILE% | Set-Content -Encoding UTF8 %CSV_FILE%"


Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rob M.Author Commented:

Thanks you very much. I am going to use the second option.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.