You need to define the same indexed view on the staging table before you can switch, since the indexed view has it's own partition. To explain the reason for this, first imagine the following simple schema:
-- TWO SIMPLE TABLES TO SWITCH
CREATE TABLE dbo.T1 (ID INT NOT NULL CONSTRAINT PK_T1_ID PRIMARY KEY, Filler CHAR(1000) NULL);
CREATE TABLE dbo.T2 (ID INT NOT NULL CONSTRAINT PK_T2_ID PRIMARY KEY, Filler CHAR(1000) NULL);
GO
-- CREATE INDEXED VIEW ON FIRST TABLE
CREATE VIEW dbo.V1
WITH SCHEMABINDING
AS
SELECT ID, Filler
FROM dbo.T1;
GO
CREATE UNIQUE CLUSTERED INDEX IX_V1_ID ON dbo.V1 (ID);
GO
-- CREATE INDEXED VIEW ON SECOND TABLE
CREATE VIEW dbo.V2
WITH SCHEMABINDING
AS
SELECT ID, Filler
FROM dbo.T2;
GO
CREATE UNIQUE CLUSTERED INDEX IX_V2_ID ON dbo.V2 (ID);
GO
-- CREATE NON INDEXED VIEW ON FIRST TABLE
CREATE VIEW dbo.V3
WITH SCHEMABINDING
AS
SELECT ID, Filler
FROM dbo.T1;
GO
-- ADD SOME DATA TO SWITCH
INSERT dbo.T2 (ID) VALUES (1);
If I run this:
SELECT p.partition_id, o.object_id, o.Name, p.rows
FROM sys.partitions p
INNER JOIN sys.objects o
ON o.object_id = p.object_id
WHERE o.Name IN ('T1', 'T2', 'V1', 'V2', 'V3');
I get the following result:
partition_id object_id Name rows
72057594886553600 1948794250 T1 0
72057594886619136 1980794364 T2 1
72057594886684672 2012794478 V1 0
72057594886750208 2028794535 V2 1
Because the view V3
has no index defined on it, it has no entry in sys.partitions
. If I then run:
ALTER TABLE dbo.T2 SWITCH TO dbo.T1;
and query the partitions again I get the following result
partition_id object_id Name rows
72057594886553600 1980794364 T2 0
72057594886619136 1948794250 T1 1
72057594886684672 2028794535 V2 0
72057594886750208 2012794478 V1 1
You can see all that has happened is that the partitions have just switched between the corresponding tables/views. If you do not have the indexed view on the second table you would only get the following partitions:
partition_id object_id Name rows
72057594886553600 1948794250 T1 0
72057594886619136 1980794364 T2 1
72057594886684672 2012794478 V1 0
And the partition on V1
would have nothing to "switch" to.
Example on SQL Fiddle