运行调用另一个数据库上的存储过程的触发器

发布于 2024-09-25 08:34:55 字数 464 浏览 2 评论 0原文

我们希望在一个数据库 (A) 上运行一个触发器,该触发器调用另一个数据库 (B) 上的存储过程。

CREATE TRIGGER trg_A ON TableA
FOR INSERT
AS
BEGIN
    EXEC DatabaseB.dbo.stp_B
END

我们希望在 DatabaseA 上以 LoginA 和 UserA 的身份执行此操作。我们还在 DatabaseB 上有 LoginB 和 UserB。

我们怎样才能做到这一点?

目前我们收到此错误消息

The server principal "..." is not able to access the 
database "DatabaseB" under the current security context.

我们已尝试 WITH EXECUTE AS 'UserB',但没有成功。

We would like to run a trigger on one database (A) that calls a stored procedure on another database (B).

CREATE TRIGGER trg_A ON TableA
FOR INSERT
AS
BEGIN
    EXEC DatabaseB.dbo.stp_B
END

We would like to do this as LoginA with UserA on DatabaseA. We also have LoginB with UserB on DatabaseB.

How can we accomplish this?

Currently we get this error message

The server principal "..." is not able to access the 
database "DatabaseB" under the current security context.

We have tried WITH EXECUTE AS 'UserB', without luck.

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

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

发布评论

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

评论(1

我为君王 2024-10-02 08:34:55

有趣的问题。你这个书呆子狙击了我。我测试了很多不同的场景。

这些失败:

  1. 表 A 上的触发器引用 DatabaseB 中的存储过程。 LoginA 在 DatabaseB 中不作为用户存在。
  2. tableA 上的触发器引用 DatabaseA 中插入 DatabaseB.dbo.TableB 的存储过程。 DatabaseB 中不存在 LoginA。
  3. 在 DatabaseB 上创建 LoginA 作为用户,但没有授予它任何权限。重复测试1和测试2。两者都失败了。
  4. 在 DatabaseA 上创建 LoginB 作为用户。确保 LoginB 可以插入 DatabaseB.dbo.TableB。以 LoginA 身份登录 DatabaseA,运行 EXECUTE AS User = 'LoginB'。尝试插入 DatabaseA.dbo.TableA。

我可以让触发器工作的唯一方法是在 DatabaseB 中为 LoginA 创建一个用户,并授予执行 DatabaseB 存储过程的权限。

Interesting question. You nerd-sniped me. I tested a bunch of different scenarios.

These failed:

  1. Trigger on tableA references stored procedure in DatabaseB. LoginA does not exist as a user in DatabaseB.
  2. Trigger on tableA references stored procedure in DatabaseA that inserts into DatabaseB.dbo.TableB. LoginA does not exist in DatabaseB.
  3. Created LoginA as a user on DatabaseB, but didn't give it any rights. Repeated test1 and test2. Both failed.
  4. Created LoginB as a user on DatabaseA. Made sure LoginB could insert into DatabaseB.dbo.TableB. Logged into DatabaseA as LoginA, ran EXECUTE AS User = 'LoginB'. Tried to insert into DatabaseA.dbo.TableA.

The only way I could get the trigger to work was to create a user in DatabaseB for LoginA and grant permissions to execute the DatabaseB stored procedure.

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