2
Answers

Store procedure and functions

sushma sharma

sushma sharma

9y
390
1
what is the difference between store procedure and functions in Sql server and when we use functions in sql server
Answers (2)
0
Rajeesh Menoth

Rajeesh Menoth

NA 24.7k 629.3k 9y
Hi,
 
1) Procedure can return zero or n values whereas function can return one value which is mandatory (Read more Here).
2) Procedures can have input, output parameters for it whereas functions can have only input parameters.
3) Procedure allows select as well as DML(INSERT/UPDATE/DELETE) statements in it whereas function allows only select statement in it.
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) Procedures cannot be utilized in a select statement whereas function can be embedded in a select statement.
 
Ref the below links...
 
 
 
 
0
Manoj Bhoir

Manoj Bhoir

NA 7.6k 294.1k 9y
Hi,

Basic Difference

  1. Function must return a value but in Stored Procedure it is optional( Procedure can return zero or n values).

  2. Functions can have only input parameters for it whereas Procedures can have input/output parameters .

  3. Functions can be called from Procedure whereas Procedures cannot be called from Function.

Advance Difference

  1. Procedure allows SELECT as well as DML(INSERT/UPDATE/DELETE) statement in it whereas Function allows only SELECT statement in it.

  2. Procedures can not be utilized in a SELECT statement whereas Function can be embedded in a SELECT statement.

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

  4. The most important feature of stored procedures over function is to retention and reuse the execution plan while in case of function it will be compiled every time.

  5. Functions that return tables can be treated as another rowset. This can be used in JOINs with other tables.

  6. Inline Function can be though of as views that take parameters and can be used in JOINs and other Rowset operations.

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

  8. We can go for Transaction Management in Procedure whereas we can't go in Function.

Reference : http://www.dotnet-tricks.com/Tutorial/sqlserver/7EDL150912-Difference-between-Stored-Procedure-and-Function-in-SQL-Server.html