tags:

views:

130

answers:

4

I have one database table which contains 8 columns. One of the columns is called IsAdmin (its data type is Bit). I have to show the user list in a grid view if and only if the signed-in user is an admin.

How can I do this? Which clause is used for this?

A: 

I would probably recommend that this be done in the application instead of in SQL. Let the application do the checks on who is an admin and place that into a session or cookie and when the page loads do a check of the session/cookie to ensure the user is an admin and if they are show the information.

If you want it in SQL you should provide some additional information such as table structure and table names.

Matt

Lima
+1  A: 

To me, doesn't sound like you need a clause but should be handled by business logic in your application.

e.g. if you only want to show the list if the signed-in user is an admin, then there's no point in running the query if the user is NOT an admin - it's a roundtrip to the db that is unneccessary.

Instead, in your application just have some logic that says "if signed-in user is an admin the populate the grid view, else don't"

AdaTheDev
A: 

The solution is too simple,as you need to select specific data if the user is in role of admin and else another data may be selected or nothing, so do the following code :

select * from [UserList] where IsAdmin = @UserRole and UserID = @UserID

In this case you will select data relevant to this user ID and his role.

Hope that this is helpful according to my understanding of the problem.

Ahmy
+1  A: 

Not entirely sure that I understand you correctly. Is the "user list" that you're looking to display also the table that you're selecting from? My understanding is that this is what you need:

if exists (
  select *
  from MyUserList
  where IsAdmin = 1
    and UserName = SUSER_NAME()
)
begin
  select *
  from MyUserList /* Or whatever you need to do if the user is admin */
end
else
begin
/* Do whatever you need to do if the user is not an admin */
end
Cobus Kruger