I have 2 stored procedures that retrieve data from a table called Places
. The procedures are called by C# code one at a time. These are the table columns.
Give this a try:
CREATE procedure GetNearbyPlaces
@Id int
AS
BEGIN
Declare @Min_Lat decimal(18, 3)
Declare @Min_Lng decimal(18, 3)
Declare @Max_Lat decimal(18, 3)
Declare @Max_Lng decimal(18, 3)
SELECT @Min_Lat=Latitude - 0.005,
@Min_Lng=Longitude - 0.005,
@Max_Lat=Latitude + 0.005,
@Max_Lng=Longitude + 0.005
FROM Places
WHERE Id = @Id
SELECT *
FROM Places
WHERE Latitude BETWEEN @Min_Lat AND @Max_Lat
AND Longitude BETWEEN @Min_Lng AND @Max_Lng
ORDER By ID ASC
END
GO
After relooking this over, another issue you could potentially be having is with DECIMAL(18,2) -- I think this needs to be DECIMAL(18,3) since you're offsetting with .005.
Here is a simpler version with a single sql statement:
SELECT P.*
FROM Places P
JOIN (
SELECT Latitude - 0.005 Min_Lat,
Longitude - 0.005 Min_Lng,
Latitude + 0.005 Max_Lat,
Longitude + 0.005 Max_Lng
FROM Places
WHERE Id = @Id
) P2 ON P.Latitude BETWEEN P2.Min_Lat AND P2.Max_Lat
AND P.Longitude BETWEEN P2.Min_Lng AND P2.Max_Lng
ORDER By ID ASC