Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Monday, 9 February 2015

Introduction:

 In this article i will explain the difference between Stored procedures and User Defined Functions (UDF's) in SQL Server.
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.
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.
User Defined Function (UDF):
 User Defined 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.
Two type of functions are there in SQL i.e. Scalar valued Functions & Table Valued.
Difference between Stored procedure and Function
1.        User Defined Function can return only 1 value which is mandatory whereas Stored Procedure can return many values(maximum 1024)

2.       User Defined Function can have only input parameters for it whereas Stored Procedures can have input/output parameters.
3.       User Defined Function can be used in a select statement where as Stored Procedures cannot be.

4.       User Defined Function can be called from Stored Procedure whereas Stored Procedures cannot be called from User Defined Function (except extended stored procedures).

5.       Stored procedures are called independently, using the EXEC or EXECUTE command, while User Defined Functions are called from within another SQL statement.

6.       User Defined Functions must always return a value (either a scalar value or a table). Stored procedures may  or may not a value.

7.       Stored Procedure can be used to read and modify data but User Defined Function can only read data.

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

9.       Stored Procedures can return XML data types but User Defined Function can't
10.   Stored procedures are compiled for first time and compiled format is saved and executes compiled code whenever it is called. But User Defined Function is compiled and executed every time it is called. 

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

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

13.   Stored Procedure allows Transaction Management whereas User Defined Function doesn’t.

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

16.   Normally the Stored procedures are used to process certain task but the User Defined Function 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. 

17.   In Stored Procedures we can use temporary tables or table variables to store temporary date but in function we can use only table variable. Temporary tables can not be created in User Defined Function .

18.   User Defined Function can be used as user defined data types in create table but procedures cannot.

0 comments:

Post a Comment