1.

What is the difference between Function and Stored procedure?

Answer»

Both are a set of SQL STATEMENTS which are encapsulated inside the block to complete some task. But they are very different in so many ways.

  • STORED Procedure- The set of SQL statements in Stored procedure is pre-compiled objects which get compiled for the very FIRST time only and afterward it gets saved and reused.
  • Functions- These are executed and compiled every time it gets called. It is mandatory for the function to always return some value also it cannot do DML operations on data.

Let me list down some major differences for both :

  1. It is mandatory in function to return value while it is optional in a stored procedure.
  2. Function only support input parameters while stored procedures can have both input/output parameter.
  3. A function can be called from a stored procedure but not vice versa.
  4. The procedure allows SELECT as well DML operations as well but function allows only to SELECT.
  5. There are RESTRICTIONS of using stored procedure in the WHERE/HAVING/SELECT section while NOTHING for function. We use function frequently in WHERE/HAVING/SELECT section.
  6. The transaction is possible in stored procedures but not in function.
  7. An exception can be taken care of in the stored procedure by using the TRY CATCH block but not in function.


Discussion

No Comment Found