gpt4 book ai didi

sql - 插入具有列存储索引的表时出错

转载 作者:行者123 更新时间:2023-12-02 09:17:05 24 4
gpt4 key购买 nike

大家好,有人可以告诉我为什么会出现这个错误,这里是存储过程

IF EXISTS(SELECT 1 FROM SYS.procedures WHERE NAME = 'InsertCategory')
BEGIN
DROP PROC InsertCategory
END

GO
Create proc dbo.InsertCategory
@category nvarchar

As
Begin

Declare @rs Int
Set @rs = 0

If(@category is not null)
BEGIN
Alter Index Category_Clus On Categories Disable
Set @rs =1
END
IF(@rs = 1)
BEGIN
Insert Into Categories (
Category)
Values (
@category)
END

Alter Index Category_Clus on Categories Rebuild

End

即将发生的错误是

Msg 35330, Level 15, State 1, Procedure InsertCategory, Line 18
INSERT statement failed because data cannot be updated in a table with a
columnstore index. Consider disabling the columnstore index before
issuing the INSERT statement, then rebuilding the columnstore index
after INSERT is complete.

我正在执行存储过程

Exec InsertCategory 'D'

我在类别表上使用非聚集列存储索引

最佳答案

根据错误消息,您的表上似乎有非聚集列存储索引,并且使用的是 SQL Server 2014 或更早版本。具有列存储索引的表在更高版本中是可更新的。

除非您使用动态 SQL 执行 INSERT,否则您将无法在与 INSERT 语句相同的过程中禁用和重建索引。这是因为 proc 是在适当的索引的情况下进行编译的,并且在早期版本的 SQL Server 中不允许对具有列存储索引的表进行插入。例如:

CREATE proc dbo.InsertCategory
@category nvarchar
AS

DECLARE @rs int;
SET @rs = 0

IF @category IS NOT NULL
BEGIN
ALTER INDEX Category_Clus ON Categories DISABLE;
SET @rs =1;
END
IF @rs = 1
BEGIN
EXEC sp_executesql N'INSERT Into dbo.Categories (Category) Values (@category)'
, N'@category nvarchar(1)'
, @category = @category;
END;

ALTER INDEX Category_Clus ON Categories REBUILD;
GO

出于性能和并发性的原因,禁用和重建索引不应该为单例插入例行执行。另外,我建议您为 nvarchar 参数指定显式长度,因为默认长度为 1。

关于sql - 插入具有列存储索引的表时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45895980/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com