INSERT INTO if not exists SQL server

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

I have a database structured as follows:

users

userid (Primary Key)
username

group



        
8条回答
  •  不思量自难忘°
    2020-12-05 12:02

    Here is a (probably oversimplified) example that addresses your issue. Note that it is always best to use parameters to prevent injection attacks, especially when verifying users.

    CREATE PROCEDURE AddUser
      @username varchar(20)
    AS
      IF NOT EXISTS(SELECT username FROM users WHERE username=@username)
        INSERT INTO users(username) VALUES (@username)
    

提交回复
热议问题