Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL grant execute on multiple objects

Hi all I want to add execute permissions to a user for multiple objects. But I can't seem to add wildcards into my code.

GRANT EXECUTE ON OBJECT::dbo.CREATESERVERSESSIONS TO [domain\user];

this works but I have a lot of stored procedures that start with XU_ now I want grant execute on all stored procedures that start with XU_

GRANT EXECUTE ON OBJECT::dbo.XU_* TO [domain\user];

but that is not working. I hope someone knows a solution to this. Thanks in advance.

like image 538
Chino Avatar asked Dec 24 '12 11:12

Chino


People also ask

How do you grant execute permission on all stored procedures?

Create a database role and add a user to that role. Grant EXECUTE permission at the schema level. Create a new stored procedure. Login as the test user and execute the stored procedure.

How do I provide execute permission in SQL Server?

Use SQL Server Management StudioExpand Stored Procedures, right-click the procedure to grant permissions on, and then select Properties. From Stored Procedure Properties, select the Permissions page. To grant permissions to a user, database role, or application role, select Search.

Does Db_owner have execute permission?

Btw, db_owner is a database ROLE in SQL Server , not a permission. Or if you want the user to execute all current and future stored procedures and scalar-valued functions: grant execute on schema::dbo to User for a single schema, or just grant execute to User for the whole database.


1 Answers

You cannot use wildcards - you have to grant either to all objects (or all objects in a schema) - or then you have to list all objects one by one.

What you might do is something like this - have SQL Server generate those statements for you:

SELECT
   p.Name,
   GrantCmd = 'GRANT EXECUTE ON OBJECT::' + p.name + ' TO [domain\user]'
FROM sys.procedures p
WHERE p.Name LIKE 'XU%'

This query will list all procedures that start with XU and create a column that contains the GRANT EXECUTE ON .... statement for that procedure.

Run this in SQL Server Management Studio, and then just copy the resulting GrantCmd column, paste it to a new window, and execute it there.

And if you really want to automate this, you could also turn this query into a cursor and then use dynamic SQL to automatically execute those resulting GrantCmd statements....

like image 93
marc_s Avatar answered Sep 28 '22 23:09

marc_s