pensio
2008-10-15 16:23:02 UTC
I'm using bcp command to export a sql server table to an excel sheet. I've
created a bcp format file to use with the command. It looks like this :
9.0
5
1 SQLCHAR 0 20 "\t" 1 ArticleNumber
SQL_Latin1_General_CP1_CI_AS
2 SQLCHAR 0 100 "\t" 2 Description
SQL_Latin1_General_CP1_CI_AS
3 SQLCHAR 0 6 "\t" 3 InvoiceNumber
SQL_Latin1_General_CP1_CI_AS
4 SQLCHAR 0 19 "\t" 4 SalesPrice
SQL_Latin1_General_CP1_CI_AS
5 SQLCHAR 0 19 "\r" 5 NumberDelivered
SQL_Latin1_General_CP1_CI_AS
to execute the command in T-sql :
declare @sql varchar(500)
select @sql = 'bcp dataBaseX.dbo.FileToExport out c:\ExcelSheet.xls -f
c:\bcp.fmt -T'
exec master..xp_cmdshell @sql
The export executes without errors. Problem is that ArticleNumber field in
the sql table is a varchar field that most of the time contains only numbers
and often starts with a zero.
If I open the excel sheet, then the leading zeros are suppressed.
The fields in the first record of the sql table contain the columnheaders
('ArticleNumber', 'Description' etc).
I hoped to mislead excel this way to see aspecially the ArticleNumber field
as a text field, but to no avail.
It is important that I see the leading zeros in the excel sheet when opening
the document, without first having to change the properties of the column.
How do I solve this ?
versions : SQL Server 2005, Office 2000
Thanks for any advice.
created a bcp format file to use with the command. It looks like this :
9.0
5
1 SQLCHAR 0 20 "\t" 1 ArticleNumber
SQL_Latin1_General_CP1_CI_AS
2 SQLCHAR 0 100 "\t" 2 Description
SQL_Latin1_General_CP1_CI_AS
3 SQLCHAR 0 6 "\t" 3 InvoiceNumber
SQL_Latin1_General_CP1_CI_AS
4 SQLCHAR 0 19 "\t" 4 SalesPrice
SQL_Latin1_General_CP1_CI_AS
5 SQLCHAR 0 19 "\r" 5 NumberDelivered
SQL_Latin1_General_CP1_CI_AS
to execute the command in T-sql :
declare @sql varchar(500)
select @sql = 'bcp dataBaseX.dbo.FileToExport out c:\ExcelSheet.xls -f
c:\bcp.fmt -T'
exec master..xp_cmdshell @sql
The export executes without errors. Problem is that ArticleNumber field in
the sql table is a varchar field that most of the time contains only numbers
and often starts with a zero.
If I open the excel sheet, then the leading zeros are suppressed.
The fields in the first record of the sql table contain the columnheaders
('ArticleNumber', 'Description' etc).
I hoped to mislead excel this way to see aspecially the ArticleNumber field
as a text field, but to no avail.
It is important that I see the leading zeros in the excel sheet when opening
the document, without first having to change the properties of the column.
How do I solve this ?
versions : SQL Server 2005, Office 2000
Thanks for any advice.