How to create schema bound, cross-database view in SQL Server - sql-server

I am attempting to create an indexed view on SQL Server 2008. I have a master database in which I cannot make any changes to (in terms of adding tables, views, etc.). However, I need to create some different views for various reasons that need to work with live data.
I have created a new database along side my master database so I can create views there. I am able to create views just fine, but I want to index some of the larger views. However, when I try to create a schema bound view cross-database, I receive the following error:
Cannot schema bind view 'dbo.Divisions' because name
'master.dbo.hbs_fsdv' is invalid for schema binding. Names must be in
two-part format and an object cannot reference itself.
Since I am going cross-database with the views, I have to reference the name in three-part format.
My creation statement for the view:
CREATE VIEW dbo.Divisions WITH SCHEMABINDING AS
SELECT master.dbo.hbs_fsdv.seq_ AS DivisionID,
master.dbo.hbs_fsdv.fs_division_desc_ AS Description
FROM master.dbo.hbs_fsdv
How can I create an indexed cross-database view in SQL Server?

Plain and simple. You can't. From the MSDN page:
The view must reference only base tables that are in the same database as the view.
https://msdn.microsoft.com/en-us/library/ms191432.aspx

Although (per the docs) it cannot be done directly with a simple SQL statement, this use case is very common and has a solution.
The architecture would have to involve caching the remote tables into your centralized database, and building the indexed view on top of them.
Some good notes on this can be found here:
What is the best way to cache a table from a (SQL) linked server view?
and
https://thomaslarock.com/2013/05/top-3-performance-killers-for-linked-server-queries/

Related

Can a Sql Server View reference a table in a different database?

I have a third party Sql Server data, which I need to use as a data source for my custom database, without duplicating data.
One thing, which comes to mind is to create a View in my custom database, which would reference one or more tables from this third party database.
Is this possible with Sql Server 2014?
Yes, as long as they're on the same server, TSQL: Create a view that accesses multiple databases . If they're on different servers, then you'd have to create a linked server, which I wouldn't suggest unless you're aware of the pitfalls.
Yes very much it is possible but you need to fully qualify the table name like
create view testview
as
select * from db_name.schema_name.table_name

Indexed View in SQL Server

Can I create an index on a view which references synonyms which point to a linked server? Since the view isn't based on local tables I wasn't sure if it was possible.
Nope.
You can't schema bind a view that references external objects and this is only one prerequisite for indexing it.
An indexed view can only reference objects within the database.

SQL Data Tools: Schema view showing Synonyms as Tables

Using SSDT in VS2012 I have created a number of synonyms from a reporting db to a transactional db.
When viewing the schema view - the synonyms appear under the tables subdir, with the SQLCMD variables as part of the table name.
I can kind-of understand the logic of this, however it does pollute the layout and makes it harder to find the tables I'm looking for?
nb. I cant expand any of these tables (ie they dont show the columns of the synonym) and there is no option to delete (right click only gives Refresh and properties)
Hmmm... Looking further at this it seems there are more issues with the schema view. The image below shows the views of the reference database. (there is no actual reference to these views in the database itself) - it also shows the views of the Master Database (which is also a db reference in the project?)

How to create views in Access 2007?

Now in SQL views are tables which can be used for data abstraction (showing specific data to required users).
Also you can edit data and insert data into the original table through views. What I want is an equivalent of this in access.
I have been able to create a view table in access but the problem is that it cannot be used to update the record set.
In Jet databases, the equivalent of a view is a saved query. You should be able to update the base tables through the query, provided that the query obeys certain rules. These rules are documented in the Access 2007 help. If you provide your view definition and some details about the base table(s), we can give some specific advice about why the query isn't updatable.

HBM2DDL -- Create a database view instead of a Table?

All,
Is there some setting that I can tell hbm2ddl to run a view creation statement instead of create a table when generating the database schema?
I'm creating my database schema using the wonderful hbm2ddl tool, but I have one issue. I need to flatten some of the tables into views to aid searching the database, and hql would be overly complex a solution. I've created Entity objects pointed at these views, in order to fetch search results via hibernate. This all works fine, until hbm2ddl is used. In an empty database schema, hbm2ddl will create the database schema based on the jpa annotations, unfortunately, it will also create my views as tables. Is there some setting that I can tell hbm2ddl to run a view creation statement instead of create a table? In lieu of that, is there a way to tell hbm2ddl to skip table creation for an entity (exclude, or something)?
Thanks!
To my knowledge, and this is unfortunate, Hibernate doesn't support things like creating views instead of tables nor validating a schema containing views. See issues like HHH-1872, HHH-2018 or HHH-1329.

Resources