Home:ALL Converter>SQL Server 2008: Must Declare Scalar Variable (Geography)

SQL Server 2008: Must Declare Scalar Variable (Geography)

Ask Time:2013-01-24T09:03:24         Author:master_gracey

Json Formatter

I am attempting to do a basic proximity search using SQL Server 2008 (passing lat/long of specified position so that returned results are locations in table ordered by distance from position).

I have code that works perfectly within SQL Server. But, when I try to execute it from within ASP/VBScript (don't ask), I get the following error:

Must declare the scalar variable '@currentPosition' (refers to line commented below)

Here is the code:

objCommand.CommandText = "declare @currentLatitude float, @CurrentLongitude float"
objCommand.Execute
objCommand.CommandText = "declare @currentPosition geography"
objCommand.Execute
objCommand.CommandText = "declare @radiusBuffer geography"
objCommand.Execute
''''error line below:
objCommand.CommandText = "Set @radiusBuffer = @currentPosition.BufferWithTolerance(10 * 1609.344,.9,1);"
objCommand.Execute
objCommand.CommandText = "set @CurrentLatitude = 12.34567"
objCommand.Execute
objCommand.CommandText = "set @CurrentLongitude = -12.34567"
objCommand.Execute
objCommand.CommandText = "SET @currentPosition = geography::Point(@CurrentLatitude, @CurrentLongitude, 4326);"
objCommand.Execute
objCommand.CommandText = "SELECT a.*, ROW_NUMBER() OVER (ORDER BY GeoLocation.STDistance(@currentPosition) ASC) AS RowNum from table_name a WHERE a.GeoLocation.STDistance(@currentPosition) < 16093.44"
objCommand.Execute

I have the variable declared (or at least I think I do), but obviously I'm missing something or doing it incorrectly. Hopefully someone smarter than me can show me what :)

Thank you for your time.

Author:master_gracey,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/14492211/sql-server-2008-must-declare-scalar-variable-geography
yy