USE [Arizona];
GO

IF EXISTS 
    (SELECT name  
     FROM master.sys.server_principals
     WHERE name = 'apscounter')
BEGIN

IF database_principal_id('apscounter') is not null
  BEGIN
    ALTER USER apscounter WITH LOGIN=apscounter
  END
END

IF EXISTS 
    (SELECT name  
     FROM master.sys.server_principals
     WHERE name = 'arizonacash')
BEGIN

IF database_principal_id('arizonacash') is not null
  BEGIN
    ALTER USER arizonacash WITH LOGIN=arizonacash
  END
END

IF EXISTS 
    (SELECT name  
     FROM master.sys.server_principals
     WHERE name = 'bmcarizona')
BEGIN

IF database_principal_id('bmcarizona') is not null
  BEGIN
    ALTER USER bmcarizona WITH LOGIN=bmcarizona
  END
END

IF EXISTS 
    (SELECT name  
     FROM master.sys.server_principals
     WHERE name = 'datamonitoring')
BEGIN

IF database_principal_id('datamonitoring') is not null
  BEGIN
    ALTER USER datamonitoring WITH LOGIN=datamonitoring
  END
END

IF EXISTS 
    (SELECT name  
     FROM master.sys.server_principals
     WHERE name = 'extraction')
BEGIN

IF database_principal_id('extraction') is not null
  BEGIN
    ALTER USER extraction WITH LOGIN=extraction
  END
END

IF EXISTS 
    (SELECT name  
     FROM master.sys.server_principals
     WHERE name = 'gcstock')
BEGIN

IF database_principal_id('gcstock') is not null
  BEGIN
    ALTER USER gcstock WITH LOGIN=gcstock
  END
END


USE [ArizonaCUST];
GO

IF EXISTS 
    (SELECT name  
     FROM master.sys.server_principals
     WHERE name = 'extraction')
BEGIN

IF database_principal_id('extraction') is not null
  BEGIN
    ALTER USER extraction WITH LOGIN=extraction
  END
END


USE [ArizonaLD];
GO

IF EXISTS 
    (SELECT name  
     FROM master.sys.server_principals
     WHERE name = 'extraction')
BEGIN

IF database_principal_id('extraction') is not null
  BEGIN
    ALTER USER extraction WITH LOGIN=extraction
  END
END


USE [HCITools]
GO

IF EXISTS 
    (SELECT name  
     FROM master.sys.server_principals
     WHERE name = 'datamonitoring')
BEGIN

IF database_principal_id('datamonitoring') is not null
  BEGIN
    ALTER USER datamonitoring WITH LOGIN=datamonitoring
  END
END

IF EXISTS 
    (SELECT name  
     FROM master.sys.server_principals
     WHERE name = 'sqlMonPerfUsr')
BEGIN

IF database_principal_id('sqlMonPerfUsr') is not null
  BEGIN
    ALTER USER sqlMonPerfUsr WITH LOGIN=sqlMonPerfUsr
  END
END

IF EXISTS 
    (SELECT name  
     FROM master.sys.server_principals
     WHERE name = 'dba_monitoring')
BEGIN

IF database_principal_id('dba_monitoring') is not null
  BEGIN
    ALTER USER dba_monitoring WITH LOGIN=dba_monitoring
  END
END


USE [ActivePos_server]
GO

IF EXISTS 
    (SELECT name  
     FROM master.sys.server_principals
     WHERE name = 'ActiveConfig')
BEGIN

IF database_principal_id('ActiveConfig') is not null
  BEGIN
    ALTER USER ActiveConfig WITH LOGIN=ActiveConfig
  END
END


USE [ActivePos_write]
GO

IF EXISTS 
    (SELECT name  
     FROM master.sys.server_principals
     WHERE name = 'ActiveConfig')
BEGIN

IF database_principal_id('ActiveConfig') is not null
  BEGIN
    ALTER USER ActiveConfig WITH LOGIN=ActiveConfig
  END
END

USE [ActiveSystemClient]
GO

IF EXISTS 
    (SELECT name  
     FROM master.sys.server_principals
     WHERE name = 'ActiveConfig')
BEGIN

IF database_principal_id('ActiveConfig') is not null
  BEGIN
    ALTER USER ActiveConfig WITH LOGIN=ActiveConfig
  END
END

USE [ActiveSystemServer]
GO

IF EXISTS 
    (SELECT name  
     FROM master.sys.server_principals
     WHERE name = 'ActiveConfig')
BEGIN

IF database_principal_id('ActiveConfig') is not null
  BEGIN
    ALTER USER ActiveConfig WITH LOGIN=ActiveConfig
  END
END

USE [MSDB]
GO

IF NOT EXISTS 
    (SELECT name  
     FROM sys.sysusers
     WHERE name = 'datamonitoring')
BEGIN
  CREATE USER [datamonitoring] FOR LOGIN [datamonitoring]
END

IF EXISTS 
    (SELECT name  
     FROM master.sys.server_principals
     WHERE name = 'datamonitoring')
BEGIN

IF database_principal_id('datamonitoring') is not null
  BEGIN
    ALTER USER [datamonitoring] WITH LOGIN=[datamonitoring]
    ALTER USER [datamonitoring] WITH DEFAULT_SCHEMA=[dbo]
  END
END

GRANT EXECUTE ON [dbo].[agent_datetime] TO [datamonitoring] 
GRANT SELECT ON [dbo].[sysjobactivity] TO [datamonitoring] 
GRANT SELECT ON [dbo].[sysjobhistory] TO [datamonitoring] 
GRANT SELECT ON [dbo].[sysjobs] TO [datamonitoring] 
GRANT SELECT ON [dbo].[sysjobschedules] TO [datamonitoring] 
GRANT SELECT ON [dbo].[sysschedules] TO [datamonitoring] 
