Is it possible to replace a table with a view (with the same name)?

I have 2 databases .... a and b

I have a table "t" in both of these databases.

Now I delete table t from database "b".

I create a view "t" (see that the name of the view is the same as the remote table) in database "b" ... and this view refers to table "t" in database "a".

I have a dotnet application that points to database "b". It has inline queries .... So I can leave a link like this "bt" in inline queries. I mean now will this refer to "t" instead of the table "t"?

+3
source share
1 answer

Yes, but the table you want to replace must be deleted or renamed first - only one object can have a name.

Using:

CREATE VIEW b.dbo.t AS
  SELECT a.*
    FROM a.dbo.t a

The only caveat is that users in d-database B may be granted SELECT privilege:

GRANT SELECT ON b.dbo.t TO user

Ideally create a role, then highlight SELECT for the role, which you can then add to users.

+5
source

Source: https://habr.com/ru/post/1780563/


All Articles