ozkhillscovington
asked on
BCP include column names
We are using BCP to query out from a table into a .csv file to another server. Is there a way to get it to pick up the column names with the query, or some type of -switch we should be using.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Actually, just thought of it.
If you bcp out of a view like this#
Create mybcp view as
select 'col1name',col2name', 'col3name'...
union
select cast(col1 as varchar), (col2 as varchar), (col3 as varchar)....
you'll get the literal strings on your output file
If you bcp out of a view like this#
Create mybcp view as
select 'col1name',col2name', 'col3name'...
union
select cast(col1 as varchar), (col2 as varchar), (col3 as varchar)....
you'll get the literal strings on your output file
if the order give you a problem (i.e. the strings appear in the middle of the results...
select c1,c2,c3
from
(select 1 i,'col1name' c1,col2name' c2, 'col3name' c3...
union
select 2 i,cast(col1 as varchar) c1, (col2 as varchar) c2, (col3 as varchar) c3...) t
order by t.1 asc
select c1,c2,c3
from
(select 1 i,'col1name' c1,col2name' c2, 'col3name' c3...
union
select 2 i,cast(col1 as varchar) c1, (col2 as varchar) c2, (col3 as varchar) c3...) t
order by t.1 asc
the second select has casts missing....
select t.c1,t.c2,t.c3
from
(select 1 i, 'col1name' c1, col2name' c2, 'col3name' c3...
union
select 2 i,cast(col1 as varchar) c1, cast(col2 as varchar) c2, cast(col3 as varchar) c3...) t
order by t.i asc
select t.c1,t.c2,t.c3
from
(select 1 i, 'col1name' c1, col2name' c2, 'col3name' c3...
union
select 2 i,cast(col1 as varchar) c1, cast(col2 as varchar) c2, cast(col3 as varchar) c3...) t
order by t.i asc
ASKER
Twoboats-- will that work with a global temp table? I am trying to adapt your code to fit my table, but having issues. My columns are:
Market, OrderType, AccountType, Phonenumber - in temp table ##wkcancel
Market, OrderType, AccountType, Phonenumber - in temp table ##wkcancel
ASKER
I found this link, which works if I drop into perm table. If I try this with temp table, it returns nulls.
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=150595&SiteID=1
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=150595&SiteID=1
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I've actually decided to use DTS, since it copies the headers. The link I noted before did work okay, but some of these reports have 45 columns and I really didn't want to tie myself to alot of coding in case the table structure changed.
But thanks for all your help. I'm awarding twoboats the points on this one for his diligent suggestions!!
But thanks for all your help. I'm awarding twoboats the points on this one for his diligent suggestions!!
ASKER
Sorry twoboats, I decided to do a split since everyone helped out! Thanks again.. EVERYONE!!
No problem.
Thank you to all, I use union instead. I like bcp because it run fast and I think it is more efferent. ;-)
Once again thank you
Once again thank you
However, you could use a linked server to the csv file (excel type) and do a select insert. Your .csv sheet could have column names that way.
bcp /? gives all the switches