Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Combining a query and a function
Message
 
À
13/04/2010 16:10:00
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Autre
Versions des environnements
SQL Server:
SQL Server 2008
Application:
Web
Divers
Thread ID:
01460047
Message ID:
01460048
Vues:
48
>Is it possible to combine a query with a function into one SQL command?
>
>I have this query:
>
>
>declare @CenterLat float=47.7795
>declare @CenterLon float=-65.7191
>declare @SearchDistance float=250
>
>select * from test
> where (latitude>=@CenterLat - @SearchDistance/111.0 and latitude<=@CenterLat + @SearchDistance/111.0) and
> (longitude>=@CenterLon - @SearchDistance/111.0 and longitude<=@CenterLon + @SearchDistance/111.0)
> and dbo.CalculateDistance(@CenterLon, @CenterLat, Longitude, Latitude) <=@SearchDistance
>
>
>I have this function:
>
>
>ALTER FUNCTION [dbo].[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) ) 
>
>
>If it is possible, what kind of syntax can I use to combine all this into one SQL command?


Sure, you could use it that way:
select *, dbo.CalculateDistance(.....) AS Distance
       from test
WHERE ...

--- or
select *
       from test
WHERE ... AND dbo.CalculateDistance(.....) BETWEEN .... AND ....
UPDATE:
After reading your post one more time, I'm not so sure that I'm understanding your question right :o))
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform