Go to content Go to navigation and search


Current SQL Server Blog Articles

    New Version of Package of TSQL Spatial Functions
    Vectorize/Segmentize SQL Server 2012
    Coordinate Editing Functions for SQL Server Spatial
    Function to Scale a geometry object for SQL Server Spatial
    TSQL String Tokenizer Function for SQL Server
    geography/geometry to MBR helper functions
    generate_series for SQL Server 2008
    Extract Polygons from result of STIntersection in SQL Server Spatial
    Function to round ordinates of a SQL Server Spatial geometry object
    Extract elements of SQL Server Spatial geometry object
    Counting number of polygon rings SQL Server Spatial
    Filtering Polygon Rings in SQL Server Spatial
    Function to Move a geometry object in SQL Server Spatial
    Alternate Centroid Functions for SQL Server Spatial
    Function to Rotate geometry objects in SQL Server Spatial
    A GetVertices wrapper for DumpPoints in SQL Server 2008 Spatial
    Creating a Morton number Space Key generator for SQL Server
    Gridding a geometry or geography object (SQL Server Denali)
    On hinting spatial indexes
    Random Search Procedure (SQL Server 2008 Spatial)
    COGO: Converting (Google Earth) Formatted Longitude/Latitude points to decimal degrees (SQL Server)
    COGO: Convert Degrees, Minutes and Seconds values to Decimal Degrees
    COGO: DD2DMS Formatting a latitude/longitude decimal degree value
    COGO: Create point from bearing and distance
    COGO: Compute number of vertices required to stroke circle or circular arc
    COGO: Calculating the bearing between two points (SQL Server 2008 Spatial)
    COGO: Compute arc length subtended by angle centre of circle
    COGO: Compute chord length of segment of a circle (arc)
    COGO: Computing Arc To Chord (Arc2Chord) Separation
    COGO: Creating a stroked polygon from a circle's centre x,y and radius
    COGO: Finding centre and radius of a curve defined by three points: FindCircle function
    Identifying Tight Radius Curves sections within LineString geometry data
    Generating random point data for SQL Server 2008 Spatial
    New Presentation on Active (Searchable) Spatial Metadata for SQL Server 2008 Spatial and FreeText
    Checking is a column in a table or a view is of type geometry or geography
    Extracting geometry type string values from geometry/geography objects in SQL Server 2008 Spatial
    Vectorising geometry objects in SQL Server 2008
    MBR to geography/geometry helper functions
    Extracting geometry types from geometry/geography objects in SQL Server 2008
    Getting the number of coordinate dimensions of a geometry/geography object in SQL Server 2008
    A Dump Points Function for SQL Server 2008 Spatial
    toGeography and toGeography Conversion Functions for SQL Server 2008 Spatial
    Write text file with spatial data from SQL Server 2008
    Loading Shapefiles into Geography type column in SQL Server 2008
    Tip #6: Correcting invalid geometries
    Tip #5: Where or where has my little column gone
    Tip #4: What Coordinate System is that?
    Tip #3: What object is that?
    Tip #2: Spatial Indexing and Primary Keys
    Tip #1: SQL Server 2008 "Katmai" - Setting SRIDs



RSS / Atom

Email me


Creative Commons License
All Blog Articles, Data Models and Free Source Code by Simon Greener, The SpatialDB Advisor is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

Checking is a column in a table or a view is of type geometry or geography

Monday November 23 2009 at 09:45

Sometimes, some things are simple and prosaic rather than clever and complicated.

Building solutions in TSQL requires one to build a prior collection of handy functions before one can be really productive.

Here is a function I use called isGeo which if provided the name of a table/view and a column name returns 1 (true) if the column exists and is of type geometry or geography.

USE [GISDB]  /* Replace this with the name of your database */

