title: Check if a row exists, otherwise insert
body: <p>I need to write a T-SQL stored procedure that updates a row in a table. If the row doesn't exist, insert it. All this steps wrapped by a transaction. </p>
  
  <p>This is for a booking system, so it must be <strong>atomic and reliable</strong>. It must return true if the transaction was committed and the flight booked.</p>
  
  <p>I'm <strong>new to T-SQL</strong>, and not sure on how to use <code>@@rowcount</code>. This is what I've written until now. Am I on the right road? I'm sure is an easy problem for you.</p>
  
  <pre><code>-- BEGIN TRANSACTION (HOW TO DO?)
  
  UPDATE Bookings
   SET TicketsBooked = TicketsBooked + @TicketsToBook
   WHERE FlightId = @Id AND TicketsMax &lt; (TicketsBooked + @TicketsToBook)
  
  -- Here I need to insert only if the row doesn't exists.
  -- If the row exists but the condition TicketsMax is violated, I must not insert 
  -- the row and return FALSE
  
  IF @@ROWCOUNT = 0 
  BEGIN
  
   INSERT INTO Bookings ... (omitted)
  
  END
  
  -- END TRANSACTION (HOW TO DO?)
  
  -- Return TRUE (How to do?)
  </code></pre>