简体   繁体   English

在SQL Server中执行存储过程的权限问题

[英]Execute permission issue in sql server for stored procedure

I have lot of stored procedures used across different clients, for few clients there is a user AdminRole and execute permissions needs to be added for AdminRole, for few clients there is no user AdminRole and there is no need to have execute permission. 我在不同的客户端上使用了很多存储过程,对于几个客户端,有一个用户AdminRole,并且需要为AdminRole添加执行权限,对于几个客户端,则没有用户AdminRole,也不需要执行权限。

Each of the stored procedure has 每个存储过程都有

For ex: 例如:

CREATE PROCEDURE PROCEDURENAME AS

SELECT FIRSTNAME FROM TABLE1

GO

GRANT EXECUTE ON PROCEDURENAME TO ADMINROLE

GO

this line in the end, however this fails to execute when there is no admin role. 这行最后,但是,当没有管理员角色时,该行将无法执行。 How i can implement this logic here in sql stored procedure ? 我如何在sql存储过程中实现此逻辑?

If userrole contains AdminRole  then

  --Execute these 2 lines

GRANT EXECUTE ON PROCEDURENAME TO ADMINROLE
GO

else

 'Do nothing

DATABASE_PRINCIPAL_ID怎么样

IF DATABASE_PRINCIPAL_ID('AdminRole') IS NOT NULL

if there are lot of stored procedures and you want ADMINROLE to have execute permission to all the stored procs in the database run the following command 如果存储过程很多,并且您希望ADMINROLE对数据库中所有存储的proc具有执行权限,请运行以下命令

IF USER_ID('AdminRole') IS NOT NULL GRANT EXECUTE TO [AdminRole] ELSE Print 'AdminRole does not exist in the database' 如果USER_ID('AdminRole')不是NULL则授权执行到[AdminRole] ELSE打印'数据库中不存在AdminRole'

Not sure if in your solution "AdminRole" is in fact SQL Login, SQL User or SQL Database Role 不知道您的解决方案中的“ AdminRole”是否实际上是SQL登录名,SQL用户或SQL数据库角色

If "AdminRole" is SQL user (at database level) or SQL role (at database level; same syntax) and you want to check if it exists at all (and not to check if current user is AdminRole or ismember of AdminRole), do: 如果“ AdminRole”是SQL用户(在数据库级别)或SQL角色(在数据库级别;相同的语法),并且您要检查它是否存在(而不是检查当前用户是AdminRole还是AdminRole的成员),请执行:

IF EXISTS(SELECT name FROM sys.database_principals WHERE name='AdminRole')
BEGIN
    -- do your GRANT here
END

If "AdminRole" is SQL login (at server level), do: 如果“ AdminRole”是SQL登录(在服务器级别),请执行以下操作:

IF EXISTS(SELECT name FROM sys.server_principals WHERE name='AdminRole')
BEGIN
    -- do your GRANT here
END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM