Functions vs Stored Procedures

后端 未结 12 652
我寻月下人不归
我寻月下人不归 2020-12-02 06:26

Let\'s say I have to implement a piece of T-SQL code that must return a table as result. I can implement a table-valued function or else a stored procedure that returns a se

12条回答
  •  情话喂你
    2020-12-02 06:40

    1. Procedure can return zero or n values whereas function can return one value which is mandatory.

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

    3. Procedure allows select as well as DML statement 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 can not be utilized in a select statement whereas function can be embedded in a select statement.

    8. UDF (User Defined function) can be used in the SQL statements anywhere in the WHERE/HAVING/SELECT section whereas 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 UDFs can be though of as views that take parameters and can be used in JOINs and other rowset operations.

提交回复
热议问题