OGR2OGR Shapefile Import – Import Shapefile to SQL Server 2019 Using OGR2OGR: Complete Tutorial

importogr2ogrshapefilesql server

I'm trying to import a shapefile into SQL Server 2019, running his command:

ogr2ogr -progress -f "MSSQLSpatial" "MSSQL:server=XXXX;database=test;UID=XXXXX;PWD=XXXX" "MYFILE" -geomfield "geom" -a_srs "EPSG:4326" -lco PRECISION=NO

it is actually importing everything (all columns) except the geometry column. I've also tried omitting the geomfield parameter with the same result.

Best Answer

Ok so it seems I've found the solution. The problem is that the current version of QGIS (GDAL version is the real problem) has a bug and it doesn't create the geography\geometry column in MSSQL...

Installing QGIS 3.4.11 that has GDAL 2.4.1 and using the following command worked:

ogr2ogr -f "MSSQLSpatial" "MSSQL:server=[your server];database=[Your DB];tables=[your table name];UID=[user];PWD=[pwd];driver={SQL Server}" "[your shp file]" -lco GEOMETRY_NAME=GEOM -lco GEOM_TYPE=GEOMETRY -nln "[your table name again]" -a_srs "EPSG:4326" -overwrite -progress -skipfailures -lco UPLOAD_GEOM_FORMAT=wkb

part of the solution was found here (unfortunately this result was far down in the google search): https://github.com/OSGeo/gdal/issues/3140