insert script_users into table scripts

Be sure to manually create a script to recreate logins with passwords and store it in a very safe place!


DELETE scripts WHERE name = 'script_users'
GO
INSERT scripts 
VALUES('script_users','DECLARE @use VARCHAR(50),')
INSERT scripts 
VALUES('script_users','        @line VARCHAR(255)')
INSERT scripts 
VALUES('script_users','                                SET NOCOUNT ON')
INSERT scripts 
VALUES('script_users','SELECT @use = "USE " +')
INSERT scripts 
VALUES('script_users','              (SELECT name FROM master..sysdatabases')
INSERT scripts 
VALUES('script_users','	       WHERE dbid = (SELECT dbid')
INSERT scripts 
VALUES('script_users','                             FROM master..sysprocesses')
INSERT scripts 
VALUES('script_users','	                     WHERE spid = @@SPID))')
INSERT scripts 
VALUES('script_users','PRINT @use')
INSERT scripts 
VALUES('script_users','PRINT "GO"')
INSERT scripts 
VALUES('script_users','PRINT "/*============ sp_helpuser ============"')
INSERT scripts 
VALUES('script_users','PRINT "Compare this to the sp_helpuser output"')
INSERT scripts 
VALUES('script_users','PRINT "at the end of the results when this"')
INSERT scripts 
VALUES('script_users','PRINT "script is run. They should be identical."')
INSERT scripts 
VALUES('script_users','PRINT ""')
INSERT scripts 
VALUES('script_users','EXEC sp_helpuser')
INSERT scripts 
VALUES('script_users','PRINT "=======================================*/"')
INSERT scripts 
VALUES('script_users','PRINT "/****** declare variables ******/"')
INSERT scripts 
VALUES('script_users','PRINT "DECLARE @cmd VARCHAR(255),"')
INSERT scripts 
VALUES('script_users','PRINT "        @lastsuid INT,"')
INSERT scripts 
VALUES('script_users','PRINT "        @suid INT,"')
INSERT scripts 
VALUES('script_users','PRINT "        @lastuid INT,"')
INSERT scripts 
VALUES('script_users','PRINT "        @uid INT"')
INSERT scripts 
VALUES('script_users','PRINT "SET NOCOUNT ON"')
INSERT scripts 
VALUES('script_users','PRINT "/****** drop existing alias ******/"')
INSERT scripts 
VALUES('script_users','PRINT "SELECT @lastsuid = 0"')
INSERT scripts 
VALUES('script_users','PRINT "WHILE (SELECT MIN(suid) FROM sysalternates"')
INSERT scripts 
VALUES('script_users','PRINT "       WHERE suid > @lastsuid) IS NOT NULL"')
INSERT scripts 
VALUES('script_users','PRINT "  BEGIN"')
INSERT scripts 
VALUES('script_users','PRINT "    SELECT @suid = (SELECT MIN(suid) FROM sysalternates"')
INSERT scripts 
VALUES('script_users','PRINT "                    WHERE suid > @lastsuid)"')
INSERT scripts 
VALUES('script_users','SELECT @line = "SELECT @cmd = " + CHAR(39) + "sp_dropalias" + CHAR(39) + " +"')
INSERT scripts 
VALUES('script_users','PRINT @line')
INSERT scripts 
VALUES('script_users','PRINT "                   (SELECT name FROM master..syslogins"')
INSERT scripts 
VALUES('script_users','PRINT "                   WHERE suid = @suid)"')
INSERT scripts 
VALUES('script_users','PRINT "    EXECUTE (@cmd)"')
INSERT scripts 
VALUES('script_users','PRINT "    SELECT @lastsuid = @suid"')
INSERT scripts 
VALUES('script_users','PRINT "  END"')
INSERT scripts 
VALUES('script_users','PRINT "/****** drop existing users ******/"')
INSERT scripts 
VALUES('script_users','PRINT "SELECT @lastuid = 0"')
INSERT scripts 
VALUES('script_users','PRINT "WHILE (SELECT MIN(uid) FROM sysusers"')
INSERT scripts 
VALUES('script_users','PRINT "       WHERE uid > @lastuid) IS NOT NULL"')
INSERT scripts 
VALUES('script_users','PRINT "  BEGIN"')
INSERT scripts 
VALUES('script_users','PRINT "    SELECT @uid = (SELECT MIN(uid) FROM sysusers"')
INSERT scripts 
VALUES('script_users','PRINT "                   WHERE uid > @lastuid"')
INSERT scripts 
VALUES('script_users','SELECT  @line = "       AND name <> " + CHAR(39) + "dbo" + CHAR(39) + ")"')
INSERT scripts 
VALUES('script_users','PRINT @line')
INSERT scripts 
VALUES('script_users','SELECT @line = "SELECT @cmd = " + CHAR(39) + "sp_dropuser " + CHAR(39) + " + "')
INSERT scripts 
VALUES('script_users','PRINT @line')
INSERT scripts 
VALUES('script_users','PRINT "                   (SELECT name FROM sysusers"')
INSERT scripts 
VALUES('script_users','PRINT "                   WHERE uid = @uid)"')
INSERT scripts 
VALUES('script_users','PRINT "    EXECUTE (@cmd)"')
INSERT scripts 
VALUES('script_users','PRINT "    SELECT @lastuid = @uid"')
INSERT scripts 
VALUES('script_users','PRINT "  END"')
INSERT scripts 
VALUES('script_users','PRINT "/************** add users *******/"')
INSERT scripts 
VALUES('script_users','SELECT "EXECUTE sp_adduser " + RTRIM(u.name) + ", " + RTRIM(l.name)')
INSERT scripts 
VALUES('script_users','FROM sysusers u,')
INSERT scripts 
VALUES('script_users','     master..syslogins l')
INSERT scripts 
VALUES('script_users','WHERE l.suid = u.suid')
INSERT scripts 
VALUES('script_users','AND u.name <> "dbo"')
INSERT scripts 
VALUES('script_users','PRINT "/************** add alias *******/"')
INSERT scripts 
VALUES('script_users','SELECT "EXECUTE sp_addalias " + RTRIM(l.name) + ", " + RTRIM(u.name)')
INSERT scripts 
VALUES('script_users','FROM master..syslogins l,')
INSERT scripts 
VALUES('script_users','     sysusers u,')
INSERT scripts 
VALUES('script_users','     sysalternates a')
INSERT scripts 
VALUES('script_users','WHERE u.suid = a.altsuid')
INSERT scripts 
VALUES('script_users','AND l.suid = a.suid')
INSERT scripts 
VALUES('script_users','AND a.suid IN (SELECT suid FROM master..syslogins)')
INSERT scripts 
VALUES('script_users','PRINT "GO"')
INSERT scripts 
VALUES('script_users','PRINT "sp_helpuser"')
INSERT scripts 
VALUES('script_users','PRINT "GO"')