title: MS SQL Server trigger to add records in a table
body: <p>I am new to sql server and T-SQL. I have two databases (dbA and dbB) on a same microsoft sql server. These two databases have a table with exactly the same fields i.e. dbA.dbo.fldA, dbA.dbo.fldB, dbA.dbo.fldC, dbB.dbo.fldA, dbB.dbo.fldB and dbB.dbo.fldC. Whenever there is a new record added in dbA meeting a condition (e.g. dbA.dbo.fldC &lt;> 0) , i want to have a trigger which automatically adds this record in dbB. But not the other way around, if a record is added in dbB it should not automatically be copied over to dbA. </p>
  
  <p>This is my current trigger. But I am not sure how I should active it. </p>
  
  <pre><code>CREATE TRIGGER tradesToOps
     ON  dbInv.dbo.tblDatTradesToday
     AFTER  INSERT,DELETE,UPDATE
  AS 
  BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;
  
       DECLARE @INSERT INT, @DELETE INT
  
      SET @INSERT = (SELECT COUNT(*) FROM INSERTED)
     SET @DELETE = (SELECT COUNT(*) FROM DELETED)
  
  
      -- when Insert happens
      IF @INSERT = 1 AND @DELETE = 0
      BEGIN
  
          INSERT INTO dbOps.dbo.tblDatTrades
          SELECT * FROM INSERTED
  
      END
  
     -- Insert statements for trigger here
  
  END
  </code></pre>
  
  <p>Best, </p>
  
  <p>Manu</p>