sys.server_principals
是您的朋友,因为它公开了Windows版本的SID。
请参考Aaron的解决方案:SQL Server SID和Windows SID之间的映射
为了完整起见,下面是代码:
CREATE TABLE dbo.TinyNumbers(Number TINYINT PRIMARY KEY);
INSERT dbo.TinyNumbers(Number)
SELECT TOP (256) ROW_NUMBER() OVER (ORDER BY number)-1
FROM master.dbo.spt_values;
CREATE FUNCTION dbo.GetWindowsSID
(
@sid VARBINARY(85)
)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN
(
SELECT ADsid = STUFF((SELECT '-' + part FROM
(
SELECT Number = -1, part = 'S-'
+ CONVERT(VARCHAR(30),CONVERT(TINYINT,CONVERT(VARBINARY(30),LEFT(@sid,1))))
+ '-'
+ CONVERT(VARCHAR(30),CONVERT(INT,CONVERT(VARBINARY(30),SUBSTRING(@sid,3,6))))
UNION ALL
SELECT TOP ((LEN(@sid)-5)/4) Number,
part = CONVERT(VARCHAR(30),CONVERT(BIGINT,CONVERT(VARBINARY(30),
REVERSE(CONVERT(VARBINARY(30),SUBSTRING(@sid,9+Number*4,4))))))
FROM dbo.TinyNumbers ORDER BY Number
) AS x ORDER BY Number
FOR XML PATH(''), TYPE).value(N'.[1]','nvarchar(max)'),1,1,'')
);
GO
CREATE VIEW dbo.server_principal_sids
AS
SELECT sp.name, sp.[sid], ad.ADsid, sp.type_desc
FROM sys.server_principals AS sp
CROSS APPLY dbo.GetWindowsSID(sp.[sid]) AS ad
WHERE [type] IN ('U','G')
AND LEN([sid]) % 4 = 0;
-- select the data
SELECT name,[sid],ADSid,type_desc FROM dbo.server_principal_sids;