gpt4 book ai didi

sql-server-2008 - 在 SQL Server 数据库中删除索引

转载 作者:行者123 更新时间:2023-12-04 07:19:36 24 4
gpt4 key购买 nike

如何删除 SQL Server 2008 数据库中的索引?这是我到目前为止所得到的:

declare @procname varchar(500) 
declare cur cursor
for
select name from sysindexes
open cur
fetch next from cur into @procname
while @@FETCH_STATUS=0
begin
exec ('drop index ' + @procname)
fetch next from cur into @procname
end
close cur
deallocate cur

最佳答案

你需要使用这样的东西,因为 DROP INDEX 语句需要你指定表名:

-- define variables for index, schema and table name
DECLARE @indexname sysname
DECLARE @schemaname sysname
DECLARE @tablename sysname

-- declare variable for actual DROP statement
DECLARE @dropstatement NVARCHAR(1000)

-- declare cursor for iterating over all indexes
DECLARE index_cursor CURSOR LOCAL FAST_FORWARD
FOR
SELECT ix.name, t.name, s.name
FROM sys.indexes ix
INNER JOIN sys.tables t ON t.object_id = ix.object_id
INNER JOIN sys.schema s ON t.schema_id = s.schema_id
WHERE t.is_ms_shipped = 0

-- open cursor
OPEN index_cursor

-- get first index, table and schema name
FETCH NEXT FROM index_cursor INTO @indexname, @tablename, @schemaname

WHILE @@FETCH_STATUS = 0
BEGIN
-- define the DROP statement
SET @dropstatement = N'DROP INDEX ' + QUOTENAME(@indexname) +
N' ON ' QUOTENAME(@schemaname) + N'.' +
QUOTENAME(@tablename)

-- execute the DROP statement
EXEC sp_executesql @dropstatement

-- get next index, table and schema name
FETCH NEXT FROM index_cursor INTO @indexname, @tablename, @schemaname
END

CLOSE index_cursor
DEALLOCATE index_cursor

关于sql-server-2008 - 在 SQL Server 数据库中删除索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28565469/

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