20 main differences between Stored procedures and Functions in Sql Server

Introduction :In previous articles i explained the Difference between Delete and Truncate in sql server and Difference between Response.Redirect and Server.Transfer in asp.net and Difference between DataSet and DataTable in asp.net and 15 main Difference between DataSet and DataReader in asp.net  and How to createWCF Service to bind,insert,edit,update,delete from sql server database in asp.net C#.
Now in this article i will explain the difference between Stored procedures and Functions in Sql Server. This is also one of the mostly asked interview questions. I have tried to list some important  differences between the two.

Stored Procedure:
A stored procedure is a pre-compiled group of Transact-SQL statements .We can say a stored procedure is a prepared SQL code that we save so that we can reuse the code over and over again.  If a repetitive T-SQL task has to be executed within an application, then the best way for it is to create stored procedure.

It is always recommended to create Stored Procedure instead of writing Inline queries so that we can just call the Stored Procedures whenever required instead of writing Inline queries again and again each time.

You can also pass parameters to the stored procedure, so depending on what the need is the stored procedure can act accordingly based on the parameter values that were passed to it.

Function:
 Function in Sql Server is a Transact-SQL or common language runtime (CLR) routine that takes
parameters, performs an action, and returns the result of that action as a value. The return value can either be a scalar (single) value or a table.


Difference between Stored procedure and Function

1.       Function can return only 1 value whereas Stored Procedure can return many values(maximum 1024)

2.       Functions can have only input parameters for it whereas Stored Procedures can have input/output parameters.

3.       Function takes one input parameter which is mandatory but Stored Procedure may take Zero to n input parameters.

4.       Functions can be used in a select statement where as Stored Procedures cannot.

5.       Functions can be called from Stored Procedure whereas Stored Procedures cannot be called from Function.

6.       Stored procedures are called independently, using the EXEC command, while functions are called from within another SQL statement.

7.       Functions must always return a value (either a scalar value or a table). Stored procedures may return a scalar value, a table value or nothing at all.

8.       Stored Procedure can be used to read and modify data but function can only read data.

9.       Stored Procedure allows SELECT as well as DML (Data Manipulation Language) statements like INSERT/UPDATE/DELETE in it whereas Function allows only SELECT statement in it.

10.   Procedures cannot be utilized in a SELECT statement whereas Function can be embedded in a SELECT statement.

11.   Stored Procedures cannot be used as an inline with a select statement while Functions can. 

12.   Stored procedures are compiled for first time and compiled format is saved and executes compiled code whenever it is called. But Function is compiled and executed every time it is called. 

13.   Stored Procedures cannot be used in the SQL statements anywhere in the WHERE/HAVING/SELECT section whereas Function can be.

14.   Exception can be handled by try-catch block in a Procedure whereas try-catch block cannot be used in a Function.

15.   Stored Procedure allows Transaction Management whereas Function doesn’t.

16.   Stored procedures can be used to change server configuration settings (in terms of security-e.g. setting granular permissions of user rights) whereas function can't be used for this
17.   The Stored Procedures can perform certain tasks in the database by using insert, delete, update and create commands but in Function you can’t perform use these commands. 

18.   Normally the Stored procedures are used to process certain task but the Functions are used to compute the values i.e. we can pass some value as input and then it perform some task on the passed value and return output. 

19.   Stored Procedures can be executed using Execute or Exec command where as Functions can run as an executable file. 

20.   Functions can be used as user defined data types in create table but procedures cannot.

Now over to you:
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."
Previous
Next Post »

16 comments

Click here for comments
Anonymous
June 20, 2013 ×

some of your points are repetitive.. but anyways its a good list

Reply
avatar
admin
Anonymous
June 25, 2013 ×

Nice, article

Reply
avatar
admin
Anonymous
July 30, 2013 ×

Good one !!

Reply
avatar
admin
July 30, 2013 ×

Thanks.it is always nice to hear that..

Reply
avatar
admin
Anonymous
September 09, 2013 ×

It is definitely a good source of valuable information. Thanks a lot.

Reply
avatar
admin
September 09, 2013 ×

i am glad to hear that you found my articles valuable..keep reading.

Reply
avatar
admin
Anonymous
September 12, 2013 ×

Really Uesful..

Reply
avatar
admin
September 12, 2013 ×

thanks for appreciating my work..keep reading

Reply
avatar
admin
Anonymous
October 21, 2013 ×

nice job

Reply
avatar
admin
October 22, 2013 ×

Thanks for liking my work..stay connected and keep reading..:)

Reply
avatar
admin
January 11, 2014 ×

Thanks Vinayak Rahate..stay connected and keep reading for more useful updates like this....

Reply
avatar
admin
February 11, 2014 ×

great bro, cheers!!!!

Reply
avatar
admin
February 11, 2014 ×

thanks Ankit Maini for appreciating my work..stay connected and keep reading for more useful updates like this..

Reply
avatar
admin
June 19, 2014 ×

Its very nice ...for ..and thanks for sharing valuable information..

Reply
avatar
admin
June 21, 2014 ×

Thanks Zahid Neaka for appreciating my work..it is always nice to hear that my articles helped anyone..Stay connected and keep reading..

Reply
avatar
admin

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily.. Out Of Topic Show Conversion CodeHide Conversion Code Show EmoticonHide Emoticon

Thanks for your comment