This topic describes how to determine whether to rewrite existing stored procedure logic as user-defined functions. For example, if you want to invoke a stored procedure directly from a query, repackage the code as a user-defined function.
In general, if the stored procedure returns a (single) result set, define a table-valued function. If the stored procedure computes a scalar value, define a scalar function.
If a stored procedure meets the following criteria, it is a good candidate for being rewritten as a table-valued function:
INSERT #temp EXEC sp_getresults
SELECT ...
FROM #temp, t1
WHERE ...
The sp_getresults stored procedure can be rewritten as a table-valued function, for example fn_results(), which means the preceding statements can be rewritten as:
SELECT ...
FROM fn_results(), t1
WHERE ...