Solved

SQL column Headers to a TXT file

Posted on 2013-11-27
5
364 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

911 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

16 Experts available now in Live!

Get 1:1 Help Now