Social Icons

Tuesday, February 14, 2012

Differences between stored procedures and triggers in databases?

Stored procedures are compiled collection of programs or SQL statements that live in the database. A stored procedure can access and modify data present in many tables. Also a stored procedure is not associated with any particular database object. But triggers are event-driven special procedures which are attached to a specific database object say a table. Stored procedures are not automatically run and they have to be called explicitly by the user. But triggers get executed when the particular event associated with the event gets fired. For example in case of a database having say 200 users and the last modified timestamp need to be updated every time the database is accessed and changed. To ensure this one may have a trigger in the insert or update event. So that whenever any insert or update event of the table gets fired the corresponding trigger gets activated and updates the last modified timestamp column or field with the current time. Thus the main difference between stored procedure and trigger is that in case of stored procedure the program logic is executed on the database server explicitly under eth user’s request but in case of triggers event-driven procedures attached to database object namely table gets fired automatically when the event gets fired.

2 comments:

  1. A stored procedure (and function) is a program block that can be called upon to perform it's function.

    A trigger "fires" (is run) upon a certain action taken on a table and or column. It can contain it's own procedure or call a stored procedure within the database.

    For example, whenever an employee table record's salary changes (update), a trigger can be set up to call a procedure to print out a salary change notification.

    ReplyDelete
  2. TRIGGER

    1- when you create a trigger you have to identify event and action of your trigger but when you create s.p you don't identify event and action

    2-trigger is run automatically if the event is occured but s.p don't run automatically but you have to run it manually

    3- within a trigger you can call specific s.p but within a s.p you cann;t call atrigger

    ReplyDelete

 

Sample text

Sample Text