Wednesday, July 18, 2012

Stored proc and function

It is a misconception that stored procs run faster than functions. The execution plans for both procs and functions are cached, so the performance suffers only the first time for both. For subsequent runs, the performance of both functions and the procs would be the same.

Procedures support DML statements (insert, update, delete) and create statement (DDL) whereas functions do not support any statements that modify data or create tables.

Procedures support transactions whereas functions do not support transactions.

Procedures may or may not return multiple values (excluding the out parameter) but a function has to return one and only one variable (and functions have no output parameters).

Functions can be part of the another SQL statement like in a select statement or a where statement. No such thing can be done with the stored procedure.

Functions cannot be run independently like stored procedures. Functions can be run only as part of other SQL statements like where, having, select.

Try catch block can be used in the procedures but not inside functions. So stored procedures support error handling (RAISEERROR).

Let me know if you find any errors in the above compilation. Adios.

No comments:

Post a Comment