locked
Need help in trigger creation RRS feed

  • Question

  • hi

    i have two table tableA and tableB.

    i need to create trigger , if tableA has any row inserted or updated , the same should be inserted and updated in tableB as well.

    thanks

    Friday, April 25, 2014 1:18 PM

Answers

  • The basic syntax for a trigger is covered in BOL.  Triggers have been frequently discussed in the forums, so reading these past discussions will likely provide some insight into the appropriate use of triggers generally.  As a starting point, how you would write an insert query to "copy" rows from tableA to TableB?  How would you write an update query to keep similar rows "the same"?  If you cannot write those queries, it will be difficult for you to implement this logic in a trigger.

    Before you go down this dangerous path, you should think twice about intentionally duplicating data. 

    • Proposed as answer by Naomi N Friday, April 25, 2014 3:43 PM
    • Marked as answer by Fanny Liu Monday, May 5, 2014 9:53 AM
    Friday, April 25, 2014 3:00 PM

All replies

  • The basic syntax for a trigger is covered in BOL.  Triggers have been frequently discussed in the forums, so reading these past discussions will likely provide some insight into the appropriate use of triggers generally.  As a starting point, how you would write an insert query to "copy" rows from tableA to TableB?  How would you write an update query to keep similar rows "the same"?  If you cannot write those queries, it will be difficult for you to implement this logic in a trigger.

    Before you go down this dangerous path, you should think twice about intentionally duplicating data. 

    • Proposed as answer by Naomi N Friday, April 25, 2014 3:43 PM
    • Marked as answer by Fanny Liu Monday, May 5, 2014 9:53 AM
    Friday, April 25, 2014 3:00 PM
  • The goal of any database is to reduce redundancy. Why do you want to increase it and use procedural code to d it? 


    --CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking in Sets / Trees and Hierarchies in SQL

    • Proposed as answer by Naomi N Friday, April 25, 2014 4:22 PM
    Friday, April 25, 2014 4:12 PM
  • i need to create trigger , if tableA has any row inserted or updated , the same should be inserted and updated in tableB as well.

    Same database, as noted above, not a good idea. Different database, consider replication:

    http://msdn.microsoft.com/en-us/library/ms151198.aspx

    Trigger examples: http://www.sqlusa.com/bestpractices2005/columnupdatetrigger/



    Kalman Toth Database & OLAP Architect SQL Server 2014 Design & Programming
    New Book / Kindle: Exam 70-461 Bootcamp: Querying Microsoft SQL Server 2012



    Saturday, May 3, 2014 8:27 PM