Sybase BCP - 包括列标题

时间:2011-02-16 02:52:05

标签: sql sybase-ase sybase-iq

Sybase BCP很好地导出但仅包含数据。有没有办法在输出中包含列名?

4 个答案:

答案 0 :(得分:0)

AFAIK在bcp输出中包含列名非常困难。

尝试使用管道和重定向功能免费使用sqsh isql replacement http://www.sqsh.org/。 F.e。

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

我想你可以获得必要的结果。

答案 1 :(得分:0)

使用bcp,您无法获取表格列。

你可以通过这样的查询得到它:

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

答案 2 :(得分:0)

我不久前解决了这个问题,通过proc将循环遍历表列,并将它们连接起来。我从此示例中删除了所有错误检查和过程包装器。这应该给你的想法。然后我将BCP从下表中导入headers.txt,然后将结果BCP到detail.txt并使用dos copy / b header.txt + detail.txt file.txt来组合标题和详细记录。这个墙都是用批处理脚本完成的。

您将BCP的表格

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

然后按下以下命令进入存储过程。使用isql在BCP提取之前调用此proc。

 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

答案 3 :(得分:-1)

我创建了一个视图,第一行是与实际表联合的列名。

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

请记住转换任何非varchar列。