NguyenL71
Posting Yak Master
228 Posts |
Posted - 2015-02-27 : 00:07:03
|
If a user belong to server role, I want to return unique server role. Pleasesee the result want below. I am using SQL 2012.Thank you so much.USE Tempdb;goDROP TABLE [dbo].[DBUser], [dbo].[ServerRole]CREATE TABLE [dbo].[DBUser]( [loginname] [varchar](15) NULL, [dbname] [varchar](15) NULL, [dbrole] [varchar](15) NULL, [typedesc] [varchar](15) NULL)GOCREATE TABLE [dbo].[ServerRole]( [serverrole] [varchar](15) NULL, [loginname] [varchar](15) NULL) ON [PRIMARY]GOINSERT INTO dbo.DBUser([loginname],[dbname],[dbrole],[typedesc]) VALUES ('Home\Peter', 'PC', 'db_ddladmin', 'WINDOWS_USER' ) ;INSERT INTO dbo.DBUser([loginname],[dbname],[dbrole],[typedesc]) VALUES ('Home\Peter', 'PC', 'db_datareader', 'WINDOWS_USER' ) ;INSERT INTO dbo.DBUser([loginname],[dbname],[dbrole],[typedesc]) VALUES ('Home\Peter', 'PC', 'db_datawriter', 'WINDOWS_USER' ) ;INSERT INTO dbo.DBUser([loginname],[dbname],[dbrole],[typedesc]) VALUES ('Home\John', 'CC', 'db_datareader', 'WINDOWS_USER' ) ;INSERT INTO dbo.DBUser([loginname],[dbname],[dbrole],[typedesc]) VALUES ('Home\John', 'CC', 'db_datawriter', 'WINDOWS_USER' ) ;INSERT INTO dbo.DBUser([loginname],[dbname],[dbrole],[typedesc]) VALUES ('Home\John', 'Dev', 'db_datareader', 'WINDOWS_USER' ) ;INSERT INTO dbo.DBUser([loginname],[dbname],[dbrole],[typedesc]) VALUES ('Robert', 'Dev', 'db_datareader', 'SQL_USER' ) ;INSERT INTO dbo.DBUser([loginname],[dbname],[dbrole],[typedesc]) VALUES ('Home\John', 'Dev', 'db_datawriter', 'WINDOWS_USER' ) ;INSERT INTO dbo.DBUser([loginname],[dbname],[dbrole],[typedesc]) VALUES ('Robert', 'Dev', 'db_datawriter', 'SQL_USER' ) ;INSERT INTO dbo.DBUser([loginname],[dbname],[dbrole],[typedesc]) VALUES ('Home\John', 'BC', 'db_owner', 'WINDOWS_USER' ) ;INSERT INTO dbo.DBUser([loginname],[dbname],[dbrole],[typedesc]) VALUES ('Home\John', 'BC', 'db_datareader', 'WINDOWS_USER' ) ;INSERT INTO dbo.DBUser([loginname],[dbname],[dbrole],[typedesc]) VALUES ('Rober', 'BC', 'db_datareader', 'SQL_USER' ) ;INSERT INTO dbo.DBUser([loginname],[dbname],[dbrole],[typedesc]) VALUES ('Home\John', 'BC', 'db_datawriter', 'WINDOWS_USER' ) ;goINSERT INTO dbo.ServerRole([serverrole],[loginname]) VALUES ('sysadmin', 'Home\John' ) ;INSERT INTO dbo.ServerRole([serverrole],[loginname]) VALUES ('sysadmin', 'Robert' ) ;INSERT INTO dbo.ServerRole([serverrole],[loginname]) VALUES ('securityadmin', 'Robert' ) ;INSERT INTO dbo.ServerRole([serverrole],[loginname]) VALUES ('serveradmin', 'Home\John' ) ;INSERT INTO dbo.ServerRole([serverrole],[loginname]) VALUES ('setupadmin', 'Home\John' ) ;go SELECT * FROM DBUser; GO SELECT * FROM ServerRole; GO loginname dbname dbrole typedesc--------------- --------------- --------------- ---------------Home\Peter PC db_ddladmin WINDOWS_USERHome\Peter PC db_datareader WINDOWS_USERHome\Peter PC db_datawriter WINDOWS_USERHome\John CC db_datareader WINDOWS_USERHome\John CC db_datawriter WINDOWS_USERHome\John Dev db_datareader WINDOWS_USERRobert Dev db_datareader SQL_USERHome\John Dev db_datawriter WINDOWS_USERRobert Dev db_datawriter SQL_USERHome\John BC db_owner WINDOWS_USERHome\John BC db_datareader WINDOWS_USERRober BC db_datareader SQL_USERHome\John BC db_datawriter WINDOWS_USERserverrole loginname--------------- ---------------sysadmin Home\Johnsysadmin Robertsecurityadmin Robertserveradmin Home\Johnsetupadmin Home\John SELECT a.loginname ,a.dbname ,a.dbrole ,b.serverrole FROM DBUser AS a JOIN ServerRole AS b ON a.loginname = b.loginname; GO-- Result want: loginname dbname dbrole serverrole--------------- --------------- --------------- ---------------Home\John CC db_datareader sysadminHome\John CC db_datawriter serveradminHome\John Dev db_datareader setupadminHome\John Dev db_datawriter Home\John BC db_owner Home\John BC db_datareader Home\John BC db_datawriter Robert Dev db_datareader sysadminRobert Dev db_datawriter securityadminRobert Dev db_datareader Robert Dev db_datawriter Home\John CC db_datareader Home\John CC db_datawriter Home\John Dev db_datareader Home\John Dev db_datawriter Home\John BC db_owner Home\John BC db_datareader Home\John BC db_datawriter Home\John CC db_datareader Home\John CC db_datawriter Home\John Dev db_datareader Home\John Dev db_datawriter Home\John BC db_owner Home\John BC db_datareader Home\John BC db_datawriter |
|