22/12/2010

SSIS Data Load and geography data-type


When loading and saving data to a SQL Server table containing a geography field, execution results in an error (0xC0209029; DTS_E_PROCESSINPUTFAILED). It turns out that there's no built-in support in SSIS for spatial data.
After some googling I ended up with this post suggesting a workaround for the problem. In short the workaround is to first remove the spatial data column, then import your data, and finally recreate your spatial data column.
In SSIS this can be done as following:
1. In the control flow pane, add a 'Execute SQL Task' before and after the 'Data Flow Task'
ssis-geography
2. In the first task ('Remove spatial column') add a SQL statement to remove your column:
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'yourTableName' AND TABLE_SCHEMA = 'yourSchemaName' AND COLUMN_NAME = 'yourSpatialColumnName')
BEGIN
ALTER TABLE yourSchemaName. yourTableName DROP COLUMN yourSpatialColumnName
END
3. In the Data Load task, do what you need to do to load and save your data (without the spatial data column !)
4. In the last task add a SQL statement to add your spatial column again.
IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'yourTableName' AND TABLE_SCHEMA = 'yourSchemaName' AND COLUMN_NAME = 'yourSpatialColumnName')
BEGIN
ALTER TABLE yourSchemaName. yourTableName ADD yourSpatialColumnName geography NULL
END