-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet_DropStatistics_SQL.dsql
More file actions
84 lines (70 loc) · 3.27 KB
/
Get_DropStatistics_SQL.dsql
File metadata and controls
84 lines (70 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
CREATE PROC [dbo].[Get_DropStatistics_SQL] @Table_Name [VARCHAR](200),@Params_In_SQL_Out [VARCHAR](MAX) OUT AS
/*
IF OBJECT_ID ('dbo.Get_DropStatistics_SQL', 'P') IS NOT NULL DROP PROCEDURE dbo.Get_DropStatistics_SQL
GO
###################################################################################################################
Example:
-------------------------------------------------------------------------------------------------------------------
DECLARE @Params_In_SQL_Out VARCHAR(MAX) = 'No[]'
EXEC dbo.Get_DropStatistics_SQL '[dbo].[Customers]', @Params_In_SQL_Out
===================================================================================================================
Proc Description:
-------------------------------------------------------------------------------------------------------------------
This proc creating and returning DROP STATISTICS SQL statement for the table. It's dropping all User-defined statistics on the table.
@Table_Name - Name of the table to drop statistics on
@Params_In_SQL_Out - Param to return string.
-- can be: 'No[],NoPrint'
###################################################################################################################
*/
BEGIN
DECLARE @Error VARCHAR(MAX) = ''
DECLARE @Params VARCHAR(100) = ISNULL(@Params_In_SQL_Out,'')
IF @Table_Name IS NULL SET @Error = @Error + 'Table name cannot be NULL'
SET @Params_In_SQL_Out = '';
IF LEN(@Error) > 0
BEGIN
PRINT @Error
END
ELSE
BEGIN
DECLARE @Schema VARCHAR(100), @Table VARCHAR(200), @ColumnsCnt INT, @This_SQL_String VARCHAR(MAX) = '', @Indicat SMALLINT = 1
DECLARE @Dot INT = CHARINDEX('.',@Table_Name)
SELECT
@Schema = CASE WHEN @Dot = 0 THEN 'dbo' ELSE REPLACE(REPLACE(REPLACE(LEFT(@Table_Name,@Dot),'[',''),']',''),'.','') END,
@Table = CASE WHEN @Dot = 0 THEN REPLACE(REPLACE(@Table_Name,'[',''),']','') ELSE REPLACE(REPLACE(SUBSTRING(@Table_Name,@Dot + 1,200),'[',''),']','') END
IF OBJECT_ID('TempDB..#Table_STATS') IS NOT NULL DROP Table #Table_STATS;
WITH CTE_STATS AS
(
SELECT DISTINCT
s.[name] AS schemaName
,t.[name] AS [Table_name]
,ss.[name] AS [stats_name]
FROM sys.Tables t
JOIN sys.schemas s ON t.schema_id = s.schema_id
JOIN sys.stats ss ON ss.[object_id] = t.[object_id] AND ss.user_created = 1
JOIN sys.stats_columns sc ON sc.[object_id] = t.[object_id] AND ss.stats_id = sc.stats_id
JOIN sys.columns c ON t.[object_id] = c.[object_id] AND sc.column_id = c.column_id
WHERE t.[name] = @Table AND s.name = @Schema
)
SELECT
schemaName
,Table_name
,stats_name
,'DROP STATISTICS ' + schemaName + '.[' + Table_name + '].[' + stats_name + '];' AS SQL_String
, ROW_NUMBER() OVER(ORDER BY stats_name) AS RN
INTO #Table_STATS
FROM CTE_STATS
SET @Params_In_SQL_Out = ''
SELECT @ColumnsCnt = MAX(RN) FROM #Table_STATS
WHILE (@Indicat <= @ColumnsCnt)
BEGIN
SELECT @This_SQL_String = SQL_String FROM #Table_STATS WHERE RN = @Indicat
SET @Params_In_SQL_Out = @Params_In_SQL_Out + CHAR(13) + @This_SQL_String
SET @Indicat += 1
END
IF CHARINDEX('No[]',@Params) > 0
SET @Params_In_SQL_Out = REPLACE(REPLACE(@Params_In_SQL_Out,'[',''),']','')
IF CHARINDEX('NoPrint',@Params) = 0
EXEC dbo.LongPrint @Params_In_SQL_Out
END
END