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

use*_*521 1 t-sql sql-server-2005

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

  1. 如果数据库中存在表或存储过程
  2. 如果已为特定角色分配了执行特定存储过程的执行权限.

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

Mar*_*ith 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')
Run Code Online (Sandbox Code Playgroud)