INSERT INTO if not exists SQL server

前端 未结 8 523
无人共我
无人共我 2020-12-05 11:45

I have a database structured as follows:

users

userid (Primary Key)
username

group



        
8条回答
  •  南方客
    南方客 (楼主)
    2020-12-05 12:10

    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.

提交回复
热议问题