Solved

Stored Procedure check Permissions SQL Server 2000

Posted on 2004-03-31
6
1,564 Views
Last Modified: 2012-05-04
I have a stored procedure that I pass a table name into as a parameter from vb.net.  I could also pass in user name if I cannot get it from sql server.

Is it possible for me to see if a user executing the stored procedure has select rights on the table and exit if not?

My table permissions are set by roles (ie. I have three users in a role)  I have granted permissions to the role and not each of the three users.

Thanks
0
Comment
Question by:barnetjeb
  • 3
  • 2
6 Comments
 
LVL 2

Expert Comment

by:kiprimshot
ID: 10725925
sp_helprotect 'table_name' will tell you the permissions for a table...by permission group
0
 
LVL 2

Assisted Solution

by:kiprimshot
kiprimshot earned 100 total points
ID: 10726131
sp_helprolemember 'RoleName' will tell you all the members of a role

both together could get you what you want... but there may be a better way
0
 
LVL 7

Expert Comment

by:Lori99
ID: 10726582
How about this.  Execute a simple SELECT against the table requested and check for a 'permission denied' error message.  I think the message number you want would be 229.  It would probably be more efficient and easier than executing multiple stored procedures to determine the user's permissions.
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

Author Comment

by:barnetjeb
ID: 10727027
I have a stored proc like this

Set @strsql = 'delete from ' + @table_name + ' where ' + @id_field + ' = ' +  @id_value
exec @strsql

I think I ran into it before that it would just go ahead and execute this if the user had execute permissions on the stored procedure even though he did not have delete permissions on the table.

I was thinking I could do some sort of check first to make sure the user has delete permissions, if not exit my stored proc.



0
 
LVL 7

Expert Comment

by:Lori99
ID: 10727179
Good point.  Even though you mentioned this was a stored procedure, I didn't take that into account.  You could use SYSTEM_USER to determine who is executing the procedure.  Then do as kiprimshot mentions above and use sp_helprotect and sp_helprolemember to figure out if they are allowed to delete from the table.  It seems like there should be a better way.  I'll keep thinking about it.
0
 
LVL 7

Accepted Solution

by:
Lori99 earned 400 total points
ID: 10727370
I found something you can use.  There is a PERMISSIONS function that will validate a user's object permissions.  The syntax is something like this.

if PERMISSIONS(OBJECT_ID(@table_name)) &0x10 = 0x10
  <user can delete, go ahead and delete>
else
  <user can't delete, kick them out of here>

The bitmap used varies based on what object permissions you want to check.  See this link for more information on syntax and usage.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_pa-pz_6f78.asp
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In today’s complex data management environments, it is not unusual for UNIX servers to be dedicated to a particular department, purpose, or database.  As a result, a SAS® data analyst often works with multiple servers, each with its own data storage…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now