在 SQL Server 2000 中,是否可以强制触发器在与用户不同的上下文中运行?

发布于 2024-07-18 19:22:38 字数 331 浏览 7 评论 0原文

假设我有一个 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 技术交流群。

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

发布评论

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

评论(3

山人契 2024-07-25 19:22:38

MSDN 资源

EXECUTE AS 指定安全性
触发器所在的上下文
被执行。 使您能够控制哪些
用户帐户 SQL 实例
服务器用于验证权限
任何数据库对象
由触发器引用。

MSDN resource

EXECUTE AS Specifies the security
context under which the trigger is
executed. Enables you to control which
user account the instance of SQL
Server uses to validate permissions on
any database objects that are
referenced by the trigger.

靑春怀旧 2024-07-25 19:22:38

触发器通常在进行初始更改的用户的权限下运行。 解决类似问题的方法是让触发器将数据写入临时表,然后让一个单独的进程(以更高级别的用户身份登录)每隔一段时间检查临时表中的数据并将其移动到目标中桌子。

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.

↘人皮目录ツ 2024-07-25 19:22:38

您使用的是哪个版本的 SQL Server? 我在 SQL Server 2005 中能够毫无问题地完成此操作:

CREATE TABLE dbo.Test_Trigger_1
(
    my_string   VARCHAR(20) NOT NULL,
    CONSTRAINT PK_Test_Trigger_1 PRIMARY KEY CLUSTERED (my_string)
)
GO

CREATE TABLE dbo.Test_Trigger_2
(
    my_string   VARCHAR(20) NOT NULL,
    CONSTRAINT PK_Test_Trigger_2 PRIMARY KEY CLUSTERED (my_string)
)
GO
CREATE TRIGGER dbo.tri_Test_Trigger_1
ON dbo.Test_Trigger_1
FOR INSERT
AS
BEGIN
    INSERT INTO dbo.Test_Trigger_2
    (
        my_string
    )
    SELECT
        my_string
    FROM
        INSERTED
END
GO

然后我创建了一个只能访问 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:

CREATE TABLE dbo.Test_Trigger_1
(
    my_string   VARCHAR(20) NOT NULL,
    CONSTRAINT PK_Test_Trigger_1 PRIMARY KEY CLUSTERED (my_string)
)
GO

CREATE TABLE dbo.Test_Trigger_2
(
    my_string   VARCHAR(20) NOT NULL,
    CONSTRAINT PK_Test_Trigger_2 PRIMARY KEY CLUSTERED (my_string)
)
GO
CREATE TRIGGER dbo.tri_Test_Trigger_1
ON dbo.Test_Trigger_1
FOR INSERT
AS
BEGIN
    INSERT INTO dbo.Test_Trigger_2
    (
        my_string
    )
    SELECT
        my_string
    FROM
        INSERTED
END
GO

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.

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