【问题标题】:Sybase BCP - include Column headerSybase BCP - 包含列标题
【发布时间】:2011-06-28 01:50:02
【问题描述】:

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

【问题讨论】:

    标签: sql sap-ase sap-iq


    【解决方案1】:

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

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

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

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        【解决方案3】:

        我不久前通过 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
        

        【讨论】:

          【解决方案4】:

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

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

          请记住转换任何非 varchar 列。

          【讨论】:

            猜你喜欢
            • 2015-11-10
            • 1970-01-01
            • 1970-01-01
            • 2013-08-08
            • 2018-09-29
            • 2013-11-23
            • 2023-04-01
            • 1970-01-01
            • 2013-03-24
            相关资源
            最近更新 更多