title: Insert trigger for duplicate records not working sql server
body: <pre><code>ALTER TRIGGER [dbo].[STOK_HARKETLERI_Insert]    
  ON [dbo].[STOK_HAREKETLERI]              
  FOR INSERT 
  AS BEGIN  
      declare @tip int 
      declare @miktar float 
      declare @stokkod nvarchar 
      declare @tarih datetime 
      declare @counter int  
  
      Select 
          @tip = sth_tip,  @miktar = sth_miktar,  
          @stokkod = sth_stok_kod, @tarih = sth_tarih 
      from inserted 
  
      select @Counter = COUNT(sth_tip) 
      from STOK_HAREKETLERI  
      where sth_evraktip = 6 
        and sth_tip = @tip 
        and sth_miktar = @miktar 
        and @stokkod = sth_stok_kod 
        and @tarih = sth_tarih   
  
      if (@counter&gt;=1)  
      begin     
         rollback     
         RAISERROR ('Record already exists', 17, -1) with log 
      end    
  END
  GO
  </code></pre>
  
  <p>The trigger is not being triggered on insert statements, however if I remove the variables and fill the data and run it on SQL Server it is running fine.</p>
  
  <p>Any suggestions?</p>
  
  <p>One more thing if I change the line <code>(@counter &gt;= 1)</code> to <code>(@counter &gt;= 0)</code> it starts working again.</p>