You must Sign In to post a response.
  • Category: .NET

    Trigger

    trigger tutorials, how to insert, update, delete by trigger.
  • #597612
    hi

    Triggers is used perform action after insert/update/create etc.

    Check below code for update table values after insert statement

    Table like this


    create table emp12
    (ID int primary key identity(1,1),
    ename varchar(30),
    BasicPay int,
    HRA int,
    Total int)


    Trigger code


    CREATE TRIGGER [dbo].[UpEmpSal]
    ON [dbo].[emp12]
    AFTER INSERT, UPDATE
    AS
    BEGIN
    SET NOCOUNT ON;
    DECLARE @i INT
    Select @i = ID FROM Inserted
    update emp12 set Total=BasicPay+HRA where ID = @i
    END

    //Insert into table like this

    insert into emp12(ename,BasicPay,HRA) values('ravi','500','500')


    After i insert values in the emp12 table total is updated as 1000 automatically

    Regards
    N.Ravindran
    Your Hard work never fails

  • #597635
    The following example will calculate the total of value1 and value2 and insert into total column when a record is inserted or updated:


    create table tbl1
    (ID int primary key identity(1,1),
    val1 int,
    val2 int,
    total int)

    CREATE TRIGGER [dbo].[UpdateTotal]
    ON [dbo].[tbl1]
    AFTER INSERT, UPDATE
    AS
    BEGIN
    SET NOCOUNT ON;
    DECLARE @id INT
    Select @id = ID FROM Inserted
    update tbl1 set total=val1+val2 where id = @id
    END

    insert into tbl1(val1,val2) values(21,31)


    Thanks & Regards
    Paritosh Mohapatra
    Microsoft MVP (ASP.Net/IIS)
    DotNetSpider MVM

  • #597642
    Hi,
    These has been answered many times, so I am providing you only the link

    http://www.dotnetspider.com/forum/157777-Explain-about-trigger-with-sample-example-coding.aspx

    All is well.
    Thanks,
    Dharma
    Editor,Mentor,MVM
    Try and fail but don't fail and try

  • #597712
    A trigger is a block of code that constitutes a set of T-SQL statements that are activated in response to certain actions or conditions.

    A trigger can also be interpreted as a special kind of stored procedure that is executed whenever an action,such as data modification,takes place.

    A Trigger is fired whenever data in the underlying table is affected by any of the Data Manipulation Language (DML) statements -INSERT,UPDATE,OR DELETE.

    Triggers help in maintaining consistent,reliable,and correct data in tables.They enable the performance of complex actions and cascade these actions to other dependent tables.


    Thanks & Regards


    Bunty

  • #597744
    CREATE trigger ic_trg_ic_user on ic_user ---- created trigger on 17-10-2007
    for update as
    IF(NOT UPDATE(User_Status))
    BEGIN
    insert into ic_user_audit
    (
    userid,
    last_login_date,
    client_ip,
    login_status
    )
    select
    ins.userid,
    ins.last_login_date,
    ins.client_ip,
    'Success'
    from inserted ins

    END


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.