我可以为SET IDENTITY_INSERT提供存储过程权限吗?

时间:2014-08-18 17:09:56

标签: sql sql-server stored-procedures

我有一个存储过程,我想授予插入标识的权限,同时不给予受限用户调用ALTER TABLE权限。这可能吗?

这是我的存储过程:

CREATE PROCEDURE [dbo].[AddInternalQu] 
(
    @id [int],
    @qu [nvarchar](500),
    @pg [nvarchar](50),
    @isactive [bit],
    @isdoc [bit],
    @allowNA [bit],
    @textBox [bit],
    @redf [bit],
    @qord [int],
    @shqu [nvarchar](50),
    @restrict [bit],
    @scanwizard [bit]
)
AS
BEGIN
    SET IDENTITY_INSERT [questions] ON;

    INSERT INTO [questions] (qu_id, question, shortqu, redflag, page, active, is_doc_qu, 
                             allowNA, textBox, qu_order, scanwizard, restricted) 
    VALUES(@id, @qu, @shqu, @redf, @pg, @isactive, @isdoc, 
           @allowNA, @textBox, @qord, @scanwizard, @restrict);

    SET IDENTITY_INSERT [questions] OFF;
END

GRANT EXEC ON AddInternalQu TO MyStandardRole;

因为它是一个用户,我添加到MyStandardRole角色有权执行存储过程,但后来尝试设置没有足够权限的身份插入失败。

1 个答案:

答案 0 :(得分:6)

“WITH EXECUTE AS OWNER”应该可以解决问题。

CREATE PROCEDURE [dbo].[AddInternalQu] 
(
    @id [int],
    @qu [nvarchar](500),
    @pg [nvarchar](50),
    @isactive [bit],
    @isdoc [bit],
    @allowNA [bit],
    @textBox [bit],
    @redf [bit],
    @qord [int],
    @shqu [nvarchar](50),
    @restrict [bit],
    @scanwizard [bit]
)
WITH EXECUTE AS OWNER
AS
BEGIN
    SET IDENTITY_INSERT [questions] ON;

    INSERT INTO [questions] (qu_id, question, shortqu, redflag, page, active, is_doc_qu, 
                             allowNA, textBox, qu_order, scanwizard, restricted) 
    VALUES(@id, @qu, @shqu, @redf, @pg, @isactive, @isdoc, 
           @allowNA, @textBox, @qord, @scanwizard, @restrict);

    SET IDENTITY_INSERT [questions] OFF;
END

GRANT EXEC ON AddInternalQu TO MyStandardRole;