Home Forums SQL Server 2005 T-SQL (SS2K5) Choosing the faster option; Stored Procedure or Function? RE: Choosing the faster option; Stored Procedure or Function?

  • For my 2 cents.

    I've found the fastest way to do something like select zip codes within a radius is to use simple betweens for latitude and longitude into a temp table. Latitude and longitude can be indexed in your zip code table. You calculate the furthest latitudes and longitudes from the center point and use those for your initial temp table.

    This gives you a square basically around the center point. Then use the trigonometric functions to trim the square into a circle and delete temp table records initially selected.

    This may jot be applicable to your application.

    As far as functions go, I've found that they can be very, very fast when used to string data together - it's about the only way to avoid a cursor. It's still RBAR, but it's faster RBAR.

    Something like:

    CREATE FUNCTION fnStringCustNotes

    (

    @CustomerIDINT

    )

    RETURNS VARCHAR(MAX)

    AS

    BEGIN

    DECLARE

    @NotesVARCHAR(MAX)

    , @Comma VARCHAR(1)

    SET @Notes = ''

    SET @Comma = ''

    SELECT @Notes = @Notes + @Comma + X.Notes, @Comma = ','

    FROM

    (SELECT Notes

    FROM CustomerNotes

    WHERECustomerID = @CustomerID

    ) AS X

    RETURN @Notes

    END