Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a SQL Sever TSQL script tell what security permissions it has?

I have a TSQL script that is used to set up a database as part of my product's installation. It takes a number of steps which all together take five minutes or so. Sometimes this script fails on the last step because the user running the script does not have sufficient rights to the database. In this case I would like the script to fail strait away. To do this I want the script to test what rights it has up front. Can anyone point me at a general purpose way of testing if the script is running with a particular security permission?

Edit: In the particular case I am looking at it is trying to do a backup, but I have had other things go wrong and was hoping for a general purpose solution.

like image 678
Martin Brown Avatar asked Oct 15 '22 17:10

Martin Brown


1 Answers

select * from fn_my_permissions(NULL, 'SERVER') 

This gives you a list of permissions the current session has on the server

select * from fn_my_permissions(NULL, 'DATABASE')

This gives you a list of permissions for the current session on the current database.

See here for more information.

like image 84
Kjartan Þór Kjartansson Avatar answered Oct 18 '22 03:10

Kjartan Þór Kjartansson