Difference between Stored Procedure and Function 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.
Differences:
1)     Stored Procedure can return zero or n values whereas function can return one value which is mandatory.
2)      Stored Procedures can have input/output parameters for it whereas functions can have only input parameters.
3)      Stored Procedure allows select as well as DML statement in it whereas function allows only select statement in it.
4)      Functions can be called from stored procedure whereas procedures cannot be called from function.
5)      Exception can be handled by try-catch block in a Stored procedure whereas try-catch block cannot be used in a function.
6)      We can go for transaction management in Stored procedure whereas we can't go in function.
7)     Stored Procedures cannot be utilized in a select statement whereas function can be embedded in a select statement.
8)      UDF can be used in the SQL statements anywhere in the WHERE/HAVING/SELECT section where as Stored procedures cannot be.
9)      UDFs that return tables can be treated as another rowset. This can be used in JOINs with other tables.

10)  Inline UDF's can be though of as views that take parameters and can be used in JOINs and other Rowsetoperations.

Related Posts:

  • Fuzzy logic Fuzzy logic Fuzzy logic is an approach to computing based on "degrees of truth" rather than the usual "true or false" (1 or 0) Boolean logic on which the modern computer is based. Example Fuzzy set theory defines fuzzy ope… Read More
  • What is the difference between oracle,sql and sql server ? What is the difference between oracle,sql and sql server ? •Oracle is based on RDBMS. •SQL is Structured Query Language. •SQL Server is another tool for RDBMS provided by MicroSoft. … Read More
  • SQL SQLObjective type Questions SQLObjective type Question and Answers Set 1  Click here 1.       What does SQL stand for? a.         Structured Query Language b. &… Read More
  • Query to find the Rank or Position SELECT Name, Marks, ( SELECT COUNT(*)+1 FROM Student B WHERE A.Marks<B.Marks) AS Rank FROM Student AORDER BY Marks DESC Output is:     To See the Table Values Click here … Read More
  • Difference between Store Procedure and Trigger? Difference between Store Procedure and Trigger? We can call stored procedure explicitly. But trigger is automatically invoked when the action defined in trigger is done.      ex: create trigger aft… Read More

0 comments:

Post a Comment