tSQL: adding login/user

Feb 25, 2012 at 12:57 AM

IF NOT EXISTS (SELECT * FROM master.dbo.syslogins WHERE [name]='web_man_editor')
BEGIN
    PRINT 'Adding login...'
    EXECUTE sp_addlogin @loginame='web_man_editor', @passwd='xxx___000999wed', @defdb='pubs'
END
ELSE
BEGIN
    PRINT 'Login already in server.'
END

IF NOT EXISTS(SELECT * FROM sysusers WHERE name = 'web_man_editor')
BEGIN
    PRINT 'Adding user to database...'
    EXECUTE sp_grantdbaccess 'web_man_editor'
    EXECUTE sp_addrolemember 'db_datareader','web_man_editor'
    EXECUTE sp_addrolemember 'db_datawriter','web_man_editor'
    --EXECUTE sp_addrolemember 'dbWebSolutionsUsers','web_man_editor'
END
ELSE
BEGIN
    PRINT 'User already in database. Verifying user map to login...'
    EXECUTE sp_change_users_login 'update_one','web_man_editor','web_man_editor'
END
PRINT 'End of procedure.'