Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d922909

Browse files
committed
Add sp_DropIndexes stored procedure
1 parent 0b2c068 commit d922909

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

‎Stored_Procedure/sp_DropIndexes.sql‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
IF OBJECT_ID('dbo.sp_DropIndexes', 'P') IS NULL
2+
EXECUTE ('CREATE PROCEDURE dbo.sp_DropIndexes AS SELECT 1;');
3+
GO
4+
5+
6+
ALTER PROCEDURE dbo.sp_DropIndexes(
7+
@debug BIT = 1
8+
)
9+
/*
10+
.SYNOPSIS
11+
Drop indexes and statistics
12+
13+
.DESCRIPTION
14+
Drop indexes and statistics
15+
16+
.PARAMETER @debug
17+
Just generate DROP statements withou executing it
18+
19+
.EXAMPLE
20+
EXEC sp_DropIndexes @debug = 0;
21+
22+
.LICENSE MIT
23+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
24+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
25+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26+
27+
.NOTE
28+
Author: Brent Ozar
29+
Created date: 2017年04月11日
30+
Version: 1.0
31+
32+
Author: Konstantin Taranov
33+
Modified date: 2018年01月03日
34+
Version: 1.1
35+
*/
36+
AS
37+
SET NOCOUNT ON;
38+
39+
CREATE TABLE #commands (ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED, Command NVARCHAR(2000));
40+
DECLARE @CurrentCommand NVARCHAR(2000);
41+
42+
INSERT INTO #commands (Command)
43+
SELECT 'DROP INDEX [' + i.name + '] ON [' + s.name + '].[' + t.name + ']'
44+
FROM sys.tables t
45+
INNER JOIN sys.indexes i ON t.object_id = i.object_id
46+
INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
47+
WHERE i.type = 2;
48+
49+
INSERT INTO #commands (Command)
50+
SELECT 'DROP STATISTICS ' + SCHEMA_NAME([t].[schema_id]) + '.' + OBJECT_NAME([s].[object_id]) + '.' + s.[name]
51+
FROM sys.[stats] AS [s]
52+
JOIN sys.[tables] AS [t]
53+
ON s.[object_id] = t.[object_id]
54+
WHERE [s].[name] LIKE '[_]WA[_]Sys[_]%'
55+
AND OBJECT_NAME([s].[object_id]) NOT LIKE 'sys%';
56+
57+
IF @debug = 1
58+
SELECT * FROM #commands;
59+
ELSE
60+
BEGIN
61+
DECLARE result_cursor CURSOR FOR
62+
SELECT Command FROM #commands;
63+
64+
OPEN result_cursor
65+
FETCH NEXT FROM result_cursor into @CurrentCommand
66+
WHILE @@FETCH_STATUS = 0
67+
BEGIN
68+
69+
EXEC(@CurrentCommand);
70+
71+
FETCH NEXT FROM result_cursor into @CurrentCommand
72+
END
73+
--end loop
74+
75+
--clean up
76+
CLOSE result_cursor;
77+
DEALLOCATE result_cursor;
78+
END;
79+
GO

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /