create update trigger

  • I have a view called view_badge and a table called t_badge.

    Both the table and the view have the same structure

    create table t_badge

    YEAR (varchar(12),not null),

    STU_CODE (varchar(12) primary key,not null),

    JOIN_CODE (varchar(12),not null),

    FORENAME1 (varchar(30),null),

    FORENAME2 (varchar(30),null),

    SURNAME (varchar(40),null),

    DOB (datetime,null)

    COURSE (varchar(18),not null),

    END_DATE (datetime,null))

    how would a create an update trigger on view_badge to update t_badge on updates to view_badge.

  • I'm not sure what you mean? If you update the view, it updates the table. The view is just a window into the table, there is nothing in the view to update.

    If you need a trigger, then create it on the table and when someone updates the view, which updates the table, the trigger will fire.

  • The view is not created from the table,

    The table is created from the view.

    So i created the table then inserted the data from the view.

    I have a script which fires every minutes if there any any new rows.

  • icampbell (12/13/2007)


    The view is not created from the table,

    The table is created from the view.

    So what's view based on? Another table? Several tables?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • The view is based on several tables

  • Keep in mind that this only works when the view is updated and not the underlying table changes that make up the view. If you need to track the changes made to the underlying tables that make up the view you will have to create multiple triggers, one on each table, and/or just create a procedure to handle everything.

    IF OBJECT_ID('tr_view_badge') IS NOT NULL

    DROP TRIGGER tr_view_badge

    go

    create trigger tr_view_badge

    on view_badge

    instead of insert, update, delete

    as

    BEGIN

    DECLARE @action CHAR(1)

    IF(EXISTS(SELECT * FROM inserted) AND EXISTS(SELECT * FROM deleted))

    SET @action = 'U'

    ELSE IF(EXISTS(SELECT * FROM inserted))

    SET @action = 'I'

    ELSE IF(EXISTS(SELECT * FROM deleted))

    SET @action = 'D'

    IF @action IS NULL

    return

    --Do what you need to the table based on the action

    END

    GO

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply