INSERT INTO if not exists SQL server

Or using the new MERGE syntax:

merge into users u
using ( 
   select 'username' as uname
) t on t.uname = u.username
when not matched then 
  insert (username) values (t.uname);

Basically you can do it like this:

IF NOT EXISTS (SELECT * FROM USER WHERE username = @username)
    INSERT INTO users (username) VALUES (@username)

But seriously, how you're going to know if user visited your website for the first time? You have to insert records in table user, when somebody register on your website, not login.


IF NOT EXISTS (select * from users where username = 'username')
BEGIN
    INSERT INTO ...
END