[英]Sybase BCP - include Column header
Sybase BCP exports nicely but only includes the data. Sybase BCP可以很好地导出,但只包含数据。 Is there a way to include column names in the output? 有没有办法在输出中包括列名?
I solved this problem not too long ago via a proc will loop through the tables columns, and concatenate them. 我不久前通过proc解决了这个问题,它将遍历表的各个列,并将它们连接起来。 I removed all the error checking and procedure wrapper from this example. 我从该示例中删除了所有错误检查和过程包装器。 this should give you the idea. 这应该给你的想法。 I then BCP'd out of the below table into headers.txt, then BCP'd the results into detail.txt and used dos copy /b header.txt+detail.txt file.txt to combine the header and detail records...this wall all done in a batch script. 然后,我将BCP从下表中移出到headers.txt中,然后将BCP将结果放入detail.txt中,并使用dos copy / b header.txt + detail.txt file.txt组合标题和详细记录。 .wall全部以批处理脚本完成。
The table you will BCP 您将在BCP中使用的表格
create table dbo.header_record
(
headers_delimited varchar(5000)
)
Then massage the below commands into a stored proc. 然后将以下命令放入存储的进程中。 use isql to call this proc before your BCP extracts. 在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 It's a very difficult to include column names in the bcp output. AFAIK在bcp输出中包含列名非常困难。
Try free sqsh isql replacement http://www.sqsh.org/ with pipe and redirect features. 使用管道和重定向功能尝试免费的sqsh isql替换http://www.sqsh.org/ 。 Fe 铁
1> select * from sysobjects
2> go 2>/dev/null >/tmp/objects.txt
I suppose you can achive necessary result. 我想您可以取得必要的结果。
With bcp you can't get the table columns. 使用bcp,您无法获取表列。
You can get it with a query like this: 您可以通过以下查询获取它:
select c.name from sysobjects o
inner join syscolumns c on o.id = c.id and o.name = tablename
I created a view with the first row being the column names unioned to the actual table. 我创建了一个视图,第一行是与实际表结合的列名。
create view bcp_view
as 'name' col1, 'age' col2, ....
union
select name, convert(varchar, age),.... from people
Just remember to convert any non-varchar columns. 只要记住要转换任何非varchar列即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.