簡體   English   中英

Sybase BCP-包含列標題

[英]Sybase BCP - include Column header

Sybase BCP可以很好地導出,但只包含數據。 有沒有辦法在輸出中包括列名?

我不久前通過proc解決了這個問題,它將遍歷表的各個列,並將它們連接起來。 我從該示例中刪除了所有錯誤檢查和過程包裝器。 這應該給你的想法。 然后,我將BCP從下表中移出到headers.txt中,然后將BCP將結果放入detail.txt中,並使用dos copy / b header.txt + detail.txt file.txt組合標題和詳細記錄。 .wall全部以批處理腳本完成。

您將在BCP中使用的表格

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

然后將以下命令放入存儲的進程中。 在BCP提取之前,請使用isql調用此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

AFAIK在bcp輸出中包含列名非常困難。

使用管道和重定向功能嘗試免費的sqsh isql替換http://www.sqsh.org/

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

我想您可以取得必要的結果。

使用bcp,您無法獲取表列。

您可以通過以下查詢獲取它:

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

我創建了一個視圖,第一行是與實際表結合的列名。

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

只要記住要轉換任何非varchar列即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM