SQL Server GRANT EXECUTE ON TRIGGER TO USER

时间:2017-01-13 10:12:46

标签: sql-server sql-server-2008 triggers sql-server-2008-r2 database-trigger

我有这个触发器:

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

有什么区别?

1 个答案:

答案 0 :(得分:0)

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

GRANT UPDATE ON [dbo].[MY_TABLE] TO [SOME_USER];