Search code examples
sql-server

SQL Server 2016 DB_CHAINING with Views


Over night it seems that we've started having users that can no longer read views in our databases where the target table is in another database. I've exhausted everything I can find so far to find the reason behind this, but I've simply gotten stuck now and would require some assistance.

In order for views to work like this, where the end user does not have explicit grants to SELECT on both databases, the option to have DB_CHAINING needs to be on. This is on for all the databases in question. Another prerequisite is to have the same database owner, which is also checked and verified to still be the case. The end users must also have connect grants to the target database where the table is, which they also do.

This is the query that checks the owner of the databases, and the DB_CHAINING option:

SELECT 
    name, 
    suser_sname(owner_sid) AS owner_name,
    is_db_chaining_on,owner_sid,
    owner_sid
FROM 
    sys.databases
WHERE 
    name IN ('TargetDB', 'ViewDB');

I've also ensured that there is no explicit grants denying access, which has not changed since yesterday and the response is still no. With above in mind, I cannot figure out what is wrong.

Microsoft SQL Server 2016 - 13.0.5893.48


Solution

  • The schema that owned the objects was not the same as the DB_OWNER. The issue was resolved by sorting this. The reason why this was not available in the logs was that the change of DB_OWNER was done with the SP for it, and it did not log the schema owner change. When reverting it, only the DB_OWNER was reverted.

    Credit to @Thom A for asking the right question.