Skip to posts
  • Publications
  • Code Library
  • Events
  • Presentations
  • Who Am I?
  • Copyright

Wayne Sheffield

My blog about SQL Server
  • Home
  • Publications
  • Code Library
  • Events
  • Presentations
  • Who Am I?
  • Copyright
  • Latest Posts
  • Latest Comments


MCM


MVP

(2017-2022)

Top 50 SQL blog 2018

Recent Posts

  • Availability Group issues fixed with Alerts
  • Using Google Chrome with SSRS
  • Speakers wanted for the Richmond (VA) SQL Server Users Group – 2020
  • T-SQL Tuesday #120 – Recap
  • T-SQL Tuesday #120 – What were you thinking? – Invitation

Top Posts

  • Working with tabs and spaces in SSMS (Day 34)
  • Using a gMSA with SQL Server
  • Checking for temporary table existence
  • A Page Split in SQL Server - the Good, the Nasty and the Smart
  • T-SQL Tuesday #115 - Notes to 20 year old Wayne
  • Are CHECKSUMs being performed with a Compressed Backup?
  • T-SQL Tuesday #61 - The Wrap-Up
  • Working with SQLSaturday SpeedPASSes
  • How to improve SQL Server's IO performance by up to 40%
  • I'm a winner!

Archives

Categories

Tags

2020 absolute values (ABS()) Advanced Certifications analytic functions assign sequential number automation Availability Group Back To Basics case-sensitive CLR String Security clustered index common table expression Data Movement deadlock analysis Denali Double-Hop efficiency extracting data from strings Failover Functions Google Chrome import file index analysis myth order by Performance physical tally table Quirky Update Scalar UDF Inlining split string splitting strings SQL SQL 2019 SQL Server SSMS SSMS Tips SSRS String Splitter T-SQL Tuesday tally table Trusted Assembly TSQL Tuesday virtual tally table Why do you do what you do? wrapup

Blogroll

  • Jen McCown
  • Phil Factor
  • Kendra Little
  • Jonathan Kehayias
  • Jorge Segarra
  • Jason Brimhall
  • Michael Swart
  • Gail Shaw
  • Brad Schulz
  • Adam Machanic
  • Tim Ford
  • Kimberly Tripp
  • Pinal Dave
  • Gianluca Sartori
  • Lynn Pettis

sp_help_revlogin_serverroles

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
CREATE PROCEDURE dbo.sp_help_revlogin_serverroles
AS
/*******************************************************************************
Script out server roles (SQL 2012+).
Script out adding logins to server roles.
    SQL 2012+: Use ALTER SERVER ROLE
    Prior:     Use master..sp_addsrvrolemember
********************************************************************************
                                MODIFICATION LOG
********************************************************************************
2012-10-17 WGS Initial Creation.
2014-01-08 WGS Proper quoting in the print statements.
*******************************************************************************/
DECLARE @SqlCmd VARCHAR(MAX);
 
-- Create the server roles if they don't exist.
-- SQL 2012 and higher
IF CONVERT(INT, PARSENAME(CONVERT(VARCHAR(20), SERVERPROPERTY('ProductVersion')), 4)) >= 11 -- SQL 2012 or higher
SET @SqlCmd =
'DECLARE @SqlCmd VARCHAR(1000);
PRINT ''--Ensuring server roles exist...'';
DECLARE cServerRoles CURSOR FOR
SELECT ''IF NOT EXISTS (SELECT 1 FROM sys.server_principals WHERE name = '''''' + name + '''''' AND type = ''''R'''')
   AND CONVERT(INT, PARSENAME(CONVERT(VARCHAR(20), SERVERPROPERTY(''''ProductVersion'''')), 4)) >= 11
BEGIN
    PRINT ''''Adding Server Role: '' + QUOTENAME(name) + '''''';
    EXECUTE (''''CREATE SERVER ROLE '' + QUOTENAME(name) + ISNULL(ca1.Auth, '''') + '''''');
END;
 
''
FROM sys.server_principals sp1
     OUTER APPLY (SELECT '' AUTHORIZATION '' + QUOTENAME(name) FROM sys.server_principals WHERE principal_id = sp1.owning_principal_id) ca1(Auth)
WHERE type = ''R''
ORDER BY name;
OPEN cServerRoles;
FETCH NEXT FROM cServerRoles INTO @SqlCmd;
WHILE @@FETCH_STATUS = 0
BEGIN
    PRINT @SqlCmd;
    FETCH NEXT FROM cServerRoles INTO @SqlCmd;
END;
 
CLOSE cServerRoles;
DEALLOCATE cServerRoles;
'
-- PRINT @SqlCmd;
EXECUTE (@SqlCmd);
 
-- add logins to the roles
PRINT '
 
--Now add logins to the server roles';
DECLARE cLoginRoles CURSOR FOR
SELECT AddLoginToRoleCmd = 'IF EXISTS (SELECT 1 FROM sys.server_principals WHERE name = ''' + ca1.RoleName + ''' AND type = ''R'')
   AND EXISTS (SELECT 1 FROM sys.server_principals WHERE name = ''' + ca2.LoginName + ''')
BEGIN
     PRINT ''Adding login ''''' + ca2.LoginName + ''''' to server role ''''' + ca1.RoleName + ''''''';
     IF CONVERT(INT, PARSENAME(CONVERT(VARCHAR(20), SERVERPROPERTY(''ProductVersion'')),4)) >= 11
        EXECUTE (''ALTER SERVER ROLE ' + QUOTENAME(ca1.RoleName) + ' ADD MEMBER ' + QUOTENAME(ca2.LoginName) + ''');
     ELSE
     EXECUTE master..sp_addsrvrolemember @loginame = N''' + ca2.LoginName + ''', @rolename = N''' + ca1.RoleName + ''';
END
ELSE
BEGIN
    PRINT ''Cannot add login ''''' + ca2.LoginName + ''''' to role ''''' + ca1.RoleName + ''''' (either login or role does not exist)''
END;
 
'
FROM sys.server_role_members srm
    CROSS APPLY (SELECT name FROM sys.server_principals WHERE principal_id = srm.role_principal_id) ca1 (RoleName)
    CROSS APPLY (SELECT name FROM sys.server_principals WHERE principal_id = srm.member_principal_id) ca2 (LoginName)
ORDER BY ca2.LoginName, ca1.RoleName;
 
OPEN cLoginRoles;
FETCH NEXT FROM cLoginRoles INTO @SqlCmd;
WHILE @@FETCH_STATUS = 0
BEGIN
    PRINT @SqlCmd;
    FETCH NEXT FROM cLoginRoles INTO @SqlCmd;
END;
 
CLOSE cLoginRoles;
DEALLOCATE cLoginRoles;
GO

Share this:

  • Share on X (Opens in new window) X
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Email a link to a friend (Opens in new window) Email
  • Print (Opens in new window) Print
  • More
  • Share on Facebook (Opens in new window) Facebook
  • Share on Pinterest (Opens in new window) Pinterest
  • Share on Reddit (Opens in new window) Reddit

Like this:

Like Loading...
  • Comments (0)
  • Trackbacks (0)
Leave a comment Trackback

No one has commented yet.

No trackbacks yet.

Leave a Reply to let me know how you liked this postCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Powered by WordPress Web Design by SRS Solutions © 2026 Wayne Sheffield Design by SRS Solutions
Manage Cookie Consent
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
View preferences
  • {title}
  • {title}
  • {title}
%d