[英]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.