7

I have a db and want to know if it contains any spatial tables. Is there a table in SQL Server that holds spatial metadata i.e. a list of all the spatial tables in the current db? Oracle has the user_sdo_geom_metadata, Postgis the geometry_columns. What about SQL Server? The only spatial metadata I found was the sys.spatial_references.

asked Nov 12, 2011 at 9:52

2 Answers 2

7

You can always resort to querying the INFORMATION_SCHEMA.TABLES and COLUMNS dictionaries to find out if there are tables that are spatially enabled.

whuber
70.4k16 gold badges189 silver badges285 bronze badges
answered Nov 12, 2011 at 16:54
6

Further to unicoletti's answer, here is a SQL query that will list spatial tables and their geometry columns:

select c.TABLE_CATALOG, c.TABLE_NAME as TABLE_NAME, 
 c.TABLE_SCHEMA as TABLE_SCHEMA, c.COLUMN_NAME as COLUMN_NAME 
 from information_schema.columns c join 
 information_schema.tables t ON c.TABLE_NAME = t.TABLE_NAME
 AND t.TABLE_TYPE = 'BASE TABLE' where c.DATA_TYPE = 'geometry'
 order by c.TABLE_SCHEMA, c.TABLE_NAME

If you're looking for geography columns change the geometry to geography in the query.

Also bear in mind a single table can have several spatial columns so it will appear twice in the query.

answered Nov 14, 2011 at 15:04
1
  • 1
    The "appear twice in the query" issue can be solved by removing "c.COLUMN_NAME as COLUMN_NAME" from the query, and changing "select" to "select distinct". Also, to get tables that have either spatial type or both, the WHERE clause would be "WHERE c.data_type IN ('geometry','geography')" Commented Dec 16, 2011 at 19:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.