用于验证权限和对象的SQL查询是否存在

时间:2010-07-22 00:29:45

标签: sql-server-2005 tsql

我正在写一个将返回的查询

  1. 如果数据库中存在表或存储过程
  2. 如果已为特定角色分配了执行该特定存储过程的执行权限。
  3. 我想我需要查询master数据库并通过它来摆动以获取我正在寻找的数据库和表或存储过程。如果已将执行权限分配给该对象上的特定角色,我如何获得'true / false'?

1 个答案:

答案 0 :(得分: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')