How would I rewrite this query to be performant by executing the SQL function only once?
SELECT Top 1 Id, Name
FROM Users U
INNER JOIN UserDetail D on U.Id = D.Id
LEFT OUTER JOIN CreditCards C ON C.Id = U.Id AND UserHasCC(U.Id) = 1
LEFT OUTER JOIN CreditCardDetails CD on C.CCID = C.CCID AND UserHasCC(U.Id) = 1
WHERE
((CD.active = 1 and UserHasCC(U.Id) = 1) OR UserHasCC(U.Id) = 0) and
U.active = 1 and
((C.IsInternational = 1 and UserHasCC(U.Id) = 1) OR UserHasCC(U.id = 0)
Basically, the query gets all users that don't have credit cards and those that have active international credit cards.