CREATE FUNCTION [dbo].[isGeo] (schema_name nvarchar(128), 
                               object_name nvarchar(128), 
                               column_name nvarchar(128))
  DECLARE v_result int;
        SELECT v_result=1
		  FROM (SELECT sc.name as schema_name,
					   ta.name as object_name,
					   co.name as column_name,
					   ty.name as column_type,
					   'TABLE' as object_type
				  FROM sys.tables ta
					   JOIN sys.schemas sc ON sc.schema_id = ta.schema_id 
					   JOIN sys.columns co ON ta.object_id = co.object_id
					   JOIN sys.types ty   ON co.user_type_id = ty.user_type_id
				 WHERE ty.name IN ('geography','geometry')
				SELECT sc.name as schema_name,
					   va.name as object_name,
					   co.name as column_name,
					   ty.name as column_type,
					   'VIEW' as object_type
				  FROM sys.views va 
					   JOIN sys.schemas sc ON sc.schema_id = va.schema_id
					   JOIN sys.columns co ON va.object_id = co.object_id
					   JOIN sys.types ty   ON co.user_type_id = ty.user_type_id
				 WHERE ty.name IN ('geography','geometry')
				) a
			WHERE a.schema_name = schema_name
			  AND a.object_name = object_name
			  AND a.column_name = column_name ;
	Return case when v_result is null then 0 else v_result end;

Now, some tests…

-- Check an existing geometry column in a physical table
select dbo.isGeo('dbo','ADMIN','GEOM') as Result;
-- Check an existing geography column in a View
select dbo.isGeo('dbo','vw_terminal','shape') as Result;
-- Try again with a non-existing schema
select dbo.isGeo('simon','ADMIN','GEOM') as Result;
-- Check action of "RETURNS NULL ON NULL INPUT" function option
select dbo.isGeo(NULL,'ADMIN','GEOM') as Result;
-- Check again with connected schema provided by appropriate function
select dbo.isGeo(SCHEMA_NAME(),'ADMIN','GEOM') as Result;

I hope this helps someone.

Creative Commons License

post this at del.icio.uspost this at Diggpost this at Technoratipost this at Redditpost this at Farkpost this at Yahoo! my webpost this at Windows Livepost this at Google Bookmarkspost this to Twitter

Comment [4]

Information_Schema nazi at your disposal :)

Simon — what’s wrong with doing this instead of all that messy JOIN stuff?

SELECT table_schema, column_name,data_type FROM information_schema.columns
WHERE DATA_TYPE IN(‘geometry’, ‘geography’);

It will catch both tables and views.

Regina · 24 November 2009, 15:36 · #


Of course you are perfectly right to suggest use of Information_Schema. And, yes, it is more succinct.

Still, I didn’t know that geometry and geography were data types from the Information_Schema SQL standard!


Simon · 25 November 2009, 01:20 · #

I think the standard says you have to list the data_type and for standard datatypes you have to call it a specific name even if you don’t call it that natively. Actually not sure it even says that. Anyrate I think most databases that support the information schema — always list the data type even if its specific to that brand of db.

MySQL — there is only one information_schema — so you have to be a bit careful — as their schema is really the database (much like Oracle :) guess another reason why Oracle and MySQL are a good pair) — so their catalog field is blank but the same query as above will work in MySQL if you include table_schema = database_name

PostgreSQL / PostGIS doesn’t quite work without more change. Reason is PostGIS is not a built in datatype.

So — the data_type field contains a useless ‘USER-DEFINED

and the real field you want to query is the udt_name (which like for varchar will have varchar (instead of character varying — which varchar is nicer anyway)

So your equivalent query in PostGIS would be

SELECT table_schema, table_name, column_name, udt_name As data_type
FROM information_schema.columns
WHERE udt_name IN(‘geometry’, ‘geography’);

Still pretty close though :)

Regina · 25 November 2009, 13:22 · #


The udt_name field is only available in PostgreSQL and not SQL Server 2008.

I have used both the SQL Server 2008 proprietary catalog and Information_Schema in coding other functions so I am not against using it here.

In summary, I don’t disagree with using the shorter Information_Schema based approach. The physical implementation details are hidden from the user via use of the isGeo() function anyway.


Simon · 27 November 2009, 01:42 · #