mercredi 4 novembre 2015

How to use dynamic SQL in SQL Server 2005 to alter login

I have a script that works fine in SQL Server 2008R2 but it couldn't work in SQL Server 2005. Is there any way that can work for both version?

Thanks!

declare @SQL nvarchar(max)  = ''

select @SQL = @SQL + 'ALTER LOGIN ' + QUOTENAME(DisabledName) + ' DISABLE;'
from [SysAdmin].[dbo].[DisabledAccountHistory]
where DisabledName in (select name 
                       from sys.server_principals 
                       where is_disabled = 0)
group by DisabledName

exec sp_executesql @SQL

Aucun commentaire:

Enregistrer un commentaire