What is the optimized way to pass multiple parameter from a single table to a function?

As a complement to Edgars answer you can do it without declaring any variables as:

SELECT dbo.fn_calcaerialdistance (x.latitude
                                 ,x.longitude
                                 ,y.latitude
                                 ,y.longitude)
FROM loc.locations as x
CROSS JOIN loc.locations y
WHERE x.place_name = 'Delhi'
  AND y.place_name = 'Mumbai' ;

I wouldn't expect it to perform any better though.


This is the only thing I could think of. You could try this as a basis:

DECLARE
    @p1 DECIMAL(12, 9),
    @p2 DECIMAL(12, 9),
    @p3 DECIMAL(12, 9),
    @p4 DECIMAL(12, 9) ;

SELECT @p1 = latitude, @p2 = longitude 
FROM loc.locations 
WHERE place_name = 'Delhi';

SELECT @p3 = latitude, @p4 = longitude 
FROM loc.locations 
WHERE place_name = 'Mumbai';

SELECT dbo.fn_calcaerialdistance (@p1, @p2, @p3, @p4) ;