CREATE TABLE test2 ( id INTEGER, name VARCHAR(10), family VARCHAR(10), amount INTEGER) CREATE VIEW dbo.test2_v WITH SCHEMABINDING AS SELECT id, SUM(amount) as amount -- , COUNT_BIG(*) as tmp FROM dbo.test2 GROUP BY id CREATE UNIQUE CLUSTERED INDEX vIdx ON test2_v(id)
I have error with this code:
Cannot create index on view 'test.dbo.test2_v' because its select list does not include a proper use of COUNT_BIG. Consider adding COUNT_BIG(*) to select list.
I can create view like this:
CREATE VIEW dbo.test2_v WITH SCHEMABINDING AS SELECT id, SUM(amount) as amount, COUNT_BIG(*) as tmp FROM dbo.test2 GROUP BY id
But I'm just wondering what is purpose of this column in this case?
Also, it is possible to create non-clustered indexes on a view, providing more possibilities to enhance the queries calling the 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.
An indexed view has a unique clustered index. The unique clustered index is stored in SQL Server and updated like any other clustered index. An indexed view is more significant compared to standard views that involve complex processing of large numbers of rows, such as aggregating lots of data, or joining many rows.
What does Indexed view mean? This is also a simple view which has a unique clustered index defined on it. When a clustered index is created on a view, the result set is stored in the database just like a table with a clustered index.
You need COUNT_BIG in this case because of the fact you are using GROUP BY.
This is one of many limitations of Indexed Views and because of these restrictions, Indexed Views can't be used in many places or the usage of it is NOT as effective as it could have been. Unfortunately, it is how it works currently. Sucks, it narrows the scope of usage.
http://technet.microsoft.com/en-us/library/cc917715.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With