Sybase BCP - include Column header

6k views Asked by At

Sybase BCP exports nicely but only includes the data. Is there a way to include column names in the output?

4

There are 4 answers

0
kolchanov On

AFAIK It's a very difficult to include column names in the bcp output.

Try free sqsh isql replacement http://www.sqsh.org/ with pipe and redirect features. F.e.

   1> select * from sysobjects
   2> go 2>/dev/null >/tmp/objects.txt

I suppose you can achive necessary result.

0
Hotel On

I solved this problem not too long ago via a proc will loop through the tables columns, and concatenate them. I removed all the error checking and procedure wrapper from this example. this should give you the idea. I then BCP'd out of the below table into headers.txt, then BCP'd the results into detail.txt and used dos copy /b header.txt+detail.txt file.txt to combine the header and detail records...this wall all done in a batch script.

The table you will BCP

    create table dbo.header_record
    (
      headers_delimited varchar(5000)
    )

Then massage the below commands into a stored proc. use isql to call this proc before your BCP extracts.

 declare 
          @last_col int,
          @curr_col int,
          @header_conc varchar(5000),
          @table_name varchar(35),
          @delim  varchar(5),
          @delim_size int


  select 
    @header_conc = '',
    @table_name = 'dbo.detail_table',
    @delim = '~'

  set @delim_size = len(@delim)


  --
  --create column list table to hold our identity() columns so we can work through it
  --
  create local temporary table col_list
    (
      col_head int identity
      ,column_name varchar(50)
    ) on commit preserve rows

  --
  -- Delete existing rows in case columns have changed
  --
  delete from header_record


  --
  -- insert our column values in the order that they were created
  --
  insert into col_list (column_name)
  select 
    trim(column_name)
  from SYS.SYSCOLUMN --sybase IQ specific, you will need to adjust.
  where table_id+100000 = object_id(@table_name) --Sybase IQ 12.7 specific, 15.x will need to be changed.
  order by column_id asc

  --
  --select the biggest identity in the col_list table
  --
  select @last_col = max(col_head)
    from col_list

  --
  -- Start @ column 1
  --
  set @curr_col = 1

  --
  -- while our current columns are less than or equal to the column we need to
  -- process, continue else end
  --
  while (@curr_col <= @last_col)
  BEGIN

    select
      @header_conc = 
        @header_conc + @delim + column_name
        from col_list where col_head = @curr_col

     set @curr_col = @curr_col + 1   
  END

  --
  -- insert our final concatenated value into 1 field, ignore the first delimiter
  --
  insert into dbo.header_record
    select substring(@header_conc, @delim_size, len(@header_conc) )

  --
  -- Drop temp table
  --
  drop table col_list
0
podman On

I created a view with the first row being the column names unioned to the actual table.

create view bcp_view
as 'name' col1, 'age' col2, ....
union
select name, convert(varchar, age),.... from people

Just remember to convert any non-varchar columns.

0
aF. On

With bcp you can't get the table columns.

You can get it with a query like this:

select c.name from sysobjects o
inner join syscolumns c on o.id = c.id and o.name = tablename