So in Part II of this article I had created a View so that I could then create an index on this view to see if that was going to give me the expected results. I did create the view, but not the index. So you probably already know. These columns are non-deterministic and as a result the create index statement fails.
CREATE
(
[ID]
)
GO
Msg 1963, Level 16, State 1, Line 1
Cannot create index on view "TESTDATABASE.dbo.vDateSelection". The view contains a convert that is imprecise or non-deterministic.
So that ends the questions...for now about indexed views and the effect of the results.
So here comes the results of how this works with no changes to the actual syntax of the query. I will introduce them one a time and then we will take a deep dive into why this behavior works sometimes and not all. And if this is correct behavior, is there the possibility that something that is executed today could fail in the future.
So looking at the scripts from my previous post there are some obvious elements missing form the schema. How about a primary key on each of the tables...and lets make these clustered unique as well.
[ID] [int]
[LookupTypeID] [int]
[LookupValue] [varchar]
[LookupType] [varchar]
We can then also add the foreign key constraint for the ID. As you can see I have modified testTable1 by adding an Identity columns for my primary key.
ALTER
FK_testTable1_testTable2
LookupTypeID
ID
With these changes we are close, Well I thought...but I am still getting the same error.
PRINT
SELECT
a
FROM
[dbo]
WHERE
DateSelection
WITH
AS
vDateSelection DateSelection
The results were the same.
So lets go the full distance now and add the index to the LookupTypeID Field to complete the cycle.
--Add Non Clustered Index.
[LookupTypeID]
And BAM. Still the same results. What could be going on here...In the article I will try to understand this myself as well as get some commentary from some insiders...
Stay Tuned.