• How to create and use trigger in SQL?

    by  • November 12, 2015 • Microsoft SQL Server • 0 Comments

    A trigger is used to perform certain action on the table like insertion, deletion or updation of data. It is database object which is bound to a table and is executed automatically. You can implicitly invoke trigger.

    Types of Triggers

    There are three action query types that you use in SQL which are:

    • INSERT
    • UPDATE
    • DELETE

    In this article you will learn trigger, types of trigger and how to create and used trigger in SQL. The only focus on insert trigger in this article.

    Create Table:

    Create the table name Student and name the column of table is ID, Name and Percentage. After this inset data on student table.

    Insert Trigger

    This trigger is fired after an INSERT on the table. Let’s create the trigger as:

    The CREATE TRIGGER statement is used to create the trigger. THE ON clause specifies the table name on which the trigger is to be attached. The FOR INSERT specifies that this is an AFTER INSERT trigger. In place of FOR INSERT, AFTER INSERT can be used. Both of them mean the same.

    In the trigger body, table named inserted has been used. This table is a logical table and contains the row that has been inserted. I have selected the fields from the logical inserted table from the row that has been inserted into different variables, and finally inserted those values into the Audit table.


    Best WordPress Themes and Plugins with Great Team and Support!

    Avatar of Abdul Rehman


    Abdul Rehman writes on .NET technologies at GetCodeSnippet.com. GetCodeSnippet.com provides you high quality Microsoft .NET development related articles, tutorials and sample code for ASP.NET, C#, VB.NET, AJAX, WCF, WPF, Web Services, Windows Services, SQL Server and other technologies.


    Leave a Reply