May 23, 2022

Trigger Vs Stored Procedure in SQL Server

Hey guys, in this blog we will learn about the differences between Triggers and Stored Procedures. Both perform a specified task but the major difference comes in the execution part. Triggers are called automatically while the stored procedures are invoked explicitly by the user.

1. Execution: We can execute a Stored Procedure whenever we want with the EXEC command. But, the triggers are fired automatically whenever a specified event (insert, update, delete) is performed on the table.

2. Parameter: Stored Procedures can take parameters as input but Triggers cannot take parameters as input.

3. Return values: Stored Procedures can return values but triggers cannot return value.

4. Transactions: Transaction statements such as begin transaction, commit transaction, and rollback Inside a Stored Procedure. But these statements cannot be used inside Trigger.

5. Calling: Stored Procedure can be called inside a Trigger. But Triggers cannot be defined inside Stored Procedure as the triggers have to invoked automatically when any event (insert, Update, delete) occurs.

6. Scheduling: A job can be scheduled to execute a Stored Procedure on a predefined time. But triggers cannot be scheduled.

7. Print command: We can use Print command inside a Stored Procedure for the debugging process. But we cannot use Print statements inside Triggers. This is one of the reasons why debugging Triggers are troublesome

8. Uses: Stored Procedures are basically used for performing the user-Specified tasks. Whereas, the triggers are normally used for maintaining the referential integrity of the database and performing the auditing works. The activities of the table events can be traced by using Triggers.

9 Syntax: In Stored procedure, exec and print syntax is used while In triggers delete and update syntax are used.


No comments:

Post a Comment

If you have any doubt or question, please contact us.