Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I give a Stored Procedure permission to SET IDENTITY_INSERT?

I have a stored procedure that I would like to give permission to insert identities while not giving the restricted user calling it ALTER TABLE privileges. Is that possible?

Here's my stored procedure:

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;

As it stands a user I add to the MyStandardRole role has the rights to execute the stored procedure but then it fails trying to set the identity insert with insufficient privileges.

like image 500
noelicus Avatar asked Aug 18 '14 17:08

noelicus


People also ask

How do you IDENTITY_INSERT is set to ON?

If the value inserted is larger than the current identity value for the table, SQL Server automatically uses the new inserted value as the current identity value. The setting of SET IDENTITY_INSERT is set at execute or run time and not at parse time.

How do you check if IDENTITY_INSERT is set to on or off in SQL Server?

Answers. In a given session , you can have only one table's IDENTITY_INSERT property set to ON. You can use set IDENTITY_INSERT state (on/off) only at excute or run time.

What is when IDENTITY_INSERT is set to off?

By default, SQL Server automatically inserts an increment value for an IDENTITY column, when the IDENTITY_INSERT parameter is set to OFF. If you don't need an explicit value for the IDENTITY column, remove the IDENTITY column from the component schema.

What does IDENTITY_INSERT mean?

IDENTITY_INSERT is a table property that allows you to insert explicit values into the column of table identifiers, i.e. into the column with IDENTITY. The value of the inserted identifier can be either less than the current value or more, for example, to skip a certain interval of values.


1 Answers

"WITH EXECUTE AS OWNER" should do the trick.

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;
like image 180
Jim V. Avatar answered Sep 25 '22 07:09

Jim V.