创建查找指定位置附近的商店的存储过程
1. 在您在上一过程中添加的 EXECUTE 语句下,添加以下 Transact-SQL 代码。
CREATE PROCEDURE GetNearbyStoresGML @Lat nvarchar(10), @Long nvarchar(10)
AS
-- Create a point geography instance based on the supplied location
DECLARE @SearchPoint geography
SET @SearchPoint = geography::Point(@Lat, @Long, 4326)
-- Create a polygon geography instance by adding a 100km buffer to the point
DECLARE @SearchArea geography
SET @SearchArea = @SearchPoint.STBuffer(100000)
--Return the search area and all store locations that intersect it
SELECT 'Search Area', '100 KM radius', @SearchArea.AsGml()
UNION ALL
SELECT StoreName,
StoreAddress + ', Tel:' + StorePhone AS ContactDetails,
StoreLocation.AsGml() As StoreGML
FROM Stores
WHERE StoreLocation.STIntersects(@SearchArea) = 1
GO
AS
-- Create a point geography instance based on the supplied location
DECLARE @SearchPoint geography
SET @SearchPoint = geography::Point(@Lat, @Long, 4326)
-- Create a polygon geography instance by adding a 100km buffer to the point
DECLARE @SearchArea geography
SET @SearchArea = @SearchPoint.STBuffer(100000)
--Return the search area and all store locations that intersect it
SELECT 'Search Area', '100 KM radius', @SearchArea.AsGml()
UNION ALL
SELECT StoreName,
StoreAddress + ', Tel:' + StorePhone AS ContactDetails,
StoreLocation.AsGml() As StoreGML
FROM Stores
WHERE StoreLocation.STIntersects(@SearchArea) = 1
GO
注意:此代码创建一个 geography 实例,该实例中包含一个基于传递到存储过程的纬度和经度参数的点。然后此代码使用 geography 数据类型的 STBuffer 方法创建表示搜索点周围方圆 100km 范围内的多边形 geography 实例。最后,此代码返回定义搜索区域的 geography 实例的 GML 表示形式以及搜索区域中的所有商店,通过使用 geography 数据类型的 STIntersects 方法可以找到这些商店。
2. 选择刚刚添加的 CREATE PROCEDURE 语句,然后单击执行运行所选的代码。
3. 在 CREATE PROCEDURE 语句下,添加下面的代码以测试存储过程。
EXECUTE GetNearbyStoresGML '34.000000', '-118.000000'
4. 选择刚刚添加的 EXECUTE 语句,然后单击执行运行所选的代码。
5. 在结果窗格中,单击第一个 XML 值,以在 XML 查看器中查看搜索区域的 GML 表示形式。
6. 关闭 XML 查看器并返回到查询编辑器。
7. 将查询脚本文件另存为 C:\SQLHOLs\Spatial and VE\Starter\StoredProcs.sql,然后关闭 SQL Server Management Studio。