Functions vs Stored Procedures
Solution 1:
If you're likely to want to combine the result of this piece of code with other tables, then obviously a table-valued function will allow you to compose the results in a single SELECT statement.
Generally, there's a hierarchy (View < TV Function < Stored Proc). You can do more in each one, but the ability to compose the outputs, and for the optimizer to get really involved decreases as the functionality increases.
So use whichever one minimally allows you to express your desired result.
Solution 2:
Functions must be deterministic, and cannot be used to make changes to the database, whereas stored procedures allow you to do inserts and updates, etc.
You should limit your use of functions, since they pose a huge scalability problem for big, complex queries. They become sort of a "black box" for the query optimizer, and you'll see enormous differences in performance between using functions and simply inserting the code into a query.
But they are definitely useful for table-valued returns in very specific cases.
If you need to parse a comma-delimited list, to simulate passing an array to a procedure, a function can turn the list into a table for you. This is common practice with Sql Server 2005, since we can't pass in tables to stored procedures yet (we can with 2008).
Solution 3:
From the docs:
If a stored procedure meets the following criteria, it is a good candidate for being rewritten as a table-valued function:
The logic is expressible in a single SELECT statement but is a stored procedure, rather than a view, only because of the need for parameters.
The stored procedure does not perform update operations, except to table variables.
There is no need for dynamic EXECUTE statements.
The stored procedure returns one result set.
The primary purpose of the stored procedure is to build intermediate results that are to be loaded into a temporary table, which is then queried in a SELECT statement.
Solution 4:
I am going to write few interesting differences between stored procedures and functions.
- We can use functions in select queries but we cannot use stored procedures in select queries.
-
We cannot use non deterministic functions in Functions but we can use non deterministic functions in stored procedures. Now question comes up, what is non deterministic function.. Ans is:-
A non deterministic function is that function which returns different outputs for same input values at different time, like getdate(). It always returns different value whenever it is run.
Exception:-
Earlier versions of sql server prior to sql 2000 do not allow to use getdate() function in user defined functions, but version 2005 and onward allows us to use getdate() function within a user defined function.
Newid() is another example of non deterministic function but cannot be used in user defined functions but we can use it in stored procedure.
We can use DML(insert, update, delete) statements within a stored procedure but we cannot use DML statements in functions on physical tables or permanent tables. If we want to do DML operation in functions we can do it over table variables not on permanent tables.
We cannot use error handling within function but we can do error handling in stored procedures.
Solution 5:
Procedure can return zero or n values whereas function can return one value which is mandatory.
Procedures can have input/output parameters for it whereas functions can have only input parameters.
Procedure allows select as well as DML statement in it whereas function allows only select statement in it.
Functions can be called from procedure whereas procedures cannot be called from function.
Exception can be handled by try-catch block in a procedure whereas try-catch block cannot be used in a function.
We can go for transaction management in procedure whereas we can't go in function.
Procedures can not be utilized in a select statement whereas function can be embedded in a select statement.
UDF (User Defined function) can be used in the SQL statements anywhere in the
WHERE
/HAVING
/SELECT
section whereas stored procedures cannot be.UDFs that return tables can be treated as another rowset. This can be used in
JOIN
s with other tables.Inline UDFs can be though of as views that take parameters and can be used in
JOIN
s and other rowset operations.