SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


insert headers into sql query results


insert headers into sql query results

Author
Message
mbhout22
mbhout22
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 29
Hi ,

I am new to TSql and need to add dummy headers to my sql result before i export it to a csv format.

i can have a header which can be a noddy as '1' or 'header' as the macro that need to read my result start reading data from the 3rd row

Many thanks,

Christian
dbo.benyos
dbo.benyos
SSC Veteran
SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)

Group: General Forum Members
Points: 214 Visits: 1019
If that's the way your tool reds CSV I suggest you find fome other tool, but as per your question, you may append dummy lines in the following way: Suppose you have a list of names and their phone numbers, returned by this fictious query:

Select PersonName, Phone From MyPhoneBook



You could add your 2 dummy lines in the following way:

Select '' AS PersonName, '' AS Phone
UNION ALL
Select '', ''
UNION ALL
Select PersonName, Phone From MyPhoneBook



Tal Ben Yosef
www.TalBenYosef.com
(visit my LinkedIn profile)
mbhout22
mbhout22
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 29
Thanks for you response, this will work for my initial question. Unfortunatly i have about 70 columns, is there a short way of doinf this without clobbering the sql codes with empty strings?

i know that its can be done in excel but was wondering if there is a sql statement that does it inside sql server
dbo.benyos
dbo.benyos
SSC Veteran
SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)

Group: General Forum Members
Points: 214 Visits: 1019
It really depends on many parameters, but as a rule of thumb - check if you need to apply this fix in many views or procedures and if all solutions are the same. If so, you might wanna forget about touching the SQL code and opting for external solutions that wil simply add those 2 empty lines in the beginning of your file.

Some of the available tools for that could be:
1. CMD --> type 2 empty lines and then your original files into a new CSV file (however, you might need the commas. I don't know which tool are you using to read your CSV files)

2. LOGPARSER --> You can use Microsoft's LogParser to do the same thing you wanted SQL to

3. Use CSCRIPT (JS / VBSCRIPT) to append replicate the first two lines

etc...

Tal Ben Yosef
www.TalBenYosef.com
(visit my LinkedIn profile)
mbhout22
mbhout22
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 29
Thanks Ben,

I am using SSIS, i can use vbcrlf cmd in access but we are trying to move away from that.

thanks,

Chris
dbo.benyos
dbo.benyos
SSC Veteran
SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)

Group: General Forum Members
Points: 214 Visits: 1019
Although I owe much of my com[puter life to mictosoft Access, I think it's a smart decision to move away from that.

You're either gonna deal with the CSV producer or the consumer....

One more choice came to my mind: Unix SED command. It has a Win32 port. It's really powerful!

Sed man page

Tal Ben Yosef
www.TalBenYosef.com
(visit my LinkedIn profile)
mbhout22
mbhout22
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 29
Thanks Ben,

i guess for the time being i will stick with access before experiemnting with other products.

Thank you for your contribution

C
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search