SQL Swiss Army Knife #2.1 - Scripting Logins - Updated
Published Mar 23 2019 11:32 AM 489 Views
First published on MSDN on Jun 21, 2010

EDIT (20-09-2012) - Added several scripting options; does not use SQL Server 2000 system tables anymore.
EDIT (11-01-2013) - Fixed issue with permission syntax depending on version (2005 vs. 2008/2012).
EDIT (18-03-2013) - Fixed issue with undeclared variable in cursor; Added options for access.

Hello all,

Here is another posting focusing on SQL scripts that may help on everyday DBA tasks, following the series "SQL Swiss Army Knife". This is another example of a script I've been using as a "local machine" bit of a wider DR strategy, in this case generating all logins and their respective server roles. Note that this does not deal with CERTIFICATE_MAPPED_LOGIN and ASYMMETRIC_KEY_MAPPED_LOGIN types.
These are the options available:

  • All users:
    EXEC usp_logins

  • One user, All DBs:
    EXEC usp_logins '<User>'

  • One user, One DB:
    EXEC usp_logins '<User>', '<DBName>'

  • All users, One DB:
    EXEC usp_logins NULL, '<DBName>'

Its output may simply be saved onto a .sql file and will resemble this:

So I hope some of you will find useful.

Until next time!

Download code here: usp_logins.sql

Disclaimer: I hope that the information on these pages is valuable to you. Your use of the information contained in these pages, however, is at your sole risk. All information on these pages is provided "as -is", without any warranty, whether express or implied, of its accuracy, completeness, fitness for a particular purpose, title or non-infringement, and none of the third-party products or information mentioned in the work are authored, recommended, supported or guaranteed by Ezequiel. Further, Ezequiel shall not be liable for any damages you may sustain by using this information, whether direct, indirect, special, incidental or consequential, even if it has been advised of the possibility of such damages.

Version history
Last update:
‎Mar 23 2019 11:32 AM
Updated by: