Solved

SQL column Headers to a TXT file

Posted on 2013-11-27
5
359 Views
Last Modified: 2013-12-16
hi All


I am using the following to take data from an output file and put into a .TXT file
Is it possible within the same procedure to output the first line as the column headers ? if So how do I do it please

thanks
Mike


CREATE Procedure Z_TEST_BCP_Text_File
(
@table varchar(100),
@FileName varchar(100)
)
as
If exists(Select * from information_Schema.tables where table_name='databaselog')
    Begin
        Declare @str varchar(1000)
        set @str='Exec Master..xp_Cmdshell ''bcp "Select * from '+db_name()+'..'+@table+'" queryout "'+@FileName+'" -c -t"|" -T '''
        Exec(@str)
    end
else
    Select 'The table '+@table+' does not exist in the database'


to execute I run the following command
EXEC Z_TEST_BCP_Text_File 'My_File_Name','C:\My_Output_File.txt'
0
Comment
Question by:MECR123
  • 3
  • 2
5 Comments
 
LVL 5

Accepted Solution

by:
Lawrence Barnes earned 400 total points
ID: 39681291
Two options I think.
1. Create a union query that joins the column names with the data and then BCP that out.  The data would have to be converted to character in the union.
2. BCP out the column names, BCP out the data into a temporary file, then join the two files. The benefit here is that you don't have to convert the data into char.

There's a conversation about this here:  http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=127655 and at the bottom there's a link to an Excel version.

LVBarnes
0
 

Author Comment

by:MECR123
ID: 39689522
hi

thanks for that - that is a good link

Just one further question please

I have concatenated the 2 files - Header file and Data file - however the data starts off on line 1 as well - is there a way to get this to start on line 2 so that
Line 1 = Headers - Line 2 onwards is the data

thanks
Mike
0
 
LVL 5

Expert Comment

by:Lawrence Barnes
ID: 39692281
Sounds like you need to switch the order of the concatenation of the two files.  

master..xp_cmdshell 'data.txt >> column headers.txt'
master..xp_cmdshell 'del data.txt'
0
 

Author Comment

by:MECR123
ID: 39698107
hi

thanks for that - but I that is not the issue

Line one contains the header and first line of data - the file is coming out as 'test sample 01' attachment
I would like it to come out as 'test sample 01 good' attachment


appreciate you help

thanks
Mike
TEST-SAMPLE-01.txt
TEST-SAMPLE-01-Good.txt
0
 
LVL 5

Expert Comment

by:Lawrence Barnes
ID: 39702488
I will look at this tonight...sorry about the delay.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now