lee posted on December 27, 2011 14:37

There’s probably a gazillion stored procedures out on the internet to grant permissions, and I needed one, so I cruised over to this site and found one. A couple of exceptions here to what I wanted to do, so I made a few changes to his base and created a new one. Here are the changes that I made:

  • Added a cursor for ease of use, and got rid of the temp table
  • Joined to sys.schemas to get the schema, and added the Schema, since we have so many schemas here at my shop
  • Identified the TF and FN objects, and CASEd the appropriate permission type (EXEC or SELECT) for each
  • Added QUOTENAME so that it would work properly for our users
IF object_id('usp_sql_grant_execute_permission') IS NOT NULL    
    DROP PROCEDURE [usp_sql_grant_execute_permission]
GO
 
CREATE PROCEDURE [dbo].[usp_sql_grant_execute_permission] 
        @login VARCHAR(50),
        @RunEXEC BIT = 1 --1 will run the statements, 0 = Prints them
AS
SET NOCOUNT ON
 
DECLARE @procname VARCHAR(150),
      @schema VARCHAR(10),
      @xtype VARCHAR (10),
      @sql VARCHAR(255)
 
-- Populate the temporary table with user defined stored procs and functions
DECLARE CS CURSOR 
FOR
SELECT s.[name], x.[name], s.xtype
FROM sys.sysobjects s
INNER JOIN sys.schemas x
    ON s.uid = x.schema_id
WHERE (xtype = 'P') OR  (xtype = 'FN' OR xtype = 'TF')
 
OPEN CS
 
FETCH NEXT FROM cs INTO @procname, @schema, @xtype
 
-- Iterate through each of the rows on the table
WHILE @@fetch_status=0
BEGIN
     -- Prepare and execute SQL statement
     SET @sql = 'GRANT ' + CASE WHEN @xtype != 'TF' THEN 'EXEC ' ELSE 'SELECT ' END +'ON ' 
+ @schema  + '.' + @procname + ' TO ' + QUOTENAME(@login)
                    
     -- do we run or print them out
     IF @RunEXEC = 1
       EXEC (@sql)
     ELSE
       PRINT @SQL
          
     FETCH NEXT FROM cs INTO @procname, @schema, @xtype    
END
 
CLOSE cs
DEALLOCATE cs
 
GO

 

I used to do a lot of administration, then didn’t do any for 100 years, and now I’m back doing some again. It’s fun for sure. Thanks to blog.strictly-software.com for contributing this code – we appreciate you!

Lee

 

----------------------------

 

http://blog.strictly-software.com/2010/01/grant-execute-permission-to-all-stored.html


Posted in: SQL Administration  Tags:
blog comments powered by Disqus

by Lee Everest, M.S.

Info

Poll

Do you use Azure or cloud in your organization?



Show Results

Ads

Search


Month List

Calendar

«  May 2012  »
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
View posts in large calendar

Tags

Disclaimer
The opinions, code, examples, et.al. expressed herein are my own personal opinions and do not represent my employer's view in any way, shape form, or fashion.  All code for demonstration purposes - no guarantees, either written or implied, are made.

© Copyright 2012 Lee Everest's SQL Server, etc. weblog