Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot create index on view because the view is not schema bound error 1939

The third part of this assignment I'm working on says, "Create and index the existing Northwind view called “dbo.Orders Qry” based on the columns OrderID and EmployeeID."

It is expected that I will get an error; however, my instructor only told us to Google it. I did, but scheme binding isn't even in this week's lesson or any others, and the things I've found are too in-depth for me to understand.

Is this an issue of me not checking a box or changing a setting somewhere?

like image 913
Sam Avatar asked May 10 '12 21:05

Sam


People also ask

Can we create index on view without Schemabinding?

You Can't… The view is created with the WITH SCHEMABINDING option. You can't always predict what the query optimizer will do. If you're using Enterprise Edition, it will automatically consider the unique clustered index as an option for a query – but if it finds a “better” index, that will be used.

How do you make a view schema bound?

Schema bound views must reference base tables only. They cannot reference other views. Schema bound views must be in the same database and schema as the referenced base tables. Multiple base tables utilize a two-part naming convention for specifying the columns for the view definition.

Can you create index on view?

Indexes can only be created on views which have the same owner as the referenced table or tables. This is also called an intact ownership-chain between the view and the table(s). Typically, when table and view reside within the same schema, the same schema-owner applies to all objects within the schema.

What is schema binding in SQL Server?

SCHEMABINDING. Binds the view to the schema of the underlying table or tables. When SCHEMABINDING is specified, the base table or tables cannot be modified in a way that would affect the view definition.


2 Answers

Sounds like this is describing an indexed view, you can read up on them at Microsoft's site here. Microsoft enabled this capability starting with SQL 2005.

In the text for the view definition, you'd need to add the words WITH SCHEMABINDING just after the CREATE VIEW statement, for example:

CREATE VIEW dbo.MyView
WITH SCHEMABINDING

AS

SELECT a, b, c
FROM dbo.MyTable

To add indexing, you'd add a statement to the view definition similar to this:

-- Create an index on the view.
CREATE UNIQUE CLUSTERED INDEX IDX_MyView_ClusteredIndex
ON dbo.MyView(a, b, c)

GO
like image 196
Darth Continent Avatar answered Sep 20 '22 15:09

Darth Continent


I was looking for exactly what was posted by Darth Continent. This worked like a charm, however, it was an entirely different situation. I believe that the answer above should at least be credited as the resolution, and if not a follow up would be great.

like image 30
Anthony Mason Avatar answered Sep 19 '22 15:09

Anthony Mason