What are the differences between the User Defined Functions (UDF) and Store Procedures in the Database terminology?
I know for new comers or beginners, it seems that we cannot find the any difference between the Functions and Store Procedure. But there are so many differences between these two terms in the database. I am going to discuss something about these two terminologies called Store Procedure and Function.Let’s talk about these in detail one by one.
Store Procedure:- A store procedure is defined as the program or the process or also procedure which is stored in the database itself by physically. Structure Query Language (SQL) and PL/SQL for ORACLE Database and DB2 are the languages are being used to create these SP’s. In simple way we can say that when we run or execute these SP’s, on the base of the user request, run on the database engine, which always runs on the different servers, returns the values to user as per his/her request. Because a store procedure is having the direct access to the database, so, that’s why we are getting the manipulated resulted values for user. It is the best way to communicate back and forth within the large database for the desired results.
User Defined Function (simply called Function):- A function is defined as the user defined routine to encapsulate the logic for being used in the other queries. Views are only useful for the single SELECT statement. But the Functions are useful for the multiple SELECT statements for providing the useful logic for the given statements. Here User Defined Functions (UDF) are categorized or classified into three different forms:-
1) Scalar Valued Function:- A scalar value function is used to return the scalar value only like Integers or may be timestamp. This value can be the name of the column in the table in the database like MS SQL, PL/SQL or DB2
2) Inline Function:- A inline function is being used for returning the single SELECT statement in the
database
3) Table Valued Function:- A table valued function is being used for any number of the row set values. It became more useful in the case of the returning multiple rows set at the same time. Because it is not possible to enclose the heavy logic in the single SELECT statement. This is the advantage of using the table valued function over the inline function.
Here are some more points to differentiate between Store Procedures and Functions to clarify to everyone in very simple way:-
a) Function can only return one scalar value which is required for user defined function. But the SP can return zero (0) or any number of values
b) We can just have the only input parameters in the user defined function (UDF). Whereas in the store procedure we can have the input and output parameters as well
c) Functions only allow the select statement no insert/update. But we can use the Store Procedures for the SELECT and DML statement
d) Function can be called from the Store Procedure. But we cannot called the Store Procedure from the Function
e) Exception can be handled in the CATCH and TRY process in the Store Procedure, whereas in the function we cannot apply the CATCH and TRY block.
f) Function(s) can be used in the select statement for getting the results. But we cannot use the SP in the select statement for our database results.
g) We cannot use the store procedure in the SQL statement for the where/having/select section. We can use the function in these conditions very easily.
h) In the function we can return the tables as another rows set. This could be done with the help of the JOINs with the other tables.
i) Inline user defined functions can be used in the other rows set operations with the help of the JOINs
Thanks,
Sandeep Rajmail


Leave a Reply