Below
is a small snippet of cursor created to identify all the users who have
the flag of IsSQLUser except for SID not Null & SID <>
0X0 (because they dbo, Guest, Information_Schema & sys accounts) and
run Auto fix the orphaned users.
DECLARE @USRNAME VARCHAR(100), @COMMAND VARCHAR(100)
DECLARE CURSORS INSENSITIVE CURSOR FOR
SELECT NAME AS USERNAME FROM SYSUSERS
WHERE ISSQLUSER = 1 AND (SID IS NOT NULL AND SID <> 0X0)
AND SUSER_SNAME(SID) IS NULL ORDER BY NAME
FOR READ ONLY
OPEN CURSORS
FETCH NEXT FROM CURSORS INTO @USRNAME
WHILE @@FETCH_STATUS=0
BEGIN
SELECT @COMMAND='SP_CHANGE_USERS_LOGIN ''AUTO_FIX'', '''+@USRNAME+''' '
EXEC(@COMMAND)
FETCH NEXT FROM CURSORS INTO @USRNAME
END
CLOSE CURSORS
DEALLOCATE CURSORS.
DECLARE @USRNAME VARCHAR(100), @COMMAND VARCHAR(100)
DECLARE CURSORS INSENSITIVE CURSOR FOR
SELECT NAME AS USERNAME FROM SYSUSERS
WHERE ISSQLUSER = 1 AND (SID IS NOT NULL AND SID <> 0X0)
AND SUSER_SNAME(SID) IS NULL ORDER BY NAME
FOR READ ONLY
OPEN CURSORS
FETCH NEXT FROM CURSORS INTO @USRNAME
WHILE @@FETCH_STATUS=0
BEGIN
SELECT @COMMAND='SP_CHANGE_USERS_LOGIN ''AUTO_FIX'', '''+@USRNAME+''' '
EXEC(@COMMAND)
FETCH NEXT FROM CURSORS INTO @USRNAME
END
CLOSE CURSORS
DEALLOCATE CURSORS.
Hope it helps !! :)
No comments:
Post a Comment