I have a user which can login in sql server. Now i need to add user in database, however i am not sure if that user already exists in database or not.
So i need script, which checks if the user exists in database or not, and if doesn’t it should add that user in that database.
How can i do that?
I think this worked:
IF DATABASE_PRINCIPAL_ID(‘login’) IS NULL
BEGIN
CREATE USER [username] FOR LOGIN [login]
END
^ this only works if the username in database is same as login.