Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I ask the SQL server if I have permissions on something? (insert, update,etc)

I'm trying to write a unit test for the database layer that validates the connection string's user has update/insert/read permissions. insert/rollback would create extra gaps in the identity column or launch triggers. for read permissions selecting against the table creates load/work on the sql database and updates when the table was last searched in the statistics. How do I programmatically ask sql server the current user's permissions on an object/table/view/stored procedure/etc.

I imagine it's in the system tables somewhere.

like image 828
Maslow Avatar asked Jan 08 '10 19:01

Maslow


People also ask

How do I give permission to update SQL Server?

Launch SQL Server Management Studio and connect with credentials that have been granted the 'sa' role. Expand Security, right-click on Logins and select New Login. Enter a descriptive Login name, select SQL Server authentication, and enter a secure password.

How do I check permissions for schema in SQL Server?

In SSMS, if you follow the path [Database] > Security > Schemas and view any schema properties, you have a tab "permissions" that list all the permissions that every user have on that specific schema.


1 Answers

You can ask for each permission individually using HAS_PERMS_BY_NAME:

SELECT HAS_PERMS_BY_NAME('<table>', 'OBJECT', 'SELECT');
SELECT HAS_PERMS_BY_NAME('<table>', 'OBJECT', 'UPDATE');
...

Or you can ask for all your permissions using fn_my_permissions:

SELECT * FROM sys.fn_my_permissions('<table>', 'object');
like image 115
Remus Rusanu Avatar answered Oct 05 '22 09:10

Remus Rusanu