Difference between stored procedure and function in sql server

Today I am very tired hence not willing to write a long article..But for sure I am going to write few interesting differences between stored procedures and functions.

1) We can use functions in select queries but we cannot use stored procedures in select queries.
2) 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 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. 

2)Newid() is another example of non deterministic function but cannot be used in user defined functions but we can use it in stored procedure.

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

4)We cannot use error handling within function but we can do error handling in stored procedures.

I hope this article will help you at least in interview.. :)

2 comments:

bhavya exploring life...... said...

thank you..for sharing this...very informative..got to know the facts...really appreciate ...ur efforts...happing blogging..

bhavya exploring life...... said...

thank you..for sharing this...very informative..got to know the facts...really appreciate ...ur efforts...happing blogging..