1

I've written this SQL Script:

    DECLARE @location geography  
    DECLARE @radius int  

    SET @location = (SELECT Location FROM Hydrants WHERE HydrantId = 2)  
    SET @radius = (SELECT Radius FROM Hydrants WHERE HydrantId = 2)  

    SELECT * 
    FROM Sites 
    WHERE @location.STDistance(location) < @radius 
    ORDER BY SiteId ASC  

I did this to refactor it

SELECT * 
FROM Sites, Hydrants 
Inner Join Hydrants.Location.STDistance(Sites.Location) < Hydrants.Radius  
WHERE Hydrants.HydrantId = 2 
ORDER BY Sites.SiteId ASC;  

but without luck.

Any advice will be welcome.

5
  • 2
    What is the problem? My mind reading device is on the fritz today. Commented Feb 23, 2012 at 20:24
  • Usually it's a good idea to include the specific RDBMS you're using. Is this SQL Server, if so, what version? Commented Feb 23, 2012 at 20:25
  • @DanNewhouse I think only SQL Server has the geography data type, starting with 2K8. Commented Feb 23, 2012 at 20:34
  • I'm working with SQL Server ver. 2008 r2 Commented Feb 23, 2012 at 20:37
  • Might be better over at codereview.stackexchange.com Commented Feb 23, 2012 at 20:38

2 Answers 2

2

Dont know the geographic features of sql 2008 but when seeing your code, the following might work:

SELECT
    Sites.*
FROM Sites
INNER JOIN Hydrants
    ON Hydrants.Location.STDistance(Sites.location) < Hydrants.Radius
WHERE Hydrants.HydrantId = 2 
ORDER BY Sites.SiteId ASC;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but this gives me both sites and hydrants in the same line. I only need the sites in the given radius from the hydrants.
0

I've never worked with the geography types, but this is the correct syntax of a join that does the same as your original query;

SELECT Sites.*
FROM Sites 
JOIN Hydrants 
  ON Hydrants.Location.STDistance(Sites.Location) < Hydrants.Radius  
WHERE Hydrants.HydrantId = 2 
ORDER BY Sites.SiteId ASC; 

Comments

Your Answer

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.