1) IF vs multiline TF vs CLR TF
Inline Table Valued Function
Acts like macro, very efficient can be treated like parametrized view
CREATE FUNCTION dbo.name(@param INT)
RETURNS TABLE
AS
RETURN
SELECT ...
FROM tab t
WHERE t.Col = @param;
GO
Multi Statement Table Valued Function
More flexible you can do many intermediate steps if needed but it will be slower than IF.
CREATE FUNCTION dbo.name()
RETURNS @Result TABLE
(Col_name INT NOT NULL,
...
)
AS
BEGIN
/* Many operations */
INSERT @Result
SELECT *
FROM ...
RETURN
END
GO
CLR Table-Valued Functions
From MSDN
Transact-SQL table-valued functions materialize the results of calling
the function into an intermediate table. Since they use an
intermediate table, they can support constraints and unique indexes
over the results. These features can be extremely useful when large
results are returned.
In contrast, CLR table-valued functions represent a streaming
alternative. There is no requirement that the entire set of results be
materialized in a single table. The IEnumerable object returned by the
managed function is directly called by the execution plan of the query
that calls the table-valued function, and the results are consumed in
an incremental manner. This streaming model ensures that results can
be consumed immediately after the first row is available, instead of
waiting for the entire table to be populated. It is also a better
alternative if you have very large numbers of rows returned, because
they do not have to be materialized in memory as a whole. For example,
a managed table-valued function could be used to parse a text file and
return each line as a row.
2) Checking if function exists:
Check ROUTINES catalog:
Returns one row for each stored procedure and function that can be
accessed by the current user in the current database.
IF EXISTS ( SELECT 1
FROM INFORMATION_SCHEMA.ROUTINES
WHERE Specific_schema = 'dbo'
AND specific_name = 'Foo'
AND Routine_Type = 'FUNCTION' )