IF NOT EXISTS

  • How do I get it NOT to error out if login already exists?

    IF NOT EXISTS(SELECT principal_id FROM sys.database_principals WHERE name = '[mylogin]')

    BEGIN

    CREATE USER [mylogin] FOR LOGIN [mylogin]

    END

  • Script out a user from SSMS window, you will find necessary options in it.

  • Don't enclose the user name in square brackets:

    IF NOT EXISTS(SELECT principal_id FROM sys.database_principals WHERE name = 'mylogin')

    BEGIN

    CREATE USER [mylogin] FOR LOGIN [mylogin]

    END

    -- Gianluca Sartori

  • Yes, it was the brackets. Thank you!

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply