Triggers vs. Stored Procedures

0

After searching the internet I found several tutorials on the subject, but what is still unclear to me is when to use a trigger and when a stored procedure.

Thank you very much for any possible guidance !!!

Sincerely,

Miguel Angel

    
asked by Miguel Angel 28.05.2017 в 18:23
source

2 answers

0

The main difference between triggers and stored procedures: It is that triggers are procedures that are executed automatically, when an event occurs on which you want to work. For this there are three types of events that can trigger a trigger: INSERT, DELETE and UPDATE. The trigger is programmed to perform a specific task that must be done whenever one of the aforementioned events occurs. It does not require human or programmatic intervention and can not be stopped. It has some characteristics:

  • It does not receive input or output parameters.
  • The only input values are those corresponding to the columns that are inserted, and are only accessible by means of certain pseudovariables (NEW and OLD).
  • An INSERT / UPDATE / DELETE operation can not be executed on the same table where the TRIGGER is running.
  • A task can not be executed on another table, if the second one has a trigger that affects the table of the first running trigger (circularity).
  • You can not invoke procedures from a TRIGGER.
  • A SELECT that returns a result table in the TRIGGER can not be invoked.
  • Others
  • A stored procedure is a stored procedure that must be invoked to execute.

  • You can receive parameters and return parameters.
  • Can handle any table, perform operations with them, and perform read / write iterations.
  • You can return a table as a result. also values within the parameters of the prototype if they are also output.
  • They exist in the base where they are created, but do not depend on any table.
  • They can accept recursion (but it is not recommended).
  • Others
  • -------------

    In summary, if you are going to perform an audit of the tables in your database, use triggers. If you want to use stored procedures it can be to get a list of clients. This action will be repeated in the system but the work is done directly by the database

        
    answered by 28.05.2017 / 19:40
    source
    0

    A trigger (in English) as its name says triggers when an event occurs in the database, such as an INSERT or a DELETE. Triggers are created and you do not need to call them to run, they simply run with the requirement that you have established. For example, you make a trigger that when an UPDATE is made in the database in a field, the trigger is executed and INSERT in another table with the old values.

    On the other hand, a procedure is like a function that you create and you can call from another site to use it as you want. You can have a procedure that tells you how many times it appears on behalf of the students that you pass as a parameter. And every time you call him he will do that. It does not run just like a trigger.

    I hope I helped you and explained myself well. Greetings.

        
    answered by 28.05.2017 в 19:34