如何找到在SQL Server中为我的用户帐户提供的访问权限列表?

时间:2015-01-29 17:48:56

标签: sql-server sql-server-2008-r2 sql-server-2012

我想查看在SQL Server中为我的帐户提供的访问权限列表?如何在SQL Server Management Studio中看到?或者我是否需要运行查询才能看到这个?

2 个答案:

答案 0 :(得分:0)

您可以通过sys.fn_my_permissions

获取它

返回数据库上调用者的有效权限列表

SELECT * FROM fn_my_permissions (NULL, 'DATABASE');

同样,您可以在不同级别(SERVER,OBJECT等)查看用户权限

答案 1 :(得分:0)

这些脚本可能适合您。假设您有权运行它们。

USE [YourDatabaseName]

--Server level security
SELECT sp.state_desc,
sp.permission_name,
principal_name = QUOTENAME(spl.name),
spl.type_desc,
sp.state_desc + N' ' + sp.permission_name + N' TO ' + cast(QUOTENAME(spl.name COLLATE DATABASE_DEFAULT) as nvarchar(256)) AS "T-SQL Script"
FROM sys.server_permissions sp
inner join sys.server_principals spl on (sp.grantee_principal_id = spl.principal_id)
where spl.name not like '##%' -- skip PBM accounts
AND spl.name = 'YourUserName'
ORDER by sp.permission_name, spl.name


--Database level security
SELECT  dp.state_desc,
dp.permission_name,
QUOTENAME(dpl.name)  AS 'principal_name',
dpl.type_desc,
dp.state_desc + N' ' + dp.permission_name + N' TO ' + CAST(QUOTENAME(dpl.name COLLATE DATABASE_DEFAULT) AS NVARCHAR(500))  AS "T-SQL Script"
FROM sys.database_permissions AS dp
INNER JOIN sys.database_principals AS dpl ON (dp.grantee_principal_id = dpl.principal_id)
WHERE dp.major_id = 0
AND dpl.name NOT LIKE '##%' -- excluds PBM accounts
AND dpl.name = 'YourUserName'
ORDER BY dp.permission_name ASC, dp.state_desc ASC



SELECT 
    USER_NAME(grantee_principal_id) AS 'User'
  , state_desc AS 'Permission'
  , permission_name AS 'Action'
  , CASE class
      WHEN 0 THEN 'Database::' + DB_NAME()
      WHEN 1 THEN OBJECT_NAME(major_id)
      WHEN 3 THEN 'Schema::' + SCHEMA_NAME(major_id) 
    END AS 'Securable'
FROM sys.database_permissions dp
WHERE class IN (0, 1, 3)
AND minor_id = 0
AND USER_NAME(grantee_principal_id) = 'YourUserName';