2

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

4

4 回答 4

0

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

使用管道和重定向功能尝试免费的 sqsh isql 替换http://www.sqsh.org/ 。

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

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

于 2011-02-16T07:48:14.203 回答
0

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

你可以通过这样的查询来获取它:

select c.name from sysobjects o
inner join syscolumns c on o.id = c.id and o.name = tablename
于 2012-03-14T11:05:07.573 回答
0

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

您将 BCP 的表

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

然后将以下命令按摩到存储过程中。在 BCP 提取之前使用 isql 调用此过程。

 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
于 2012-08-09T16:53:22.273 回答
-1

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

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

请记住转换任何非 varchar 列。

于 2017-05-30T21:16:11.007 回答