title: How to fix If condition with rollback transaction in Trigger in SQL Server
body: <p>I'm doing my homework and i'm trying to create a trigger with if condition. But that Trigger doesn't work properly.</p>
  
  <p>This is for my homework. Im using Northwind Database in my training. </p>
  
  <p>Our teacher has asked us to create a trigger in Products table. Her question is : </p>
  
  <p>"While inserting data into a Product Table; If inserted data has the same product name in Products table, cancel this transaction and print this warn message : 'That product name has already in my table' warning'" </p>
  
  <p>Here is my code:</p>
  
  <pre><code>create trigger AyniUrun
  on products
  after insert
  as
  begin
      declare @insertedProductName nvarchar (250) =  (select ProductName from inserted)
      if exists (select ProductName from Products where ProductName = @insertedProductName)
          begin
              print 'AYNI İSİMDE ÜRÜN VAR!'
              rollback
          end 
  end
  </code></pre>
  
  <p>When I try to insert the same named product into Products Table, it gives me that message : "The transaction ended in the trigger. The batch has been aborted.". This is what i want in this condition. </p>
  
  <p>But when I try to insert a different named product into Products Table, it also gives me same message and terminates my transaction. Where did i go wrong ?</p>