Triggers Examples

  • Hi, Can any one send me the simple examples of using Instead of Trigger and After Trigger with Northwind database tables.

    I want to create the triggers and execute them.

  • What are you trying to do with triggers?

    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 trigger should insert records into tables.

    I need only general examples.

  • Check the following link

    http://www.sqlservercentral.com/articles/Triggers/64214/

  • tochaturvedi (10/14/2008)


    The trigger should insert records into tables.

    I need only general examples.

    CREATE TRIGGER trg_SomeInsert ON SOMETABLE

    AFTER INSERT

    AS

    Insert into someothertable

    Select somecolumn from inserted

    GO

    Does that help at all?

    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
  • Hi Gail,

    Thanks for the reply.

    I know that the above trigger will fire after insert action to the sometable and insert the records to someothertable.

    You used the statement

    Select somecolumn from inserted.

    What is this inserted? Can i use the sometable inplace of inserted?

  • tochaturvedi (10/14/2008)


    Hi Gail,

    Thanks for the reply.

    I know that the above trigger will fire after insert action to the sometable and insert the records to someothertable.

    Just the one column.

    You used the statement

    Select somecolumn from inserted.

    What is this inserted? Can i use the sometable inplace of inserted?

    inserted and deleted are pseudo tables visible only within triggers. The inserted table will contain the rows affected by an insert or the new values of rows affected by an update. The deleted table contains rows affected by the delete or the old values of rows affected by an update.

    You can use sometable, but then you'll get all the rows in that table. With inserted, you just get the rows that were inserted by the statement that fired the trigger.

    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
  • I have posted something about this on my blog. Please follow link below:

    http://dbalink.wordpress.com/2008/06/20/how-to-sql-server-trigger-101/

    SQL Server Database Administrator

  • Hi all,

    following is my trigger:

    CREATE TRIGGER trigger1 ON Table1 after Update AS

    begin

    INSERT INTO table2

    SELECT * from inserted

    end

    in above Query i want to add record from inserted table to table2 which is on another Database2.

    and Table1 is in Database1.

    After Updating Database1.Table1 trigger should get fire and record get inserted into Database2.Table2.

    How can we do this????

  • ganesh.khuspe_v (4/2/2014)


    Hi all,

    following is my trigger:

    CREATE TRIGGER trigger1 ON Table1 after Update AS

    begin

    INSERT INTO table2

    SELECT * from inserted

    end

    in above Query i want to add record from inserted table to table2 which is on another Database2.

    and Table1 is in Database1.

    After Updating Database1.Table1 trigger should get fire and record get inserted into Database2.Table2.

    How can we do this????

    you need to do the following

    CREATE TRIGGER trigger1 ON Table1 after Update AS

    begin

    INSERT INTO [Database2].[schema].table2(ID, Name) ---- this is required otherwise will get error.

    SELECT ID, Name

    from inserted

    end

    and table2 must exists in the Database2 on the same sql server.

    hope it helps

  • Thanx for the reply. I did the same on the same server but its not working. Actually i wanted to do the same on two different Sql server. My one Database Server is server1 and another one is Server2.One database present on server1 and other is on server2.Following is My query.

    CREATE TRIGGER trigger1 ON Document after Update AS

    begin

    INSERT INTO [TEST].[dbo].Document_Log(ID,InwardID,RetrievalID,UnitID,DepartmentID,DocumentType,

    DocumentCriticality,AdditionalInfo1,AdditionalInfo2,AdditionalInfo3,DocAccRelated,AccountNumber,

    CustomerName,TransactionDateFrom,TransactionDateTo,DocCategory,DocClosingDate,ProposedDestructionDate,

    CheckerDestructionDate,

    SuperCheckerDestructionDate,

    DocumentStatusID,

    DocumentName,

    Location,

    StorageVendor,

    BoxBarcode,

    IsRefiled,

    ReinventoryStatus,

    DateofDispatch,

    AdditionalDispatchDetails,

    FileBarcode,

    InwardDate,

    RetrievalDate,

    LostStatus,

    CreatedOn,

    CreatedBy,

    UpdatedOn,

    UpdatedBy) ---- this is required otherwise will get error.

    SELECT ID,InwardID,RetrievalID,UnitID,DepartmentID,DocumentType,

    DocumentCriticality,AdditionalInfo1,AdditionalInfo2,AdditionalInfo3,DocAccRelated,AccountNumber,

    CustomerName,TransactionDateFrom,TransactionDateTo,DocCategory,DocClosingDate,ProposedDestructionDate,

    CheckerDestructionDate,

    SuperCheckerDestructionDate,

    DocumentStatusID,

    DocumentName,

    Location,

    StorageVendor,

    BoxBarcode,

    IsRefiled,

    ReinventoryStatus,

    DateofDispatch,

    AdditionalDispatchDetails,

    FileBarcode,

    InwardDate,

    RetrievalDate,

    LostStatus,

    CreatedOn,

    CreatedBy,

    UpdatedOn,

    UpdatedBy

    from inserted

    end

    I m getting an error as

    Msg 208, Level 16, State 1, Procedure trigger1, Line 4

    Invalid object name 'TEST.Document_Log'.

    I tried this on the same server by making replica of database2 on the same server. But same is not working on one server then how it will work on different server. Please Suggest me something so that i can do the same on two different server.

    Thanks...

  • Thanx for the reply. I did the same on the same server but its not working. Actually i wanted to do the same on two different Sql server. My one Database Server is server1 and another one is Server2.One database present on server1 and other is on server2.Following is My query.

    CREATE TRIGGER trigger1 ON Document after Update AS

    begin

    INSERT INTO [TEST].[dbo].Document_Log(ID,InwardID,RetrievalID,UnitID,DepartmentID,DocumentType,

    DocumentCriticality,AdditionalInfo1,AdditionalInfo2,AdditionalInfo3,DocAccRelated,AccountNumber,

    CustomerName,TransactionDateFrom,TransactionDateTo,DocCategory,DocClosingDate,ProposedDestructionDate,

    CheckerDestructionDate,

    SuperCheckerDestructionDate,

    DocumentStatusID,

    DocumentName,

    Location,

    StorageVendor,

    BoxBarcode,

    IsRefiled,

    ReinventoryStatus,

    DateofDispatch,

    AdditionalDispatchDetails,

    FileBarcode,

    InwardDate,

    RetrievalDate,

    LostStatus,

    CreatedOn,

    CreatedBy,

    UpdatedOn,

    UpdatedBy) ---- this is required otherwise will get error.

    SELECT ID,InwardID,RetrievalID,UnitID,DepartmentID,DocumentType,

    DocumentCriticality,AdditionalInfo1,AdditionalInfo2,AdditionalInfo3,DocAccRelated,AccountNumber,

    CustomerName,TransactionDateFrom,TransactionDateTo,DocCategory,DocClosingDate,ProposedDestructionDate,

    CheckerDestructionDate,

    SuperCheckerDestructionDate,

    DocumentStatusID,

    DocumentName,

    Location,

    StorageVendor,

    BoxBarcode,

    IsRefiled,

    ReinventoryStatus,

    DateofDispatch,

    AdditionalDispatchDetails,

    FileBarcode,

    InwardDate,

    RetrievalDate,

    LostStatus,

    CreatedOn,

    CreatedBy,

    UpdatedOn,

    UpdatedBy

    from inserted

    end

    I m getting an error as

    Msg 208, Level 16, State 1, Procedure trigger1, Line 4

    Invalid object name 'TEST.Document_Log'.

    I tried this on the same server by making replica of database2 on the same server. But same is not working on one server then how it will work on different server. Please Suggest me something so that i can do the same on two different server.

    Thanks...

  • If you want to move the data from Server1 to Server2, then you need a connect to that server.

    1) Create a link to Server2 link

    2) Create a SYNONYM

    CREATE SYNONYM myTable2 FOR

    [Server2].[DataBase2].dbo.Table2;

    3) Change your Query like this

    INSERT INTO MyTable2(ID,InwardID,RetrievalID,UnitID,DepartmentID,DocumentType,

    DocumentCriticality,AdditionalInfo1,AdditionalInfo2,AdditionalInfo3,DocAccRelated,AccountNumber,

    CustomerName,TransactionDateFrom,TransactionDateTo,DocCategory,DocClosingDate,ProposedDestructionDate,

    CheckerDestructionDate,

    SuperCheckerDestructionDate,

    DocumentStatusID,

    DocumentName,

    Location,

    StorageVendor,

    BoxBarcode,

    IsRefiled,

    ReinventoryStatus,

    DateofDispatch,

    AdditionalDispatchDetails,

    FileBarcode,

    InwardDate,

    RetrievalDate,

    LostStatus,

    CreatedOn,

    CreatedBy,

    UpdatedOn,

    UpdatedBy) ---- this is required otherwise will get error.

    SELECT ID,InwardID,RetrievalID,UnitID,DepartmentID,DocumentType,

    DocumentCriticality,AdditionalInfo1,AdditionalInfo2,AdditionalInfo3,DocAccRelated,AccountNumber,

    CustomerName,TransactionDateFrom,TransactionDateTo,DocCategory,DocClosingDate,ProposedDestructionDate,

    CheckerDestructionDate,

    SuperCheckerDestructionDate,

    DocumentStatusID,

    DocumentName,

    Location,

    StorageVendor,

    BoxBarcode,

    IsRefiled,

    ReinventoryStatus,

    DateofDispatch,

    AdditionalDispatchDetails,

    FileBarcode,

    InwardDate,

    RetrievalDate,

    LostStatus,

    CreatedOn,

    CreatedBy,

    UpdatedOn,

    UpdatedBy

    from inserted

    But i wont recommend using this kind of logic in the trigger. If you are using Store Procedure for

    C.R.U.D operation, then i would suggest to bring this logic into a store Procedure.(If you need a immediate result)

    If you just storing this information for logging purposes i would suggest the following:

    1) Create a Log Dump table in Database1.

    2) Create a SSIS package to move the data from Server1 to server 2 and Truncate the Dump Table in Database1.

    3) Create a SQL JOB, link it with SSIS package, and Set the desired Interval.

    Hope it helps.

  • I dont have any idea about SSIS package. I tried to make new Flat file in SSIS but not worked. Can u please give me practical example.???

  • You can get your hands dirty in SSIS. Following is the link for Stairway to Integration Services[/url] by Andy Leonard

    As per your current case, you just need to move the data from Source Table to the Destination Table.

    Just like importing/Export Wizard in SQL SEVER. If you want to use the SSIS Approach you need to the following

    1) Create a Log Table in Server1.Database1 and use this table in the Trigger.

    2) Create a replica Log Table on Server2.Database2.

    3) For kick start sample ssis package is attached. (change the file extention from txt to dtsx).

    Hope it helps.

Viewing 15 posts - 1 through 14 (of 14 total)

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