当涉及到MS SQL并且在搜索过程中发现此代码时,我是一个完整的新手。它看起来就像我想要的那样,它是基于纬度和纬度值进行半径搜索的。创建函数时关键字'CREATE'附近的语法不正确
但是,我不断收到:关键字'CREATE'附近的语法不正确。,这是代码的第一行。我的数据库是2008年MS SQL
下面是代码:
CREATE FUNCTION CalculateDistance
(@Longitude1 Decimal(8,5),
@Latitude1 Decimal(8,5),
@Longitude2 Decimal(8,5),
@Latitude2 Decimal(8,5))
Returns Float
AS BEGIN
Declare @Temp Float
Set @Temp = sin(@Latitude1/57.2957795130823) * sin(@Latitude2/57.2957795130823) + cos(@Latitude1/57.2957795130823) * cos(@Latitude2/57.2957795130823) * cos(@Longitude2/57.2957795130823 - @Longitude1/57.2957795130823)
if @Temp > 1
Set @Temp = 1
Else If @Temp < -1
Set @Temp = -1
Return (3958.75586574 * acos(@Temp))
End
-- FUNCTION
CREATE FUNCTION LatitudePlusDistance(@StartLatitude Float, @Distance Float) Returns Float
AS BEGIN
Return (Select @StartLatitude + Sqrt(@Distance * @Distance/4766.8999155991))
End
-- FUNCTION
CREATE FUNCTION LongitudePlusDistance
(@StartLongitude Float,
@StartLatitude Float,
@Distance Float)
Returns Float
AS BEGIN
Return (Select @StartLongitude + Sqrt(@Distance * @Distance/(4784.39411916406 * Cos(2 * @StartLatitude/114.591559026165) * Cos(2 * @StartLatitude/114.591559026165))))
End
-- ACTUAL QUERY
-- Declare some variables that we will need.
Declare @Longitude Decimal(8,5),
@Latitude Decimal(8,5),
@MinLongitude Decimal(8,5),
@MaxLongitude Decimal(8,5),
@MinLatitude Decimal(8,5),
@MaxLatitude Decimal(8,5)
-- Get the lat/long for the given id
Select @Longitude = Longitude,
@Latitude = Latitude
From qccities
Where id = '21'
-- Calculate the Max Lat/Long
Select @MaxLongitude = LongitudePlusDistance(@Longitude, @Latitude, 20),
@MaxLatitude = LatitudePlusDistance(@Latitude, 20)
-- Calculate the min lat/long
Select @MinLatitude = 2 * @Latitude - @MaxLatitude,
@MinLongitude = 2 * @Longitude - @MaxLongitude
-- The query to return all ids within a certain distance
Select id
From qccities
Where Longitude Between @MinLongitude And @MaxLongitude
And Latitude Between @MinLatitude And @MaxLatitude
And CalculateDistance(@Longitude, @Latitude, Longitude, Latitude) <= 2
任何想法是怎么回事?
谢谢!!!
编辑:非常感谢bluefeet和Aaron Bertrand为我指出正确的方向!
除了使用'GO'分隔批次外,[请使用'dbo.'前缀创建/引用对象,*尤其是*函数](http:// sqlblog。COM /博客/ aaron_bertrand /存档/ 2009/10/11 /坏习惯,对开球避开最架构prefix.aspx)。 –
@AaronBertrand hello亚伦,我的服务器在共享环境中,MS SQL数据库表都不在dbo中,而是它们是user21587。所以我的表将是user21587.qccities这是否有所作为?我确实尝试了我的前缀和dbo。前缀,两者仍然给我错误,现在它说:'去'附近的语法不正确。 – Jennifer
你究竟在哪里运行这段代码?你是否尝试过分别运行每个“CREATE FUNCTION”? –