Results 1 to 3 of 3

Thread: Sql Server trigger for update

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 1999
    Posts
    11

    Post

    Hi,
    Does anybody have an example of sql server trigger for an update?

    Gil

  2. #2
    Guru Clunietp's Avatar
    Join Date
    Oct 1999
    Location
    USA
    Posts
    1,844

    Post

    Hi Gil

    This is something I threw together, it checks to see if a value already exists, and raises an error if it does.

    Code:
    CREATE TRIGGER tr_test ON [Names] 
    FOR INSERT
    AS
    -- Declare variables
    Declare @NewName varchar(50)
    Declare @MyCount int
    
    -- Get Name of inserted value
    Select @NewName = inserted.[name] from inserted
    
    -- Get Count 
    Select @MyCount = count(*) from Names where [Name] = @NewName
    
    -- Check to see if it already exists
    if @MyCount > 1
    BEGIN
    -- roll back change, raise error
    ROLLBACK
    RAISERROR ('NAME ALREADY EXISTS!', 16, 1)
    END

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 1999
    Posts
    11

    Post

    Thanks,

    It wasn't exactly what I need but it gave me a starting point.
    I am adding an example of update trigger for everybody.

    Code:
    -------------------------------------------
    CREATE TRIGGER IntUpdate ON appIntD
    FOR update
    AS
    declare @newkod int
    declare @oldkod int
    select @oldkod = deleted.kod from deleted
    select @newkod = inserted.kod from inserted
    if update(kod)
    begin
    update appIntLinD
    set intr=@newkod
    where intr= @oldkod
    end
    -------------------------------------------
    gil

Posting Permissions

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



Click Here to Expand Forum to Full Width