IntroductionWe are very familiar with this two things if we are familiar with the database thing. procedures are called from our web application or window applications and procedures are too. Both are meant to serve almost the same purpose, but there is a difference between them.
Here in this article I will explain that with some examples.
What is a Procedure and a Function in SQL ?
Stored procedure is a program that is physically present inside the database and those are executed by the database engine itself ( automatically ) in response to a call from the user.A stored procedure may return anything or not.
User Defined Function:
A user defined function is a block of logic that is written for performing a specific task. Those can not be executed directly by the database engine in response to the user call. But a function must return something.
There are three types of User Defined Functions as below;
- Scalar Valued Function : Can return a scalar value like an integer, TimeStamp, string. So this function can be used as a column name in a query.
- Inline Function : Can contain a single select statement.
- Table Valued Function : Can contain multiple row sets, that means it can return a table with the help of multiple statements.
Difference Between Procedure and Function
- It can contain any update, delete, alter, open statements.
- Returns integer integer value always by default.
- Can have input/output parameters.
- Procedures can not be called from a user defined function.
- Exception can be handled by using try - catch.
- We can use transactions inside a procedures.
- Can not be called as an inline query ( can not be called from a select statement )
- A function can contain a insert, update delete statements but that function can not be called from a SQL query.
- Returns one value which is mandatory.
- Can have only input parameters on output parameters.
- Function can be called from a procedure.
- No Exception handling can be done by using try - catch block.
- Can not use transaction inside a function.
- Can be called in a SQL query ( that behaves as a column in a select statement ; only scalar valued function )
That's all for the differentiation between User Defined Functions and Stored Procedure.