2010-07-22 69 views
1

我写一个查询,将返回TSQL查询来验证权限和对象存在

  1. 如果一个表或存储过程,如果一个特定的角色已经assignned执行权限来执行数据库
  2. 中存在特定的存储过程。

我想我需要查询主数据库,并通过摆动来到数据库和我正在寻找的表或存储过程。如果已将执行权限分配给该对象上的特定角色,如何获得“真/假”?

回答

8
USE YourDatabase 

/*To find out if it exists*/ 

SELECT OBJECT_ID('dbo.spFoo') /*Will be NULL if it doesn't exist or you don't have 
           permission to see the object*/ 

/*To find out the permissions on it take a look at the following system views*/ 

select * from sys.database_permissions p 
inner JOIN sys.database_principals dp 
    on  p.grantee_principal_id = dp.principal_id 
    where major_id=object_id('dbo.spFoo')