SQL存储过程删除并创建索引

时间:2014-08-21 00:05:23

标签: sql sql-server stored-procedures indexing

我需要截断一个表来刷新数据,但是这个表的索引视图取决于它。我试图删除索引,然后截断表,然后重新创建索引。我收到一个错误,即非聚集索引不存在或者我没有权限删除它们...代码存在于下面显示的存储过程中。我假设它与存储过程中的执行顺序有关,因为手动执行代码(drop / truncate / create)。

DROP INDEX [IDX_VDetailEmergency] ON [dbo].[vFactEmergencySummary] WITH ( ONLINE = OFF )
DROP INDEX [IDX_VDetailEmergency_facility_refno] ON [dbo].[vFactEmergencySummary] WITH ( ONLINE = OFF )
DROP INDEX [IDX_VDetailEmergency_mode_of_separation_refno] ON [dbo].[vFactEmergencySummary] WITH ( ONLINE = OFF )
DROP INDEX [IDX_VDetailEmergency_period_refno] ON [dbo].[vFactEmergencySummary] WITH ( ONLINE = OFF )

truncate table DetailEmergency

CREATE UNIQUE CLUSTERED INDEX [IDX_VDetailEmergency] ON [dbo].[vFactEmergencySummary] 
(
    [facility_refno] ASC,
    [period_refno] ASC,
    [mode_of_separation_refno] ASC,
    [ed_visit_type_refno] ASC,
    [triage_category] ASC,
    [UDG_refno] ASC,
    [URG_refno] ASC,
    [URG_MDB_refno] ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

CREATE NONCLUSTERED INDEX [IDX_VDetailEmergency_facility_refno] ON [dbo].[vFactEmergencySummary] 
(
    [facility_refno] ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF,     DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

CREATE NONCLUSTERED INDEX [IDX_VDetailEmergency_mode_of_separation_refno] ON [dbo].[vFactEmergencySummary] 
(
   mode_of_separation_refno ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

CREATE NONCLUSTERED INDEX [IDX_VDetailEmergency_period_refno] ON [dbo].[vFactEmergencySummary] 
(
   period_refno ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

1 个答案:

答案 0 :(得分:0)

如果您可以删除并重新创建表格,则可以使用SQL Server Management Studio执行以下步骤:

  1. 右键单击表格可用的数据库
  2. 点击任务 - >生成脚本
  3. 按“下一步”导航至选择对象
  4. 选择特定数据库对象
  5. 选择您的表格
  6. 高级
  7. 在列表中找到脚本DROP并创建,并将下拉列表更改为脚本DROP并创建
  8. 如果要在脚本中包含索引或约束等,请向下滚动
  9. 确定并选择保存到新查询窗口以在新查询窗口中查看脚本
  10. 下一步完成
  11. 快乐管理!