Results 1 to 3 of 3

Thread: Trigger ?

  1. #1
    Join Date
    Mar 2002
    Posts
    192

    Unanswered: Trigger ?

    I have a table that has a date field in it. Is there any way to write a trigger or stored procedure that will automatically enter a date/time to that field whenever a user modifies that record? If the answer is yes can someone provide me with a simple example of what it could look like.

  2. #2
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    809
    Code:
    CREATE TRIGGER <trigger name> ON <table name>
    FOR INSERT, UPDATE
    AS
    UPDATE <table name>
       SET <date column> = getdate()
      FROM <table name>
      JOIN inserted ON <table name>.<primary key> = inserted.<primary key>
    Paul Young
    (Knowledge is power! Get some!)

  3. #3
    Join Date
    Oct 2002
    Posts
    1

    Talking

    CREATE TABLE [test1] (
    [a] [int] NULL ,
    [b] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    [c] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    [d] [datetime] NULL
    ) ON [PRIMARY]
    GO

    CREATE TRIGGER insert1
    ON test1
    FOR update
    AS
    BEGIN
    update test1
    set d = getdate()
    from test1 a, deleted b
    where a.a = b.a
    END

    update test1 set b='ASDFGH' where a=100


    It works, considering a as primary key of the table.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •