Let\'s say I have to implement a piece of T-SQL code that must return a table as result. I can implement a table-valued function or else a stored procedure that returns a se
I personally use table valued functions when all I am returning is a single table with no affects. Basically I treat them like parameterized views.
If I need multiple recordsets returned or if there will be values updated in tables, I use a stored procedure.
My 2 cents
It depends :) If you want to use the table-valued result in another procedure, you're better of using a TableValued Function. If the results is for a client, the stored proc is usualy the better way to go.
As mentioned above, functions are more readable/composable/self documenting, but are less performant in general, and can be seriously less performant if you get carried away with them in joins such as
SELECT *
FROM dbo.tvfVeryLargeResultset1(@myVar1) tvf1
INNER JOIN dbo.tvfVeryLargeResultset1(@myVar2) tvf2
ON (tvf1.JoinId = tvf2.JoinId)
Often, you just have to accept the redundancy of code that a tvf could eliminate (at a unacceptable performance cost.)
One other point I haven't yet seen mentioned is that you can't use database state-changing temp tables inside of a multi-statement tvf. The most functionally equivalent mechanism to a temp table is the non-state changing, in memory table variable, and for large datasets, a temp table will likely be more performant than a table variable. (Other alternatives include dynamic tables & common table valued expressions, but at some level of complexity, these cease to be a good option IMO.)
I would perfromance test both. It is likely the sp approach or a derived table would be significantly faster than a function and if so that approach should be used. In general I avoid functions becasue they can be performance hogs.
Stored procedures are pre compiled queries which executes faster and saves from the sql injections. They can return 0 or N values. We can perform DML operations inside the stored procedures. We can use functions inside the procedures and can use functions in the select query. Functions are used to return any value and DML operations not possible in functions. functions are of two types scalar and tabled-valued. scalar function returns a single value, tabled-valued function used to returns rows of tables.
I am going to write few interesting differences between stored procedures and functions.
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.