We have two databases (DB1, DB2) with the following view structure.
DB1:
- T1, T2, T3, ..... Tn (Tables)
- V1 (View consuming {T1, T2, T3 ..... Tn })
Inside DB2 we have to create a view V2 which will pull the data from DB1.dbo.V1
USE DB2;
CREATE VIEW dbo.V2 AS SELECT * FROM DB1.dbo.V1; -- with some business logic
Now the problem statement is, user created for the DB2 should not have any login access to DB1 but should be able to login on DB2 to consume the V2.
I don't want to disable the owner chain setting. Till now we have tried the below solution. But it enables the user to see the selected view (V1) inside DB1.
USE DB1;
CREATE View dbo.V1 AS SELECT * FROM T1; -- And some other tables according to business logic
CREATE USER testUser FOR LOGIN testUser;
GRANT SELECT ON dbo.V1 To testUser;
USE DB2;
CREATE View dbo.V2 AS SELECT * FROM DB1.dbo.V1; -- with some business logic
Please consider testUser is already present for DB2, I have just mapped it with the DB1 database.
This solution provides me the expected result. But it also allows testUser to login on the DB1 and fetch the data directly from DB1.dbo.V1
. Is there any way to deny login access to DB1 but at the same time allowing select access to V2 created inside the DB2?
What we are trying to do over here is to create an user who will have a complete access to DB2. User should be able to fetch the data using the internal views as explained above. But should not able to login to DB1. In Linux/Unix based systems admin can create an internal user which can be used for the access definitions but can not be used for the logging in into system. We are looking solution to implement something similar in SQL.
If we don't grant select access to V1, server throws permission error:
The SELECT permission was denied on the object V1, database 'DB1', schema 'dbo'.