SQL Differences between stored procedure and triggers

前端 未结 13 1324
北荒
北荒 2021-01-30 20:38

I\'m having trouble understanding the difference between a stored procedure and a trigger in sql. If someone could be kind enough to explain it to me that would be great.

<
相关标签:
13条回答
  • 2021-01-30 21:16
                        ***TRIGGERS*** 
    
    1. Action on specific time.

    2. Triggers is a special type of stored procedure that is not called directly by user.

    3. When the trigger is created, it is defined to fire when a specific type of data modification is made against a specific table or column
    0 讨论(0)
  • 2021-01-30 21:19

    Some differences between triggers and procedures:

    1. We can execute a stored procedure whenever we want with the help of the exec command, but a trigger can only be executed whenever an event (insert, delete, and update) is fired on the table on which the trigger is defined.
    2. Stored procedure can take input parameters, but we can't pass parameters as input to a trigger.
    3. Stored procedures can return values but a trigger cannot return a value.
    4. We can use transaction statements like begin transaction, commit transaction, and rollback inside a stored procedure but we can't use transaction statements inside a trigger
    5. We can call a stored procedure from the front end (.asp files, .aspx files, .ascx files, etc.) but we can't call a trigger from these files.
    0 讨论(0)
  • 2021-01-30 21:20

    Think of a stored procedure like a method in an object-oriented programming language. You pass in some parameters, it does work, and it can return something.

    Triggers are more like event handlers in an object-oriented programming language. Upon a certain condition, it can either (a) handle the event itself, or (b) do some processing and allow for the event to continue to bubble up.

    0 讨论(0)
  • 2021-01-30 21:20

    In respect to triggers in SQL Server: a trigger is a special piece of code that automatically gets executed when an event occurs in the database server.

    DML triggers execute when a user tries to modify data through a data manipulation language (DML) event. DML events are INSERT, UPDATE, or DELETE statements on a table or view. These triggers fire when any valid event is fired, regardless of whether or not any table rows are affected

    We can create trigger like this:

    CREATE TRIGGER TriggerName
    ON [dbo].[TableName]
    FOR DELETE, INSERT, UPDATE
    AS
    BEGIN
        SET NOCOUNT ON
    END
    

    A stored procedure is nothing more than prepared SQL code that you save so you can reuse the code over and over again. So if you think about a query that you write over and over again, instead of having to write that query each time you would save it as a stored procedure and then just call the stored procedure to execute the SQL code that you saved as part of the stored procedure.

    • We can do lot of programming stuff in a stored procedure and execute again and again.
    • We can create procedure which take the input process and give the output
    • We can handle the error through try catch
    • Stored procedures can be nest and call again and again with nested calling
    • It's more secure

    We can create a stored procedure like this:

    CREATE PROCEDURE dbo.Sample_Procedure 
        @param1 int = 0,
        @param2 int  
    AS
        SELECT @param1,@param2 
        RETURN 0;
    

    Differences in both of then

    • Trigger can not be called manually where stored procedure can be called manually.

    • Trigger executes automatically when event happens and can be use for reporting and data protection from deleting or dropping the table and data from database. We can prevent from trigger. On the other hand, a stored procedure has to be called by somebody.

    • A stored procedure can be called from front end (client application) but trigger can not be called from client application.

    0 讨论(0)
  • A stored procedure is a group of SQL statements that is compiled one time, and then can be executed many times. Triggers are named database objects that are implicitly fired when a triggering event occurs. The trigger action can be run before or after the triggering event. Triggers are similar to stored procedures but differ in the way that they are invoked. A trigger is not called directly by a user, where as a stored procedure is directly called by a user.

    0 讨论(0)
  • 2021-01-30 21:28

    A trigger fires after an insert, update, or delete. A stored procedure is a server-side program that is run when you invoke it.

    0 讨论(0)
提交回复
热议问题