SQL Server 2008 : Multiple Queries in One Stored Procedure

后端 未结 1 1717
忘了有多久
忘了有多久 2021-02-11 06:27

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.



        
1条回答
  •  鱼传尺愫
    2021-02-11 06:36

    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
    
    • SQL Fiddle Demo

    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
    
    • Updated Fiddle

    0 讨论(0)
提交回复
热议问题