使用“执行 sp_Executesql”时的权限

发布于 2024-09-25 10:53:02 字数 442 浏览 4 评论 0原文

我有一个数据库,其中所有访问都由存储过程控制。 DBA 希望避免向用户提供对基础表的直接读/写访问权限,这一点我可以理解。因此,所有数据的更新和选择都是通过存储过程完成的。基本上,他创建了一个角色,该角色对数据库中的所有存储过程具有 EXECUTE 权限,并向用户授予该角色。

问题是存储过程之一动态构建 SQl 查询并通过“执行 sp_Executesql”执行它。无需详细说明,查询是动态构建的,因为它会根据许多用户输入参数而发生显着变化。有问题的存储过程只是一个 SELECT sql 语句,但是我发现仅授予存储过程 EXECUTE 权限是不够的。使用“执行 sp_Executesql”的存储过程中引用的基础表需要被授予“datareader”访问权限,否则存储过程将失败。

关于如何纠正这个问题有什么想法吗?我确实想将对表的访问限制为仅存储过程,但我需要找到一种方法来解决使用“执行 sp_Executesq”的存储过程。谢谢。

I have a database where all access is controlled by stored procedures. The DBA would like to avoid giving users direct read/write access to the underlying tables, which I can understand. Hence all updating and selecting of data is done via stored procedures. Basically he has created one role that has EXECUTE permissions to all the stored procedures in the database and given users that role.

The problem is that one of the stored procedures dynamically builds a SQl Query and executes it via "Execute sp_Executesql". Without going into great detail the query is built dynamically because it changes significantly depending on many user input parameters. The stored procedure in question is only a SELECT sql statement however I am finding that just giving the stored procedure EXECUTE permission is not enough. The underlying tables referenced within the stored procedure that make use of "Execute sp_Executesql" need to have been given "datareader" access or else the stored procedure fails.

Any thoughts on how to correct this? I really wanted to restrict access to the tables to only stored procedures, but I need to find a way to work around the stored procedures that make use of "Execute sp_Executesq"l. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罪#恶を代价 2024-10-02 10:53:02

在包装过程中,您可以使用 EXECUTE AS OWNER 或 EXECUTE AS SomeuserWithNoLogin

这将更改包含 sp_executesql 的存储过程期间的登录上下文。

  • 如果您使用 OWNER,它将起作用,因为您已经在使用所有权链。
  • 如果您的 DBA(好人!)不希望您以 dbo 身份运行,那么请设置一个具有完全读取权限但没有权限的用户。 EXECUTE AS 需要一个条目 sys.database_principals

如下所示:

CREATE USER SomeuserWithNoLogin WITH WITHOUT LOGIN
EXEC sp_addrolemember 'db_datareader', 'SomeuserWithNoLogin'

有关详细信息,请参阅 MSDN 上的 EXECUTE AS 子句创建过程

In the wrapper proc you can use EXECUTE AS OWNER or EXECUTE AS SomeuserWithNoLogin

This will change the login context for the duration of the stored proc which includes sp_executesql.

  • If you use OWNER, it will work because you're already using ownership chaining.
  • If your DBA (good man!) does not want you running as dbo, then set up a user that has full read but no rights. EXECUTE AS <user> requires an entry is sys.database_principals

Like this:

CREATE USER SomeuserWithNoLogin WITH WITHOUT LOGIN
EXEC sp_addrolemember 'db_datareader', 'SomeuserWithNoLogin'

For more info, see EXECUTE AS Clause on MSDN and CREATE PROCEDURE

苍风燃霜 2024-10-02 10:53:02

真正的问题是 sp_Executesql 位于 master 数据库中,而不一定是您工作的数据库中。您的 DBA 必须向调用过程授予执行 sp_Executesql 权限。任何有权调用该过程的人都可以运行 sp_Executesql。

The real problem is that sp_Executesql is in the master database, not necessarily the database your working in. Your DBA has to give execute sp_Executesql permission to the calling procedure. Than anyone who has permission to call that procedure will be able to run the sp_Executesql.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文