Home Microsoft SQL Server DigiMailing iRN Contact
    Keyword



sys.spt_table_privileges_view

  No additional text.


Syntax

create view sys.spt_table_privileges_view
as
    select
        -- begin (for doing joins)
        object_id           = o.object_id,
        schema_id           = o.schema_id,
        -- end (for doing joins)
        TABLE_CATALOG       = convert(sysname,db_name()),
        TABLE_SCHEMA        = convert(sysname,s.name),
        TABLE_NAME          = convert(sysname,o.name),
        GRANTOR             = convert(sysname,user_name(p.grantor_principal_id)),
        GRANTEE             = convert(sysname,user_name(p.grantee_principal_id)),
        PRIVILEGE_TYPE      = convert(nvarchar(30), v.permission),
        IS_GRANTABLE        = convert(bit,        case when p.state = 'G' then 0    else 1     end),
        IS_GRANTABLE_STR    = convert(varchar(3), case when p.state = 'G' then 'NO' else 'YES' end)
    from
        sys.all_objects o inner join
        sys.database_permissions p on
            (
                o.type in ('S','U','V') and
                p.major_id = o.object_id and
                p.type in ('RF','SL','IN','DL','UP') and
                p.state <> 'D' -- only grant rows
            ) inner join
        sys.spt_permission_names v on
            (
                v.type = p.type
            ) inner join
         sys.schemas s on
            (
                o.schema_id = s.schema_id and
                s.principal_id <> p.grantee_principal_id and -- no rows for owner, such rows are generated in 3rd select
                has_perms_by_name(s.name+'.'+o.name, 'OBJECT', v.permission) = 1
            )

    union

    select
        -- begin (for doing joins)
        object_id           = o.object_id,
        schema_id           = o.schema_id,
        -- end (for doing joins)
        TABLE_CATALOG       = convert(sysname,db_name()),
        TABLE_SCHEMA        = convert(sysname,s.name),
        TABLE_NAME          = convert(sysname,o.name),
        GRANTOR             = convert(sysname,user_name(p.grantor_principal_id)),
        GRANTEE             = convert(sysname,user_name(u.principal_id)),
        PRIVILEGE_TYPE      = convert(nvarchar(30), v.permission),
        IS_GRANTABLE        = convert(bit,        case when p.state = 'G' then 0    else 1     end),
        IS_GRANTABLE_STR    = convert(varchar(3), case when p.state = 'G' then 'NO' else 'YES' end)
    from
        sys.all_objects o inner join
        sys.database_permissions p on
            (
                o.type in ('S','U','V') and
                p.major_id = o.object_id and
                p.type in ('RF','SL','IN','DL','UP') and
                p.state <> 'D' -- only grant rows
            ) inner join
        sys.spt_permission_names v on
            (
                v.type = p.type
            ) inner join
        sys.schemas s on
            (
               o.schema_id = s.schema_id
            ) inner join
        sys.database_principals u on
            (
                s.principal_id <> u.principal_id -- no rows for owner, such rows are generated in 3rd select
            ) inner join
        sys.database_role_members m on
            (
                p.grantee_principal_id = m.role_principal_id and
                u.principal_id = m.member_principal_id and
                has_perms_by_name(s.name+'.'+o.name, 'OBJECT', v.permission) = 1
            )

    union

    select  -- Add rows for table owner
        -- begin (for doing joins)
        object_id           = o.object_id,
        schema_id           = o.schema_id,
        -- end (for doing joins)
        TABLE_CATALOG       = convert(sysname,db_name()),
        TABLE_SCHEMA        = convert(sysname,schema_name(o.schema_id)),
        TABLE_NAME          = convert(sysname,o.name),
        GRANTOR             = convert(sysname,user_name(u.principal_id)),
        GRANTEE             = convert(sysname,user_name(ObjectProperty(o.object_id,'ownerid'))),
        PRIVILEGE_TYPE      = convert(nvarchar(30),v.permission),
        IS_GRANTABLE        = convert(bit,1),
        IS_GRANTABLE_STR    = convert(varchar(3),'YES')
    from
        sys.all_objects o inner join
        sys.database_principals u on
            (
                o.type in ('S','U','V') and
                u.principal_id = 1 -- grantor is 'dbo' of database
            ) inner join
        sys.spt_permission_names v on
            (
                user_id() = 1 or -- executed as 'dbo'
                user_id() = ObjectProperty(o.object_id,'ownerid') -- or object owner
            )

 
Last revision SQL2008SP2
See also

  sp_table_privileges (Procedure)
sp_table_privileges_rowset (Procedure)
sp_table_privileges_rowset2 (Procedure)
       



News

  Query a named instance
Sybase+ASE+silent+install
SQL 2008 R2
Deprecated procedures in SQL2008
Reporting Services item-level role definitions
Create all your missing indexes
Converting datetime field
Start MSSQL Server Profiler at time
Replicating MSSQL Server views
Exploring Microsoft Sharepoint
The OLE DB provider "SQLNCLI10" for linked server indicates.
Mobile solar charger
Oracle to SQL Server replication
Cannot insert the value NULL into column
Undocumented Microsoft SQL Server 2008
VMware
Zoekmachine optimalisatie
SQL Servers hidden objects
FckEditor
Flash in Ajax
De hype die AJAX heet
Barcode scannen
MySQL; Gratis, makkelijk en snel!
PHP
sIFR; de combinatie tussen HTML en Flash