By submitting my Email address I confirm that I have read and accepted the Terms of Use and Declaration of Consent.

By submitting your personal information, you agree that TechTarget and its partners may contact you regarding relevant content, products and special offers.

You also agree that your personal information may be transferred and processed in the United States, and that you have read and agree to the Terms of Use and the Privacy Policy.

indexes of a given table. Sp_ Dropindexes takes a single parameter (@objname) and drops all of its user-created indexes.

Note that I created this procedure in SQL Server 2000 using temporary tables. Here goes:

-- Scripted by Sachin Wable
-- Use this script to Drop All user created indexes giving
-- table name as Parameter
CREATE proc sp_Dropindex
@objname nvarchar(776)
as
declare @NAME varchar(50),
@objid int,
@dbname sysname
if @dbname is not null and @dbname <> db_name()
begin
raiserror(15250,-1,-1)
return (1)
end
select @objid = object_id(@objname)
if @objid is NULL
begin
raiserror(15001,-1,-1,@objname)
return (1)
end
declare db cursor for
select name from sysindexes where
id=(select id from sysobjects where type ='u' and name=@objname)
and indid > 0 and indid < 255 and (status & 64)=0
open db
fetch next from db INTO @NAME
if @@fetch_status = -1
Begin
raiserror(15472,-1,-1) --'Object does not have any indexes.'
close db
deallocate db
return (1)
End
while @@fetch_status=0
BEGIN
if @@fetch_status=0
begin
exec('Drop index '+@objname+'.'+@name)
END
fetch next from db INTO @NAME
end
close db
deallocate db
Go

Reader feedback

Art D. notes: "This SP will drop PK's too, as well as indices on system tables."

0 comments

Register

Login

Forgot your password?

Your password has been sent to:

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy