SQL Server - putting a block of code into a view/temp table

Viewed 44

sorry about the above title, I'm not quite sure on how to word it, but here is the gist...

I have found this query below which will calculate the miles between 2 points in longitude and latitude...

DECLARE @sourceLatitude FLOAT = 53.0150594250908;  
DECLARE @sourceLongitude FLOAT = -2.24460456782419;  
DECLARE @destinationLatitude FLOAT = 52.002933355733400;
DECLARE @destinationLongitude FLOAT = -0.976678285584733;
DECLARE @Location FLOAT  
SET @Location = SQRT(POWER(69.1 * ( @destinationLatitude - @sourceLatitude),  
2) + POWER(69.1 * ( @sourceLongitude  
- @destinationLongitude )  
* COS(@destinationLatitude / 57.3), 2))  
PRINT @Location  

now based on the PRINT result, I am trying to create a view which will display a list of these results. for example, i have a table called TableAddress which contains a list of address's

TableAddress   
Address            Long                  Lat
Address 1    52.5600450207834     -0.274050229426521
Address 2    53.5746997938162     -0.62449270669287
Address 3    50.3404675259117     -5.15208822743251

and I have TableDestination which contains a list of destinations:

TableDestination 
Address            Long                  Lat
Destination 1    52.5063420216939   -2.07973524437415
Destination 2    50.9776014579626   -1.35438374178925
Destination 3    53.5493068199536   -0.679623916124968
  1. the user will select the AddressDestination via a parameter
  2. based on this result, i want to create a view which will list how far each of the address's are from the destination, using the above query.

Im not usre where to start so could someone please point me in the right direction into how i could achieve this?

thankyou very much

example result:

 theView
    AddressName           DestinationName          Distance
    Address 1              Destination 1             38 
    Address 2              Destination 1             49
    Address 3              Destination 1             16
1 Answers
Related