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 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.