views:

53

answers:

0

I have a stored proc that I use to create an xls file and then write data from queries to it. I can't take credit for this, here's the source http://www.simple-talk.com/sql/t-sql-programming/sql-server-excel-workbench/ . I can write varchar, txt, etc without issue. I can't seem to get any datetime fields to write and I have guessed at all the possible field types I can think of. Any ideas?

I have tried DateTime, Date, etc.

Here's the stored proc:

ALTER procedure [dbo].[spExecute_ADODB_SQL] 
@DDL varchar(2000),
@DataSource Varchar(100),
@Worksheet varchar(100)=null,
@ConnectionString varchar(255) 
=  'Provider=Microsoft.Jet.OLEDB.4.0;
 Data Source=%DataSource;
 Extended Properties=Excel 8.0'
as
Declare 
@objExcel int,
@hr int,
@command varchar(255),
@strErrorMessage varchar(255),
@objErrorObject int,
@objConnection int,
@bucket int

Select @ConnectionString
=replace (@ConnectionString, '%DataSource', @DataSource)
if @Worksheet is not null
Select @DDL=replace(@DDL,'%worksheet',@Worksheet)

Select @strErrorMessage='Making ADODB connection ',
  @objErrorObject=null
EXEC @hr=sp_OACreate 'ADODB.Connection', @objconnection OUT
if @hr=0 Select @strErrorMessage='Assigning ConnectionString property "' 
  + @ConnectionString + '"',
  @objErrorObject=@objconnection
if @hr=0 EXEC @hr=sp_OASetProperty @objconnection, 
  'ConnectionString', @ConnectionString
if @hr=0 Select @strErrorMessage
 ='Opening Connection to XLS, for file Create or Append'
if @hr=0 EXEC @hr=sp_OAMethod @objconnection, 'Open'
if @hr=0 Select @strErrorMessage
 ='Executing DDL "'+@DDL+'"'
if @hr=0 EXEC @hr=sp_OAMethod @objconnection, 'Execute',
  @Bucket out , @DDL
if @hr<>0
begin
Declare 
 @Source varchar(255),
 @Description Varchar(255),
 @Helpfile Varchar(255),
 @HelpID int

EXECUTE sp_OAGetErrorInfo  @objErrorObject, @source output,
 @Description output,@Helpfile output,@HelpID output
Select @strErrorMessage='Error whilst '
 +coalesce(@strErrorMessage,'doing something')+', '
 +coalesce(@Description,'')
raiserror (@strErrorMessage,16,1)
end
EXEC @hr=sp_OADestroy @objconnection

and here is the create statement:

spExecute_ADODB_SQL @DDL='Create table table_name
(column_name format, column_name format, column_name format)',
@DataSource ='C:\WorkBookName.xls'

it is in the code above where I seem to have the issue.

Thanks in advance.