Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grant Exec permissions to all SPROCS

Status
Not open for further replies.

mtorpy

MIS
Aug 20, 2002
31
US
I am trying to write a script to grant EXEC permissions on all non-system stored procedures on all DB's to a specific user 'QATest'

The below code will do this successfully but I need a way of writing it so it will run on ALL databases. Avoiding writing the same code for all db's on the system.

Any help?

---------------
declare @sql nvarchar(4000)
declare @db sysname ; set @db = 'Test32' --Change DB NAME
declare @u sysname ; set @u = QUOTENAME('QATest')

set @sql ='select ''grant exec on '' + QUOTENAME(ROUTINE_SCHEMA) + ''.'' +
QUOTENAME(ROUTINE_NAME) + '' TO ' + @u + ''' FROM INFORMATION_SCHEMA.ROUTINES ' +
'WHERE OBJECTPROPERTY(OBJECT_ID(ROUTINE_NAME),''IsMSShipped'') = 0'

PRINT 'Adding EXEC Stored Procedure permissions to ' + @u + ' on data base ' + @db

exec master.dbo.xp_execresultset @sql,@db
 
Have you tried sp_msforeachdb?

------
[small]select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')[/small]
[banghead]
 
OK Tried this but no luck as of yet is it possible?


EXEC sp_MSforeachdb
@command1 = 'IF ''?'' NOT IN(''master'', ''model'',''tempdb'',''msdb'')
IF EXISTS (select * from ?..sysusers where name = ''QATest'' and issqluser = 1)
begin
print ''Granting EXEC Permissions to QATest user in ?''

declare @sql nvarchar(4000)
set @sql ='select ''grant exec on '' + QUOTENAME(ROUTINE_SCHEMA) + ''.'' +
QUOTENAME(ROUTINE_NAME) + '' TO ' + 'QATest' + ''' FROM INFORMATION_SCHEMA.ROUTINES ' +
'WHERE OBJECTPROPERTY(OBJECT_ID(ROUTINE_NAME),''IsMSShipped'') = 0'

exec ?.dbo.xp_execresultset @sql,?
end'
 
OK I GOT IT

EXEC sp_MSforeachdb
@command1 = 'IF ''?'' NOT IN(''master'', ''model'',''tempdb'',''msdb'')
begin
declare @u sysname
set @u = QUOTENAME(''QATest'')

declare @sql nvarchar(4000)
set @sql =''select ''''grant exec on '''' + QUOTENAME(ROUTINE_SCHEMA) + ''''.'''' +
QUOTENAME(ROUTINE_NAME) + '''' TO '' + @u + '''''' FROM INFORMATION_SCHEMA.ROUTINES '' +
''WHERE OBJECTPROPERTY(OBJECT_ID(ROUTINE_NAME),''''IsMSShipped'''') = 0''


PRINT ''Adding [EXEC] permissions to all nonsystem stored procedures to user '' + @u + '' on database '' + ''[?]''
exec master.dbo.xp_execresultset @sql,''?''

end'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top