views:

24

answers:

1

I am writing a small utility to copy a database from a proprietary ODBC database into a SQL Server database. Everything is working great, except when I use SQLBulkCopy to copy over the data. It works in most cases, but not when it's trying to copy a TIME field into a SQL DATETIME. I get this error:

The given value of type TimeSpan from the data source cannot be converted to type datetime of the specified target column.

When I create the schema in SQL Server I have to make the DATE and TIME fields DATETIME types in SQL Server, so there is no way around this. I wanted to use SQLBulkCopy so I didn't have to manually read through every record in every table and wrap logic around the special cases. Before I go down that road, is there another way I can do this? I have no control at all on the source database schema or content.

A: 

I assume you are dealing with pre-SQL Server 2008. SQL Server 2008 has DATE and TIME data types.

I think you would have to use a DataTable which matched the SQL Server schema and load this from your source reader, appropriately changing any TIME to a DATETIME by adding date information (e.g. 1/1/1900). Then use WriteToServer(DataTable). You might want to do it in batches, since you may use a bunch of memory reading it all into a DataTable.

Any particular reason you can't use SSIS?

Cade Roux
That pretty much confirms what I thought. I was hoping for a magic wand, but I guess not. Yes, it's SQL Server 2000. I can't use SSIS because the ODBC driver doesn't support remote connectivity and the ODBC database is on a different machine on the network, unless I can use SSIS in reverse from a non-SQL Server machine to read in the ODBC and export to a remote SQL Server.
John Virgolino
SSIS runs in an external process (SQL Server 2005 and up). You can read from just about anything and go to just about anything. And you can put script tasks and transformations to change data types and everything. Add SSIS can manage the buffers (and you can tune them for performance). It ships with SQL Server Standard Edition, but it isn't really part of the engine.
Cade Roux