SQL Server GRANT EXECUTE ON TRIGGER TO USER

问题描述 投票:0回答:1

我有这个触发器:

CREATE TRIGGER [dbo].[TRIGGER_UPDATE_MODIFIED]
   ON  [dbo].[MY_TABLE]   
   AFTER UPDATE 
AS 
BEGIN

    SET NOCOUNT ON;    

    UPDATE [dbo].[MY_TABLE] SET [DATE_MODIFIED] = GETDATE(), [USER_MODIFIED] = CASE WHEN UPDATE ([USER_MODIFIED]) THEN i.[USER_MODIFIED] ELSE RIGHT(SUSER_NAME(), 100) END
    FROM INSERTED i
    WHERE [MY_TABLE].[SOME_FIELD] = i.[SOME_FIELD]

END

我需要授予用户EXECUTE这个触发器的SOME_USER权限,所以我这样做:

GRANT EXECUTE
    ON OBJECT::[dbo].[TRIGGER_UPDATE_MODIFIED] TO [SOME_USER]
    WITH GRANT OPTION
    AS [dbo];
GO

我得到以下错误:

无法找到对象'TRIGGER_UPDATE_MODIFIED',因为它不存在或用户没有权限。

为什么我会收到此错误,如何解决?

我考虑过这样做:

CREATE TRIGGER [dbo].[TRIGGER_UPDATE_MODIFIED]
   ON  [dbo].[MY_TABLE]   
   WITH EXECUTE AS 'SOME_USER'
   AFTER UPDATE 
AS 
BEGIN

    SET NOCOUNT ON;    

    UPDATE [dbo].[MY_TABLE] SET [DATE_MODIFIED] = GETDATE(), [USER_MODIFIED] = CASE WHEN UPDATE ([USER_MODIFIED]) THEN i.[USER_MODIFIED] ELSE RIGHT(SUSER_NAME(), 100) END
    FROM INSERTED i
    WHERE [MY_TABLE].[SOME_FIELD] = i.[SOME_FIELD]

END

这是正确的(使用WITH EXECUTE AS 'SOME_USER')?或者也许使用WITH EXECUTE AS OWNER

有什么区别?

sql-server sql-server-2008 triggers sql-server-2008-r2 database-trigger
1个回答
0
投票

总结评论: 您无法授予DML触发器的权限。您只能授予执行将触发触发器的DML语句的权限。在您的情况下,您应该将表的更新权限授予用户:

GRANT UPDATE ON [dbo].[MY_TABLE] TO [SOME_USER];
© www.soinside.com 2019 - 2024. All rights reserved.