SQL Server – How to Import Shapefile Using Only SQL Scripts

shapefilesql server

I am trying to import some shapefiles into SQL Server 2008 R2. I found this useful tool called Shape2SQL however I am not sure how I can import shapefile directly using only SQL scripts as this is important for build automation and deployment. So far I haven't found anything useful just yet.

Would anyone be able to point some directions?

Best Answer

I would equate this to asking for a SQL script that imported an excel file to SQL Server. Even if it were possible, you would be better off if you did not use only SQL scripts to automate the deployment of information.

Its like asking for a screwdriver to hammer in a nail. SQL Server doesn't understand things like shapefile format and excel spreadsheets because that's not what a DBMS is for: it's what DTS and/or SSIS is for.

To answer your question, however, GDAL has been the DTS of spatial information for quite some time now. Have a look at the following command to import spatial information to SQL Server using ogr2ogr:

ogr2ogr -overwrite -f MSSQLSpatial "MSSQL:server=.\MSSQLSERVER2008;database=spatial;trusted_connection=yes" "TG20.shp"

GDAL Binaries (Assuming Windows) are available from this list (via). You can install according to this process, but you should ignore the steps about python.

After installing GDAL, just modify the connection string and data source in the above command to suit your needs. Note that ogr2ogr also supports multiple other formats.