Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I'm working with MSSQL Server Management Studio 2008 and I need to expose a view to a third party for their data reconciliation. I have created the appropriate view but I'm having trouble creating a user and giving that user appropriate permissions to select from the view.

I followed the wizards for creating a login and a user and then added my view in the Securables section with the grant box checked for select. Everything seemed fine but when I logged in as that user and tried to do a "Select * from MyViewName" it told me that the select permission was denied.

I just recreated the user (this time just using SQL instead of the wizard) and explicitly granted select permissions and now it is giving me the error: Msg 916, Level 14, State 1, Line 2 The server principal "username" is not able to access the database "unrelated_db" under the current security context. (I don't know why it's trying to access the unrelated database...)

I really don't know where to go from here. Again, basically all I need is to create a user that I can give to the third party to have them connect to our database and select from this view.

share|improve this question
3  
Does the view reference base tables in other databases? If so, you'll have to deal with ownership chaining. Also, make sure that you currently have the database context set to the desired database. –  Thomas Stringer Apr 27 '12 at 19:24
    
@Shark is right, I missed the context of the "unrelated_db" - I thought that you simply meant "not their default db". If they get access denied on that other database, then the view must reference that database somehow (perhaps through other views or synonyms). –  Aaron Bertrand Apr 27 '12 at 19:32
    
Is there some way to see the dependencies for a view? I mean, as far as I know, everything from the view should be contained within the main database. The view is just three fields, two coming from a payment details table and one coming from a join to the account details table but both tables are in the same database (and the same database as the view). –  velojason Apr 27 '12 at 21:24
    
Can you generate a script for the two tables (and the view) and show us? Can you also verify that the payment details and account details tables are in fact tables and not synonyms or views? –  Aaron Bertrand Apr 28 '12 at 17:06
    
Just open SQL and goto security tab and add the user if already there then remove and again add the user. –  user16910 Jan 10 '13 at 11:17
add comment

2 Answers

create login YourTpvLogin with password = 'enter new password here'
go

use SomeDb
go

create user YourTpvUser for login YourTpvLogin
go

grant select on YourView to YourTpvUser
go

You can test this out by doing the following:

execute as user = 'YourTpvUser'
go

select *
from YourView

revert
go
share|improve this answer
    
Apparently this doesn't recursively grant privileges. The user is allowed to select from "YourView" but if "YourView" depends on other relations/databases, it will fail: The server principal "YourTpvUser" is not able to access the database "OtherDb" under the current security context. –  lilalinux Aug 26 '13 at 14:29
add comment

Please don't use the UI for this. It's a confusing mess.

It sounds to me like what you want is to create a user in a database, for a specific login, who only has permissions to select from one view. So, since you already have the login created:

USE your_db;
GO
CREATE USER username FROM LOGIN username;
GO
GRANT SELECT ON dbo.MyViewName TO username;
GO

EDIT here is an example of a script that will lead to the error you mention.

First, create some table in the unrelated_db:

CREATE DATABASE unrelated_db;
GO
USE unrelated_db;
GO
CREATE TABLE dbo.foo(bar INT);
GO

Now create a relatively restricted login:

USE [master];
GO
CREATE LOGIN username WITH PASSWORD='foo', CHECK_POLICY = OFF;
GO

Now create a database where the view will live, and add the login as a user:

CREATE DATABASE velojason;
GO
USE velojason;
GO
CREATE USER username FROM LOGIN username;
GO

Now create a function that will reference the table in the other database, and a synonym to the other table:

CREATE FUNCTION dbo.checkbar()
RETURNS INT
AS 
BEGIN
    RETURN 
    (
      SELECT TOP (1) bar 
        FROM unrelated_db.dbo.foo 
        ORDER BY bar
    );
END
GO
CREATE SYNONYM dbo.foo FOR unrelated_db.dbo.foo;
GO

Now create a local table:

CREATE TABLE dbo.PaymentDetails
(
  PaymentID INT
);
GO

Now create a view that references the table, the function and the synonym, and grant SELECT to username:

CREATE VIEW dbo.SomeView
AS
  SELECT 
    p.PaymentID, 
    x = dbo.checkbar(), -- function that pulls from other DB
    y = (SELECT bar FROM dbo.foo) -- synonym to other DB
    FROM dbo.PaymentDetails AS p;
GO
GRANT SELECT ON dbo.SomeView TO username;
GO

Now try to execute as username and select only the local column from the view:

EXECUTE AS USER = 'username';
GO
  -- even though I don't reference any of the columns 
  -- in the other DB, I am denied SELECT on the view:
SELECT PaymentID FROM dbo.SomeView;
GO
REVERT;
GO

Result:

Msg 916, Level 14, State 1, Line 3
The server principal "username" is not able to access the database "unrelated_db" under the current security context.

Now change the view to not reference any external objects, and run the above SELECT again, and it works:

ALTER VIEW dbo.SomeView
AS
  SELECT 
    p.PaymentID 
    --x = dbo.checkbar(),
    --y = (SELECT bar FROM dbo.foo)
    FROM dbo.PaymentDetails AS p;
GO

Short of showing us the scripts for the Payment Details, Account Details and MyView objects, maybe you can let us know if this query returns any results. You can find references to various objects through the catalog view sys.sql_expression_dependencies, but this view is not perfect - I believe it depends on all the views being refreshed (in the case where views reference other views, for example, or underlying schema has changed) in order to be accurate.

DECLARE 
  @dbname   SYSNAME = N'unrelated_db',
  @viewname SYSNAME = N'dbo.SomeView';

SELECT DISTINCT 
    [This object] = 
    OBJECT_SCHEMA_NAME([referencing_id]) 
      + '.' + OBJECT_NAME([referencing_id]), 
    [references this object] = 
    OBJECT_SCHEMA_NAME([referenced_id]) 
      + '.' + OBJECT_NAME([referenced_id]), 
    [and touches this database] = referenced_database_name,
    [and is a(n)] = o.type_desc,
    [if synonym, it references] = s.base_object_name
FROM sys.sql_expression_dependencies AS d
LEFT OUTER JOIN sys.objects AS o
ON o.[object_id] = d.referenced_id
LEFT OUTER JOIN sys.synonyms AS s
ON d.referenced_id = s.[object_id]
AND s.base_object_name LIKE '%[' + @dbname + ']%'
WHERE OBJECT_ID(@viewname) IN (
        referenced_id, 
        referencing_id, 
        (SELECT referencing_id FROM sys.sql_expression_dependencies 
        WHERE referenced_database_name = @dbname)
) OR referenced_database_name = @dbname;

SQL Server isn't just going to try to access unrelated_db for the fun of it... there must be some tie to that database from the view you're trying to use. Unfortunately if we can't see the view definition and more details about the objects it touches, all we can do is speculate. The two main things I can think of are synonyms or functions that use three-part names, but seeing the actual scripts will give us a much better idea instead of guessing. :-)

You may also want to check sys.dm_sql_referenced_entities, however this function returns nothing useful in the example above.

share|improve this answer
    
Very nice answer, +1. –  Thomas Stringer Apr 28 '12 at 18:16
    
@Shark Thanks sir. My original answer was gnawing at me overnight, so I had to improve it. :-) –  Aaron Bertrand Apr 28 '12 at 18:18
    
Ha! It's a curse and a blessing, my friend. –  Thomas Stringer Apr 28 '12 at 18:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.