SQL Server存储过程,地理位置不返回数据

时间:2018-04-23 11:34:36

标签: sql-server sql-server-2008 stored-procedures

我编写了以下存储过程,以根据传递的纬度/经度和类别ID返回数据。

我需要返回一个交易者列表,其覆盖范围在传递的lat long范围内(并且它们涵盖了传递的类别)。因此,我希望围绕交易者纬度/多头位置绘制一个圆,使用他们将操作的半径来计算x个米(这存储在Traders.OperatingRadius列中)。如果传递的lat long coord在此范围内,那么它们应该包含在返回列表中。

CREATE PROCEDURE FindTradersWithinRadiusLatLong 
    @LAT decimal(9,6),
    @LONG decimal(9,6),
    @CATEGORY int
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @GEO1 GEOGRAPHY;

    SET @GEO1 = geography::Point(@LAT, @LONG, 4326)

    SELECT
        x.Id, x.Name, 
        x.Latitude, x.Longitude,
        x.Distance, x.IsArchived
    FROM
        (SELECT
             Traders.Id, Traders.Name,
             Latitude, Longitude,
             CategoryId = TraderCategories.Id,
             OperatingRadius,
             Traders.IsArchived,
             Distance = (@geo1.STDistance(geography::Point(ISNULL(Latitude, 0), ISNULL(Longitude, 0), 4326)))
         FROM
             ((Addresses
         INNER JOIN
             Traders ON Addresses.TraderId = Traders.Id)
         INNER JOIN
             TraderCategories ON Traders.Id = TraderCategories.TraderId)) AS x
    WHERE
        x.Distance <= x.OperatingRadius
        AND x.CategoryId = @CATEGORY
        AND (x.IsArchived = 0 OR x.IsArchived = NULL);
END
GO

TraderCategories是一个链接表,如下所示;

    Table TraderCategories
    int FK TraderId 
    int FK CategoryId 

现在我添加了一个地址;

latitiude - 43.590000, Longitude - -111.120000

对于Id 1

的类别,还有一个TraderCategory关系

然后我尝试使用上面的方法调用存储过程,并且不返回任何匹配项。

表格定义如下:

CREATE TABLE [Bemfeito].[Addresses]
(
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Address1] [nvarchar](max) NULL,
    [Address2] [nvarchar](max) NULL,
    [Address3] [nvarchar](max) NULL,
    [TraderId] [int] NULL,
    [Latitude] [decimal](9, 6) NOT NULL,
    [Longitude] [decimal](9, 6) NOT NULL,
    [OperatingRadius] [real] NOT NULL DEFAULT (CONVERT([real],(0)))

    CONSTRAINT [PK_Addresses] 
        PRIMARY KEY CLUSTERED ([Id] ASC)
)
GO

ALTER TABLE [Bemfeito].[Addresses]  WITH CHECK 
    ADD CONSTRAINT [FK_Addresses_Traders_TraderId] 
        FOREIGN KEY([TraderId]) REFERENCES [Bemfeito].[Traders] ([Id])
GO

ALTER TABLE [Bemfeito].[Addresses] CHECK CONSTRAINT [FK_Addresses_Traders_TraderId]
GO

CREATE TABLE [Bemfeito].[Traders]
(
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Email] [nvarchar](max) NULL
    [Name] [nvarchar](max) NULL

    CONSTRAINT [PK_Traders] 
        PRIMARY KEY CLUSTERED ([Id] ASC)
)
GO

CREATE TABLE [Bemfeito].[TraderCategories]
(
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [CategoryId] [int] NULL,
    [TraderId] [int] NULL,

    CONSTRAINT [PK_TraderCategories] 
        PRIMARY KEY CLUSTERED ([Id] ASC)
)
GO

ALTER TABLE [Bemfeito].[TraderCategories] WITH CHECK 
    ADD CONSTRAINT [FK_TraderCategories_Categories_CategoryId] 
        FOREIGN KEY([CategoryId]) REFERENCES [Bemfeito].[Categories] ([Id])
GO

ALTER TABLE [Bemfeito].[TraderCategories] CHECK CONSTRAINT [FK_TraderCategories_Categories_CategoryId]
GO

ALTER TABLE [Bemfeito].[TraderCategories] WITH CHECK 
    ADD CONSTRAINT [FK_TraderCategories_Traders_TraderId] 
        FOREIGN KEY([TraderId]) REFERENCES [Bemfeito].[Traders] ([Id])
GO

ALTER TABLE [Bemfeito].[TraderCategories] CHECK CONSTRAINT [FK_TraderCategories_Traders_TraderId]
GO

最后完成类别

CREATE TABLE [Bemfeito].[Categories]
(
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](max) NULL,
    [Value] [int] NOT NULL,
    CONSTRAINT [PK_Categories] 
        PRIMARY KEY CLUSTERED ([Id] ASC)
)

有谁能告诉我,我在哪里出错?

1 个答案:

答案 0 :(得分:0)

如果查看这个微软引用here,您应该注意到传递给STDistance的参数是一种几何数据类型,而您传递的是Point数据类型。

目前这样写的行

,Distance = (@geo1.STDistance(geography::Point(ISNULL(Latitude, 0), ISNULL(Longitude, 0), 4326))

应写如下。

,Distance = (@geo1.STDistance(geography::STGeomFromText('Point('+ISNULL(Longitude, 0)+' '+ISNULL(Latitude, 0)')',4326))