-- Finds intersecting branches
-- Accepts lat and lng
CREATE PROC SP_GET_INTERSECTING_BRANCHES
@lat FLOAT,
@lng FLOAT
AS
BEGIN
DECLARE @point GEOMETRY
SET @point = GEOMETRY::Point(@lng, @lat, 4326)
SELECT BranchId, BranchName FROM [dbo].[Branch]
WHERE @point.STIntersects(SpatialData) = 1
END
GO
-- Finds the closest branches
-- Accepts lat, lng and the amount of matching rows to return
CREATE PROC SP_GET_CLOSEST_BRANCHES
@lat FLOAT,
@lng FLOAT,
@amount INTEGER
AS
BEGIN
DECLARE @point GEOMETRY
SET @point = GEOMETRY::Point(@lng, @lat, 4326)
SELECT TOP (@amount) BranchId, BranchName, @point.STDistance(SpatialData) AS Distance FROM [dbo].[Branch]
ORDER BY @point.STDistance(SpatialData)
END
GO