Question about bcp command line utility

Question about bcp command line utility

Post by Sinisa Zegara » Fri, 21 Jan 2000 04:00:00



I need to use bcp command line utility to export SQL Server 7.0 table
into the text file. It works OK, but in the output text file I don't
have header row with the names of columns. How can I get the header row
?
 
 
 

Question about bcp command line utility

Post by Umachandar Jayachandra » Sat, 22 Jan 2000 04:00:00


    There is no option as such, but you can use this trick (create  a view
or use the query directly):

-- To export data with a header row
-- (Note datatype for all columns should be varchar b'cos of the header)
use pubs
go
select cast(min(case ordinal_position when 1 then column_name end) as
varchar) as col1,
 cast(min(case ordinal_position when 2 then column_name end) as  varchar)
as col2,
 cast(min(case ordinal_position when 3 then column_name end) as  varchar)
as col3,
 cast(min(case ordinal_position when 4 then column_name end) as  varchar)
as col4,
 cast(min(case ordinal_position when 5 then column_name end) as  varchar) as
col5,
 cast(min(case ordinal_position when 6 then column_name end) as  varchar)
as col6,
 cast(min(case ordinal_position when 7 then column_name end) as  varchar)
as col7,
 cast(min(case ordinal_position when 8 then column_name end) as  varchar)
as col8,
 cast(min(case ordinal_position when 9 then column_name end) as  varchar)
as col9
 from information_schema.columns where table_name = 'authors'
union all
select au_id, au_lname, au_fname, phone, address, city, state, zip,
cast(contract  as varchar)
 from authors

--
Umachandar Jayachandran (UC)
SQL Server MVP, MCDBA, MCSE + Internet, MCP + Internet