3

即使我选择了脚本对象级权限的选项,Sql Server Management Studio 2008 也不是脚本表权限。这是一个错误还是有其他方法可以做到这一点?它正在为存储的过程创建权限,但不是为表创建权限。我是系统管理员。

如果它不起作用,是否有免费的 sql server 工具来编写权限脚本?

4

1 回答 1

2

我在一个 SQL Server 论坛站点上遇到了这个方便的脚本,但如果我能再次找到它,我会很烦恼:

CREATE VIEW [dbo].[viw_DBPerms] AS
SELECT 
    CASE 
            WHEN o.type = 'P' THEN 'Stored Procedure' 
            WHEN o.type = 'TF' THEN 'Table Function' 
            WHEN o.type = 'FN' THEN 'Scalar Function' 
            WHEN o.type = 'U' THEN 'Table'
            WHEN o.type = 'V' THEN 'View' 
            WHEN o.type = 'SQ' THEN 'Service Queue'
            ELSE o.type
    END AS [Type],
    s.name AS [Schema], 
    o.name AS [Object],
    pr.name AS [User], 
    pe.permission_name AS Permission
FROM sys.database_permissions pe
LEFT JOIN sys.database_principals pr ON pe.grantee_principal_id = pr.principal_id
JOIN 
    (   SELECT [object_id] AS [id], [name], type, schema_id, 1 AS [class] FROM sys.objects 
        UNION 
        SELECT [service_id] AS [id], [name] COLLATE SQL_Latin1_General_CP1_CI_AS [name], 'Service', '0', 17 AS [class] FROM sys.services 
        UNION
        SELECT [service_cONtract_id] AS [id], [name], 'Service Contract', '0', 16 AS [class] FROM sys.service_contracts
        UNION
        SELECT [message_type_id] AS [id], [name], 'Message Type', '0', 15 AS [class] FROM sys.service_message_types
    ) o 
    ON pe.major_id = o.id AND pe.class = o.class
LEFT JOIN sys.schemas s ON o.schema_id = s.schema_id

...然后只需使用:

SELECT Object, 
    SUM(CASE Permission WHEN 'SELECT' THEN 1 ELSE 0 END) AS 'SELECT',
    SUM(CASE Permission WHEN 'INSERT' THEN 1 ELSE 0 END) AS 'INSERT',
    SUM(CASE Permission WHEN 'UPDATE' THEN 1 ELSE 0 END) AS 'UPDATE',
    SUM(CASE Permission WHEN 'DELETE' THEN 1 ELSE 0 END) AS 'DELETE',
    SUM(CASE Permission WHEN 'ALTER' THEN 1 ELSE 0 END) AS 'ALTER'
FROM viw_DBPerms
WHERE [User] = '<sqluser>'
GROUP BY Object

这适用于 SQL 2005,我希望它可以在 SQL 2008 上正常工作。

于 2008-11-06T03:50:53.220 回答