SQL Server 2008 R2 – How to Give Access to a View Without Table Access

sql serversql-server-2008-r2

I have a user (called myUser) which has db_datareader role on myCustomDatabase.

myCustomDatabase and MSDB are owned by sa user.

I've enable chaining (it's already enabled on MSDB):

ALTER DATABASE myCustomDatabase SET DB_CHAINING ON;

I created a view on myCustomDatabase as following:

CREATE VIEW myCustomDatabase.dbo.myView AS
    SELECT run_date  FROM msdb.dbo.sysjobhistory

I would like myUser be able to call this view, and not the following query:

SELECT * FROM msdb.dbo.sysjobhistory // Should not work
SELECT * FROM myCustomDatabase.dbo.myView // Should work

With described permissions I get following error:

The SELECT permission was denied on the object 'sysjobhistory', database 'msdb', schema 'dbo'.

Where am I wrong with db chaining?


Edit:

Here the script I run with succeed on a database test, but I still don't succeed to apply this changes on an already existing database:

CREATE DATABASE myCustomDatabase ;
GO

ALTER DATABASE myCustomDatabase SET DB_CHAINING ON;
USE myCustomDatabase ;
GO

CREATE VIEW dbo.myView AS
    SELECT run_date  FROM msdb.dbo.sysjobhistory
GO

// The login myUser already exists on my server
CREATE USER myUser FROM LOGIN myUser;

GRANT SELECT ON dbo.myView TO myUser;

Here the script I run on my already existing DB:

/*CREATE DATABASE thisDatabaseAlreadyExists ;
GO*/

ALTER DATABASE thisDatabaseAlreadyExists SET DB_CHAINING ON;
USE thisDatabaseAlreadyExists ;
GO

create  VIEW dbo.myView AS
    SELECT run_date  FROM msdb.dbo.sysjobhistory
GO

/* The user already exist, he's got db_datareader role on thisDatabaseAlreadyExists*/
/*CREATE USER myUser FROM LOGIN myUser;*/

GRANT SELECT ON dbo.myView TO myUser;

Best Answer

It was a problem about the database owner.

Even if you can see an owner on this interface: enter image description here

Be sure that an owner is entered into files tab: enter image description here