Friday, 4 September 2015

Stored Procedure and their differences

Stored Procedure is a group of sql statements that has been created once and stored in database. It’s pre-compile objects which are compiled for first time and its compiled format is saved which executes (compiled code) whenever it is called. Stored procedures will accept input parameters so that single stored procedure can be used over network by multiple clients using different input data. Stored procedures will reduce network traffic and increase the performance.

Function is not pre-compiled object it will compile & execute every time whenever it was called.

Differences –

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(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)      We can’t join Stored Procedure,But We can join functions.

Q) Why we can't execute stored procedure inside a function?
Answer:
1. Stored Procedure may contain DML statements.
2. Function can't contain DML statements.
            So executing Function inside stored procedure will never break rule 1.
    But executing stored procedure inside function may break rule no 2.

No comments:

Post a Comment