question

Wang avatar image
Wang asked

Alter table switch

I am getting the error Msg 11404, Level 16, State 1, Line 2 ALTER TABLE SWITCH statement failed. Target table XXXX is referenced by 10 indexed view(s), but source table XXX' is only referenced by 5 matching indexed view(s). Every indexed view on the target table must have at least one matching indexed view on the source table. when trying to switch destination table to source table. I have checked that all indexes are the same in both source and destination tables plus dependencies and also in the same file group. Also checked the constraints and they are all fine. What are the other possible issues that I should be on the look out for? Thanks again! C!
tablespartitioning
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

SirSQL avatar image
SirSQL answered
By the looks of things you have multiple indexed views. You'll need to match those up in order to be able to perform the switch (it's not just the indexes on the table that have to line up)
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Wang avatar image Wang commented ·
One example of the indexes created for the views is as follows: CREATE UNIQUE CLUSTERED INDEX [PK_FCT_TRANSFH_TBL] ON [staging].[FCT_TRANSFH_TBL] ([YEAR], [REPORTER], [HS], [SRC_01_FLAG]) ON CWDC_PG{@V_YEAR} When I comment out where the index is to be aligned on the filegroup i.e. --ON CWDC_PG{@V_YEAR}, I get no error on the alter table switch. I thought that part of aligning the indexes was ensuring its aligned to the partition of the table its a dependent on, right? If there is nothing wrong with my syntax then what are the other possible issues?
0 Likes 0 ·
Wang avatar image
Wang answered
I have checked the views too and created the indexes for them on the partition. Do functions and stored procedures affect the switch too?
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

SirSQL avatar image SirSQL commented ·
No, functions and procedures don't matter. You just have to ensure that the schema matches up.
0 Likes 0 ·
Wang avatar image Wang commented ·
Please explain further on multiple indexed views. How about the statistics? Is there a statement to run to ensure the statistics are copied or not to be copied?
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.