views:

180

answers:

3

In SQL Server 2005, there are built in roles:

db_reader

db_writer

etc.

Is there any role that lets a user execute an stored proc?

I don't want to use db_owner, because that will permit deletion and updates, which I don't need. The only permissions I need are:

SELECT

EXECUTE

+1  A: 

No, I don't believe that there is a database or server role - you have to grant the execute permission granularly to the user for the relevant stored procedures.

Peter Schofield
or you could put users into roles and grant the rights to each proc to the role rather than 1000 differnt users.
HLGEM
+1  A: 

Take a look at this site. It may provide you an interesting idea to do this quickly.

Aaron
But what @Peter said is essentially correct...you will need to grant a username executing privileges...this site will give you a quick way to do that.
Aaron
That's a lovely solution! You got my vote...
Peter Schofield
A: 

To expand on the answer, the general gist is to create a database role and assign permissions to that role. In order to do that, you need some fancy dynamic SQL such as:

Set @Routines = Cursor Fast_Forward For
    Select ROUTINE_SCHEMA + '.' + ROUTINE_NAME, ROUTINE_TYPE, DATA_TYPE
    From INFORMATION_SCHEMA.ROUTINES
    Where ROUTINE_NAME NOT LIKE 'dt_%'
        Or ROUTINE_TYPE = 'FUNCTION'

Open @Routines
Fetch Next From @Routines Into @Procname, @RoutineType, @DataType

While @@Fetch_Status = 0
Begin
    Set @Msg = 'Procname: ' + @Procname + ', Type: ' + @RoutineType + ', DataType: ' + Coalesce(@DataType,'')
    Raiserror(@Msg, 10, 1) WITH NOWAIT

    If @RoutineType = 'FUNCTION' And @DataType = 'TABLE'
        Set @SQL = 'GRANT SELECT ON OBJECT::' + @Procname + ' TO StoredProcedureDataReader'
    Else
        Set @SQL = 'GRANT EXECUTE ON OBJECT::' + @Procname + ' TO StoredProcedureDataReader'

    exec(@SQL)

    Fetch Next From @Routines Into @Procname, @RoutineType, @DataType
End

Close @Routines
Deallocate @Routines

This code will grant EXECUTE to stored procedures and scalar functions and SELECT to user-defined functions that return a TABLE type.

Thomas