Search code examples
sql-server-2008geography

STWithin not working for Geography


Issue:

When executing @somegeog.STWithin(@othergeog) I get the execption:

Msg 6506, Level 16, State 10, Line 5
Could not find method 'STWithin' for type 'Microsoft.SqlServer.Types.SqlGeography' in assembly 'Microsoft.SqlServer.Types'

Background:

I am working with SQL Server (2008 R2) Geography data for the first time. I have a list of points in a table, and I want to retrieve those in a given area. I intend to write a stored procedure that accepts latitude and longitude bounds for the area.

I can write this procedure easily by extracting the latitude and longitude from the stored geography points, but I was hoping to let the inbuilt functionality do the work. After googling the topic for a while, I found the STWithin method, but even using Microsoft's example, I get an error claiming the method doesn't exist.

MS Example from link:

DECLARE @g geography;
DECLARE @h geography;
SET @g = geography::Parse('POLYGON ((-120.533 46.566, -118.283 46.1, -122.3 47.45, -120.533 46.566))');
SET @h = geography::Parse('CURVEPOLYGON (COMPOUNDCURVE (CIRCULARSTRING (-122.200928 47.454094, -122.810669 47.00648, -122.942505 46.687131, -121.14624 45.786679, -119.119263 46.183634), (-119.119263 46.183634, -119.273071 47.107523), CIRCULARSTRING (-119.273071 47.107523, -120.640869 47.569114, -122.200928 47.454094)))');
SELECT @g.STWithin(@h);

Can somebody please explain what I'm doing wrong?

Thanks in Advance!


Solution

  • I think the example you are looking at applies to SQL Server 2012. Try the example here: http://technet.microsoft.com/en-us/library/bb933991.aspx