在 SQL Server 2000 中,是否可以强制触发器在与用户不同的上下文中运行?
假设我有一个 SQL Server 2000 表,任何名称都可以,与本主题无关。 在此表上,我有一个在更新或插入后运行的触发器。
用户可以插入和更新触发器所附加的表,但不能插入和更新触发器所针对的其他表。
如果用户修改原始表中的数据,我会收到一个异常,抱怨用户无权修改触发器的目标表中的数据。
我认为这是由触发器在用户上下文中运行这一事实引起的。 有没有办法让触发器在自己的上下文中运行,或者我没有正确解释此异常的原因?
编辑:我应该指出,我正在使用 SQL Server 2000,因此使用 EXECUTE AS 不起作用。
Let's say I have a SQL Server 2000 table, any name will do, it's irrelevant for this topic. On this table I have a trigger that runs after update or insert.
The user is able to insert and update the table on which the trigger is attached, but not on other tables that the trigger targets.
If the user modifies data in the original table, I get an exception complaining that the user doesn't have permission to modify data in the target tables of the trigger.
I assume this is caused by the fact that the trigger is running in the context of the user. Is there a way to have the trigger run in its own context or am I not interpreting the cause of this exception correctly?
Edit: I should point out that I'm using SQL Server 2000, so using EXECUTE AS won't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
MSDN 资源
MSDN resource
触发器通常在进行初始更改的用户的权限下运行。 解决类似问题的方法是让触发器将数据写入临时表,然后让一个单独的进程(以更高级别的用户身份登录)每隔一段时间检查临时表中的数据并将其移动到目标中桌子。
Triggers do normally operate with the permissions of the user who made the initial change. A workaround for something like this is for the trigger to write the data into a temporary table and then have a separate process (logged in as a higher-level user) check for data in the temporary table every so often and move it into the target table.
您使用的是哪个版本的 SQL Server? 我在 SQL Server 2005 中能够毫无问题地完成此操作:
然后我创建了一个只能访问 Test_Trigger_1 的登录名,我确认它无法访问 Test_Trigger_2,我在 Test_Trigger_1 中插入了一行,并且 Test_Trigger_2 中出现了一行。
Which version of SQL Server are you using? I was just able to do it without any problems in SQL Server 2005:
Then I created a login that only had access to Test_Trigger_1, I confirmed that it couldn't access Test_Trigger_2, I inserted a row into Test_Trigger_1 and a row appeared in Test_Trigger_2.