What is difference between function and procedure in SQL SERVER

Stored Procedures are pre-compile objects which are compiled for first time and its compiled format is saved which executes (compiled code) whenever it is called. But Function is compiled and executed every time when it is called.

  1. Function must return a value but in Stored Procedure it is optional( Procedure can return zero or n values)
  2. Procedure allows select as well as DML(INSERT/UPDATE/DELETE) statements in it whereas function allows only select statement in it.
  3. Functions can have only input parameters for it whereas Procedures can have input/output parameters 
  4. Functions can be called from procedure whereas procedures cannot be called from function.
  5. Exception can be handled by try-catch block in a procedure whereas try-catch block cannot be used in a function.
  6. We can go for transaction management in procedure whereas we can't go in function.
  7. Functions can be called from select statement.Procedures can’t be called from Select/Where/Having etc statements. Execute/Exec statement can be used to call/execute stored procedure.
  8. In function we can use only table variables, it will not allow using temporary tables.But in stored procedure we can use both table variables as well as temporary table in it.

Comments

Popular Posts