martes, 17 de mayo de 2016

SQL SERVER: crear usuario y login si no existen

IF NOT EXISTS 
    (SELECT name  
     FROM master.sys.server_principals
     WHERE name = 'ff')
BEGIN
    CREATE LOGIN ff WITH PASSWORD = N'ff', CHECK_POLICY = OFF
END
GO
IF NOT EXISTS 
    (SELECT 1   
     FROM DBO.SYSUSERS
     WHERE name = 'ff')
BEGIN
CREATE USER ff FROM LOGIN ff
EXEC SP_ADDROLEMEMBER 'db_datawriter', 'ff'
EXEC SP_ADDROLEMEMBER 'db_datareader', 'ff'
END
GO