Click here to Skip to main content
15,908,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

As I know that in triggers we perform an action on change of data in table.
I want to ask that if i am using stored procedure and perform delete from a table and then if i want to delete records from other table, for this i can write one more Sql query in stored procedure to perform action.

Why we need triggers? what is the use of triggers?

please give me answer with explanation.

thanks in advance.
Posted

A Stored Procedure[^] does some stuff (like select, update, delete, make calculations) when you call it and Trigger[^] does some stuff (usually updates or inserts) whenever it is automatically triggered.
So assume you have a Table with three fields ID, Name and Counter. Now EVERYTIME Name is updated you want to increment Counter by one. You could write a Stored Procedure for this, but for an update you would always need to call the Stored Procedure. Someone WILL not do this though and your Counter is now running behind. If you write a Trigger for this Counter is ALWAYS updated (wether you change Name from code, a Stored Procedure or even manually!
Of course the use of Triggers has its downsides. I always find it to be a 'black box', I do some update statement and the result is different then I expected! After hours of debugging I find a Trigger that alters my data. Also, if you're not careful with Triggers they could trigger around endlessly (and cause and Exception). In the above example what if your trigger accidentally watched the entire row? Name will be updated then Counter. Counter again triggers the Trigger which will update Counter etc... :)
I recently had an experience with Triggers that locked entire Database Tables!
Also, any update that triggers a Trigger will, of course, take a little longer because the Trigger has to execute.

I am not saying Triggers are bad, but you have to know when and how to use them. Best use them with extreme caution! So this is not a question of 'which is best', but what do you need.
You have to decide which approach your specific case needs.
Good luck :)
 
Share this answer
 
v2
Comments
Wendelius 18-May-12 16:42pm    
Good answer, my 5.
Sander Rossel 18-May-12 17:41pm    
Thanks Mika :)
The difference between triggers and procedures in short:

  • triggers fire autonomously upon modifications
  • procedures are executed based on a request

What to use is a matter of design. In my opinion both are good and necessary and they are not comparable.

If you think about an environment where you have several tables and procedures. Most likely you will make modifications on the same tables in several procedures. Now if you don't use triggers you have to repeat part of the business rules in all the procedures and when they change, well you change them all. This wouldn't happen if the logic is properly divided between triggers and procedures.

As a rule of thumb I would suggest using triggers when the modification is autonomous, it doesn't matter where or why the change occurs. Procedures then again are built to handle larger tasks almost like a unit-of-work.

Another point of view is that is it certain that the modifications are done using the procedures? What if someone executes a modification on a table directly. In this case the trigger still reacts while the procedure is bypassed.

To sum it: Don't compare these two, but use them both in scenarios in what they are designed for :)
 
Share this answer
 
Comments
Sander Rossel 18-May-12 17:43pm    
Also a good answer, my 5.
Wendelius 18-May-12 17:45pm    
Thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900