conditionally create user in SQL Server

You can consult the two system catalogs sys.server_principals to check for server logins, or sys.database_principals in your specific database for users of your database:

use myDB
GO

if not exists(select * from sys.database_principals where name="foo")
  -- create your database user
   

if not exists(select * from sys.server_principals where name="foo")
   -- you need to create a server login first

Marc

Leave a Comment

tech