• aruopna (1/24/2014)


    Do you mean to say you need something like this? Try executing this piece (in text mode) on your database, it will generate create user scripts only for those users without a login.

    SET NOCOUNT ON

    DECLARE @name_holder VARCHAR(255)

    DECLARE my_cursor CURSOR FOR

    SELECT name

    FROM sys.database_principals

    WHERE Datalength(sid) >= 28

    AND sid NOT IN (SELECT sid

    FROM sys.server_principals)

    AND type = 'S'

    AND principal_id > 4

    OPEN my_cursor

    FETCH next FROM my_cursor INTO @name_holder

    WHILE ( @@FETCH_STATUS <> -1 )

    BEGIN

    SELECT

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

    + Space(1) + '''' + name + '''' + ') BEGIN CREATE USER '

    + Space(1) + Quotename([name])

    + ' WITHOUT LOGIN WITH DEFAULT_SCHEMA = '

    + Quotename([default_schema_name]) + Space(1)

    + 'END; '

    FROM sys.database_principals AS rm

    WHERE [type] IN ( 'U', 'S', 'G' )

    AND name = @name_holder

    FETCH next FROM my_cursor INTO @name_holder

    END

    CLOSE my_cursor

    DEALLOCATE my_cursor

    Thanks for this. I'll evaluate adding it to the script. I didn't adjust the script for contained DB users, obvioiusly. I'll see what I can do for getting an update out for that.

    Appreciate you asking, Srizwanh, as well.

    Steve