【发布时间】:2012-10-29 07:26:04
【问题描述】:
我在测试 sql server 2012 的列存储索引功能时玩得很开心。因为您无法使用此类索引更新/插入表,所以我阅读了一些选项:保留一个单独的表并为每个批量插入使用新分区或禁用索引,执行更新/插入,然后重建索引。
对于我的测试,我选择了后一个选项并最终得到了这个存储过程:
-- Disable the columnstore index.
ALTER INDEX [All_Columns_Columnstore_Index] ON [dbo].[Tick] DISABLE
-- Insert data into tick table from staging table.
insert into Tick
select [Date],
SymbolID,
Price
from TickTemporary
-- Delete data from staging table.
delete from TickTemporary
-- Enable (rebuild) the columnstore index.
ALTER INDEX [All_Columns_Columnstore_Index] ON [dbo].[Tick] REBUILD
如果我手动执行这些行,一切正常。但是,如果我运行该过程,我会收到无法在具有列存储索引的表上执行更新/插入的错误。
这是为什么?
更新:
我遵循了我之前接受的答案中的建议,但我仍然得到同样的结果。
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Disable the columnstore index.
EXEC DisableColumnStoreIndex
-- Insert data into tick table from staging table.
insert into Tick
select [Date],
SymbolID,
Price
from TickTemporary
-- Delete data from staging table.
delete from TickTemporary
-- Enable (rebuild) the columnstore index.
EXEC RebuildColumnStoreIndex
甚至尝试在存储过程调用周围放置“begin tran”和“commit tran”。
使用动态 sql 之类的:
declare @sql nvarchar(max)
set @sql =
'insert into Tick
select [Date],
SymbolID,
Price
from TickTemporary'
exec(@sql)
有效,但实际上,我想在没有动态 sql 的情况下过日子。这种情况下不可以吗?
【问题讨论】:
标签: sql-server stored-procedures insert-update columnstore