Thursday 23 August 2018

Grant a user access to view stored procedures in MSSQL

To grant access to a non admin user (assuming you have created them an SQL login imported from Active Directory), first run this command in a new query window from SSMS, substituting domain\username for the actual users credentials. Make sure you are targeting the correct database in the context menu


select 'GRANT VIEW DEFINITION ON [' + schema_name(schema_id) + '].[' + name +
       '] TO ' + '[domain\username]'
  from sys.all_objects
where type_desc = 'SQL_STORED_PROCEDURE'
   and schema_id <> schema_id('sys')


This will return a list of the commands you need to run to allow the user access to each stored procedure. Right click the table and select all then copy. Now paste this into your query window, overwriting the above command and run it. Voila, it is done.