tags:

views:

1893

answers:

5

This one is on "Experts Exchange". Let's open the answer up to the rest of the world.

I have a view which is selecting rows from a table in a different database. I'd like to grant select access to the view, but not direct access to the base table. The view has a where clause restricting the number of rows.

Can I grant select to the view and not the base table, or do I need to switch to a stored procedure (not preferred at all).

Thanks!

+1  A: 

GRANT SELECT ON [viewname] TO [user]

should do it.

Barry Fandango
Actually, the user doesn't have access to the base table referred to in the view, so I get an error stating I don't have access in the current security context.
Jiyosub
A: 

You can grant permissions on a view and not the base table. This is one of the reasons people like using views.

Have a look here: GRANT Object Permissions (Transact-SQL)

cciotti
Because the user doesn't have access to the base table the view is referring to, even though I grant select privilege to the view, I still get select permission was denied to the base table. I don't want to grant direct privileges on the base table.
Jiyosub
I tested this under SQL Server 2005. You grant the user public on the database then permissions on the view. In testing this, the user does not have select on the table and can select from the view. The user cannot even see the tables in the UI.
cciotti
Hmmm, 2 things differ in my setup. SQL 2008 and the view is referencing a table in another database. However, the database is on the same server and the user has public on the database.
Jiyosub
I don't have a 2008 server to test with, good luck.
cciotti
+3  A: 

As you state in one of your comments that the table in question is in a different database, then ownership chaining applies. I suspect there is a break in the chain somewhere - check that link for full details.

RoadWarrior
This was spot on! Thanks!
Jiyosub
A: 

I tried this in one of my databases.

To get it to work, the user had to be added to the database housing the actual data. No rights were needed, just access.

Have you considered keeping the view in the database it references? Re usability and all if its benefits could follow.

Sam
A: 

Question Removed. I figured out the answer.

JDamian