문제

When I run the pseudocode below:

BEGIN TRANSACTION @TName
BEGIN TRY
    -- Disable columnstore index
    ALTER INDEX CSIX_MyTable ON dbo.MyTable DISABLE;

    MERGE INTO dbo.MyTable

...

I get this error:

MERGE statement failed because data cannot be updated in a table with a columnstore index. Consider disabling the columnstore index before issuing the MERGE statement, then rebuilding the columnstore index after MERGE is complete.

What I don't understand is that I clearly disable the index before I perform the MERGE. I've double, triple, and quadruple checked that I'm disabling the correct index. In fact, if I execute the ALTER INDEX statement by itself and then run the above block again the MERGE completes just fine. Any ideas as to why this is happening or how I can fix it?

EDIT:

I also have the following pseudocode that executes without problem:

BEGIN TRANSACTION @TName
BEGIN TRY
    -- Disable columnstore index
    ALTER INDEX CSIX_MyTable2 ON dbo.MyTable2 DISABLE;

    INSERT INTO #MyTempTable(Columns...)
    SELECT Columns...
    FROM (
        MERGE INTO dbo.MyTable
        ...
        OUTPUT ...
    ) A

...

The index gets disabled and the MERGE processes without a hitch. It appears that for some reason the wrapping INSERT statement appears to "trick" the compiler into executing the code.

I'm sure moving the ALTER INDEX statement to another task will solve the problem but my hope with this question is to try and discover WHY the code doesn't run as is.

도움이 되었습니까?

해결책

A guess: the code is being rejected at compile time. SQL takes your script, parses it, sees the merge, determines there's a columnstore index, and raises and returns the error... without being clever enough to "notice" that the index is disabled in the prior step.

I don't know that this is the problem, but I've seen issues like this before.

다른 팁

One of the limitation of using columnstore index is that you cannot update your table without disabling or dropping columnstore index from table.

One suggestion is that use disable columnstore index statement before transaction statement. Also you can try by dropping columnstore index and after than recreating it.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top