Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
podcast
Filter by Categories
ArcGIS Pro
GDAL
GeoJson
Map
postgis
Python
QGIS
Uncategorized

How to import a shape file into SQL Server

3 ways to import a shape file into SQL Server

Importing a shapefile into SQL Server involves several steps, primarily because SQL Server’s spatial data types are designed to store location information, such as geometry or geography data, but it doesn’t natively support the direct import of shapefiles as some other database systems might. However, you can accomplish this task by using a combination of tools and processes. Here’s a general approach:

1. Import a shape file into SQL Server using GDAL

  • GDAL/OGR: Install GDAL/OGR, a translator library for raster and vector geospatial data formats. It includes a utility called ogr2ogr which is very useful for converting and importing shapefiles. You can download it from GDAL’s official site.

Convert Shapefile to SQL Server Import Format

You can use ogr2ogr to convert the shapefile into a format that can be easily imported into SQL Server. The following command converts a shapefile into a SQL script that can be executed in SQL Server:

ogr2ogr -f MSSQLSpatial "MSSQL:server=your_server;database=your_database;trusted_connection=yes;" your_shapefile.shp -a_srs "EPSG:4326" -progress -overwrite -lco "GEOM_TYPE=geometry" -nln "your_table_name"

Replace your_server, your_database, your_shapefile.shp, and your_table_name with your SQL Server’s name, the database where you want to import the shapefile, the path to your shapefile, and the name you want to give to the new table, respectively. The -a_srs "EPSG:4326" option specifies the spatial reference system; adjust this according to your shapefile’s CRS.

2. Import a shape file into SQL Server using Management Studio (SSMS)

If you prefer a graphical interface or the above method doesn’t suit your needs, you can:

  • Use SQL Server Import and Export Wizard: Although this wizard doesn’t directly support shapefiles, you can first convert your shapefile to a CSV format using a tool like QGIS or GDAL and then import the CSV through the wizard. You’ll need to manually create spatial data columns and import the spatial data accordingly.
  • Bulk Insert: After converting your shapefile to a CSV, you can use the BULK INSERT SQL command to import your data. You’ll need to create a table that matches the CSV structure and includes a column for the spatial data. After importing, use the STGeomFromText function to convert WKT (Well-Known Text) columns into geometry or geography types.

Verify the Import

After importing, you should verify that the data has been correctly imported into SQL Server:

SELECT TOP 10 * FROM your_table_name;

This SQL query will show you the first 10 rows of your newly imported table. Check the spatial data using SQL Server Management Studio or any other tool that can visualize spatial data to ensure it looks correct.

Additional Tips

  • Spatial Reference System (SRS): Be aware of the SRS your shapefile uses and the one your SQL Server is set to use. You might need to transform the SRS during the import process.
  • SQL Server Versions: The exact capabilities and limitations may vary depending on your SQL Server version, so consult the relevant documentation for specifics related to spatial data handling.

3. Import a shape file into SQL Server using QGIS

Importing a shapefile into SQL Server using QGIS involves a few steps, leveraging QGIS’s capabilities to connect to databases and its ability to export or save layers to various formats, including directly to a database like SQL Server. Here’s how you can do it:

Step 1: Install QGIS and SQL Server

  • QGIS: Ensure you have QGIS installed on your computer. If not, download and install it from the QGIS website.

Step 2: Prepare SQL Server

  • Create a Database (if necessary): Before importing the shapefile, you might need to create a new database or decide on an existing database where the data will reside.
  • Enable Spatial Data: Ensure that your SQL Server instance is configured to store spatial data. Modern versions of SQL Server come with spatial data support out of the box.

Step 3: Connect QGIS to SQL Server

  1. Open QGIS and go to the “Data Source Manager” button, or click on “Layer” > “Data Source Manager” from the top menu.
  2. Select the “DB Manager” from the bottom of the list on the left, then choose “Database” > “DB Manager” > “DB Manager…”.
  3. In the DB Manager, click on “Database” > “New Connection”, and select “Microsoft SQL Server” from the list.
  4. Enter your SQL Server connection details:
  • Name: Give your connection a name.
  • Provider/DSN: Choose “Microsoft SQL Server” from the dropdown.
  • Host: Your SQL Server address.
  • Database: Optional at this stage, you can select it later.
  • User name and Password: Enter your credentials if necessary.
  1. Click “Test Connection” to ensure everything is set up correctly, then click “OK” to save the connection.

Step 4: Import the Shapefile into SQL Server

  1. Load the Shapefile into QGIS:
  • Go to “Layer” > “Add Layer” > “Add Vector Layer…” or use the “Open Data Source Manager” button.
  • Browse to your shapefile location, select it, and click “Open” to load it into your QGIS project.
  1. Export the Shapefile to SQL Server:
  • Right-click on the shapefile layer in the “Layers” panel.
  • Choose “Export” > “Save Features As…”.
  • In the “Format” dropdown, select “Microsoft SQL Server”.
  • Under “Database”, select the SQL Server connection you created earlier. If you haven’t selected a database yet, do so now.
  • Specify the Table name you want to use.
  • Adjust the Geometry column name if necessary, and set the SRID (Spatial Reference System ID) to match your shapefile’s projection or the desired projection in SQL Server.
  • Click “OK” after configuring all options.

Step 5: Verify the Import

After the export process completes, you can verify the import directly within QGIS’s DB Manager by browsing the database and table, or you can use SQL Server Management Studio (SSMS) to check the newly created table and ensure the data has been imported correctly.

Additional Tips

  • Coordinate Systems: Pay attention to the coordinate system of your shapefile and how it relates to the spatial reference system used in SQL Server. You might need to transform it during the import process.
  • Data Types and Indexing: After importing, consider adding indexes to your spatial columns to improve performance, especially if you’re working with large datasets.

This process provides a relatively straightforward way to import shapefiles into SQL Server using QGIS, making it accessible even for those who might not be comfortable with command-line tools or direct SQL manipulation.

About the Author
I'm Daniel O'Donohue, the voice and creator behind The MapScaping Podcast ( A podcast for the geospatial community ). With a professional background as a geospatial specialist, I've spent years harnessing the power of spatial to unravel the complexities of our world, one layer at a time.