我不是 T-SQL 方面的专家,但是,这可能会满足您的期望。
declare @Proc nvarchar(50)
declare @RowCnt int
declare @MaxRows int
declare @ExecSql nvarchar(255)
select @RowCnt = 1
select @Proc = 'SELECT * from fn_my_permissions'
declare @Import table (rownum int IDENTITY (1, 1) Primary key NOT NULL , TableName varchar(50))
insert into @Import (TableName) select name from sys.Tables
declare @Output table (entity_name varchar(50), subentity_name varchar(50), permission_name varchar(50))
select @MaxRows=count(*) from @Import
while @RowCnt <= @MaxRows
begin
select @ExecSql = @Proc + '(N''' + TableName + ''', N''OBJECT'') where subentity_name = ''''' from @Import where rownum = @RowCnt
insert into @Output exec sp_executesql @ExecSql
Select @RowCnt = @RowCnt + 1
end
select * from @Output
这将为当前用户提供权限。如果您想查找给定用户的权限,请尝试以下操作;
EXECUTE AS LOGIN = N'username'
GO
declare @Proc nvarchar(50)
declare @RowCnt int
declare @MaxRows int
declare @ExecSql nvarchar(255)
select @RowCnt = 1
select @Proc = 'SELECT * from fn_my_permissions'
declare @Import table (rownum int IDENTITY (1, 1) Primary key NOT NULL , TableName varchar(50))
insert into @Import (TableName) select name from sys.Tables
declare @Output table (entity_name varchar(50), subentity_name varchar(50), permission_name varchar(50))
select @MaxRows=count(*) from @Import
while @RowCnt <= @MaxRows
begin
select @ExecSql = @Proc + '(N''' + TableName + ''', N''OBJECT'') where subentity_name = ''''' from @Import where rownum = @RowCnt
insert into @Output exec sp_executesql @ExecSql
Select @RowCnt = @RowCnt + 1
end
select * from @Output
GO
REVERT
GO
当然,只需将用户名替换为您需要获取权限的用户的登录名即可。
您还必须指定要为哪个数据库执行此操作。
我已将结果限制在表格本身。但是,如果您删除;
where subentity_name = '''''
您还可以找到每个列的权限。
我确信必须有更好的方法来做到这一点......但是,这似乎无论如何都有效!