Tuesday, November 27, 2007

SQL Server GRANT EXECUTE TO [Role] During Stored Procedure Creation

Why is it so troublesome to find the simple things?

For some time now I've wanted to automatically grant execute permissions to a specific database role during the creation of my stored procedures (for a given app). No, I don't want to grant permissions to a set of existing procedures. I grew accustomed to doing this when I created Oracle Stored Procs a few years ago and tried several times with similar syntax to no avail.
Well I'm about to create a sh*tload of pros and the extra time (and frustration) it takes to add and grant execute on each sp through the management studio was enough to get me to look for the solution again.

I already create enough procedures to warrant the creation of a temp txt file to hold a new proc template:

SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
-- ==============================================================
-- Author: Brian H Donoho
-- Create date:
-- Description:
-- ==============================================================

CREATE PROCEDURE [dbo].[usp_]

AS

BEGIN



END

Looking here, the syntax is pretty much what I'd tried using at the bottom of the CREATE PROCEDURE.

Of course it was one tiny little thing coming between me and my goal. A couple of well placed GOs.

This Works:


SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
-- ==============================================================
-- Author: Brian H Donoho
-- Create date:
-- Description:
-- ==============================================================

CREATE PROCEDURE [dbo].[usp_]

AS

BEGIN



END
GO

GRANT EXECUTE ON [dbo].[usp_] TO [Role]
GO

Labels: , , ,

0 Comments:

Post a Comment

<< Home