views:

54

answers:

1

I'm about to implement this function to calculate some numbers.

CREATE FUNCTION [dbo].[funLookupFTE] (@PFID int) RETURNS
    VARCHAR(20) AS BEGIN 

DECLARE @NumberOfFTE AS VARCHAR(20)
SET @NumberOfFTE = (SELECT  SUM(CASE WHEN Hours <= 20 THEN 0.5 WHEN Hours > 20 THEN 1     END) AS FTECount 
                    FROM tblPractitioners 
                    WHERE PFID =
@PFID)
RETURN @NumberOfFTE
END

Just trying to see if it's optimal and it won't take matters into its own hand.

Thanks

+4  A: 

If this is to be called in any sort of query, you're probably better off using a subquery instead of a user-defined function.

For example:

SELECT * FROM tblPractioners P1
INNER JOIN
(
    SELECT  PFID, SUM(CASE WHEN Hours <= 20 THEN 0.5 WHEN Hours > 20 THEN 1 END) 
        AS FTECount 
    FROM tblPractitioners 
    GROUP BY PFID
) P2 ON P1.PFID = P2.PFID

Performance of user-defined functions can be very poor, but you'll really need to compare the execution plans using a subquery or UDF to see which is better.

LittleBobbyTables
+1: Leave some for us, eh?
OMG Ponies
@OMG - Hey, I was gone to dinner for a few hours, you guys had your chance. And whoops, didn't realize how late it was!
LittleBobbyTables
@OMG Ponies - Look who's talking!! :-) :-) Both of you leave some for the rest of us, i say!! :-)
InSane
@InSane: Sir, you *wound* me! :)
OMG Ponies
I'm cracked up by your comments :) but thanks for the insights.
webb