tags:

views:

31

answers:

1

I am trying to create the following stored procedure in sql server Lat and Lng are the parameters being passed from c# code behind .But I am not able to create this stored procedure it indicates with error saying undefined column name Lat,Lng

CREATE FUNCTION spherical_distance(@a float, @b float, @c float)
RETURNS float
AS
BEGIN
    RETURN ( 6371 * ACOS( COS( (@a/@b) ) * COS(  (Lat/@b)  ) * COS( ( Lng/@b ) - (@c/@b) )  + SIN( @a/@b ) * SIN(  Lat/@b  ) ) )    
END

This is my query from c# code behind.

sqlda.SelectCommand.CommandText = "select *, spherical_distance( Lat, 57.2958, Lng) as distance
                                     from business 
                                    where (( distance < '" + radius + "' )
                                      and (StreetName like '%" + streetname + "%')
                                      and (Keyword like '%" + keyword1 + "%' )) 
                                 order by spherical_distance(Lat,57.2958,Lng)";

This is the view clause

create view [dbo].[business] as 
SELECT Id,
       Name1,
       ZipCode,
       StreetName,
       StreetNumber,
       State1,
       Lat,
       Lng,
       Keyword
  FROM Business_Details
+1  A: 

You named parameters @a and @c and not LON and LAT.

CREATE FUNCTION spherical_distance(@a float, @b float, @c float)
RETURNS float
AS
BEGIN
    RETURN ( 6371 * ACOS( COS( (@a/@b) ) * COS(  (Lat/@b)  ) * COS( ( @c/@b ) - (@c/@b) )  + SIN( @a/@b ) * SIN(  @a/@b  ) ) )    
END
Alex Reitbort
He wants to read Lat and Lng from the view. But I don't get it why it doesn't work.
DHN
Alex is right - in your function RETURN statement, you are trying to do COS( (Lat/@b) ) - but there is no variable called Lat in your function. The only variables defined in your function are @a, @b, @c. Hence your function will not compile. Replacing Lat by @a and Lon by @c - based on what you are passing in your commandtext should do the trick
InSane
Thanks for the reply i have replaced my stored procedure code with this its working fineCREATE FUNCTION spherical_distance1(@a float, @b float, @c float ,@Lat float,@Lng float)RETURNS floatASBEGIN RETURN ( 6371 * ACOS( COS( (@a/@b) ) * COS( (@Lat/@b) ) * COS( ( @Lng/@b ) - (@c/@b) ) + SIN( @a/@b ) * SIN( @Lat/@b ) ) ) END
mahesh