views:

41

answers:

2

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.

+4  A: 

Try using the following stored proc

EXEC sp_table_privileges 
   @table_name = ' <table name> '

It should give you want you are looking for

Sparky
+1 this shows a nice list of everyone's permissions on the table
Maslow
+6  A: 

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');
Remus Rusanu
+1 both of these are good information to have. in sql server 2005, it's `SELECT Has_Perms_By_Name('<table>','OBJECT','SELECT')`
Maslow
Thanks Maslow, I fixed the syntax and args
Remus Rusanu