title: WHERE clause with INSERT statement, "Incorrect Syntax" at the WHERE, and IF NOT EXISTS running regardless of if record exists
body: <p>I have this INSERT query:</p>
  
  <pre><code>INSERT INTO Appointments (StaffID, CustomerID, TimeSlot, AppDate) 
  VALUES (1, 11, 1, DATEADD(day, 1, GETDATE()))
  WHERE NOT EXISTS 
      (SELECT * FROM Appointments 
       WHERE StaffID = 1 AND CustomerID = 11 AND TimeSlot = 1 AND AppDate = DATEADD(day, 1, GETDATE()));
  </code></pre>
  
  <p>Yet it says "Incorrect syntax near the keyword 'WHERE'" </p>
  
  <p>This is very basic error, but have no clue whats up. </p>
  
  <p>I have also tried using IF NOT EXISTS, which gives no syntax errors, but also doesn't work, preforming the insert even though the record with the values does already exist:</p>
  
  <pre><code>IF NOT EXISTS 
      (
          SELECT * FROM Appointments 
          WHERE StaffID = 1 AND CustomerID = 11 AND TimeSlot = 1 AND AppDate = DATEADD(day, 1, GETDATE())
      )
      BEGIN
          INSERT INTO Appointments (StaffID, CustomerID, TimeSlot, AppDate) 
          VALUES (1, 11, 1, DATEADD(day, 1, GETDATE()))
  END;
  </code></pre>
  
  <p>The record with the values 1, 11, 1, and tomorrow does 100% already exist, yet inserts it again. </p>