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

Rob M.
Rob M. used Ask the Experts™
on
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.

Output:
Pipe Delimited Text File with Pipe Delimited Header
TEST SQL:
(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
as
begin

select userid,login,fullname,address
from userdetails
end

Thanks in advance.

Robert
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Developer
Commented:
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
    )
AS
    SET NOCOUNT ON;

    IF ( @ForExport = 1 )
        BEGIN
            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 ,
                                U.[address]
                         FROM   userdetails U ) Q
            ORDER BY Q.Src;
        END;
    ELSE
        BEGIN
            SELECT U.userid ,
                   U.[login] ,
                   U.fullname ,
                   U.[address]
            FROM   userdetails U;
        END;

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

SetLocal

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%"
Del "%CSV_DATA_FILE%"
Del "%CSV_HEADER_FILE%"

EndLocal

Open in new window

Author

Commented:
ste5an,

Thanks you very much. I am going to use the second option.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial