Thursday, April 9, 2009

Give permission to all stored procedures for a SQL user

use < DatabaseName>
Declare @userName varchar(50)
set @userName = < UserName>

declare cur cursor FAST_FORWARD for
select name from sysobjects where xtype = 'P' and category <> 2

declare @spName varchar(255)

open cur
fetch next from cur into @spName

WHILE @@FETCH_STATUS = 0
BEGIN
exec('GRANT EXECUTE ON [dbo].[' + @spName + '] TO ' + @userName + '')
fetch next from cur into @spName
end

close cur
deallocate cur


SELECT 'Given exec permision to all procedures for the user ' + @userName

--