Skip to content

Commit 2174631

Browse files
Azure Update
1 parent c262256 commit 2174631

File tree

1 file changed

+71
-61
lines changed

1 file changed

+71
-61
lines changed

TablesManagement/Scripting/01-sp_tblScriptIndexes.sql

Lines changed: 71 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
/* *****************************************************************************************
2+
AZURE SQL DB Notice
3+
4+
Comment-out the unsupported USE [master] when running in Azure SQL DB/Synapse Analytics
5+
or ignore error caused by unsupported USE statement
6+
******************************************************************************************** */
7+
18
USE [master]
29
GO
310
IF NOT EXISTS (SELECT 1 FROM sys.all_objects WHERE object_id = OBJECT_ID('[dbo].[sp_tblScriptIndexes]') AND TYPE = 'P')
4-
EXECUTE ('CREATE PROCEDURE [dbo].[sp_tblScriptIndexes] AS BEGIN PRINT ''Container'' END')
11+
EXECUTE ('CREATE PROCEDURE [dbo].[sp_tblScriptIndexes] AS BEGIN PRINT ''Container'' END')
512
GO
613
/* ****************************************************
714
sp_tblScriptIndexes v 0.65 (2018-07-30)
@@ -32,21 +39,21 @@ SOFTWARE.
3239
written consent.
3340
3441
Description:
35-
Scripts indexes for specifed table
42+
Scripts indexes for specifed table
3643
3744
3845
Parameters:
3946
40-
@tableName nvarchar(261) = NULL --Table which indexes should be scripted
41-
,@newTableName nvarchar(261) = NULL --New table Name. If NULL then Indexes are scripted for original table
42-
,@partitionID int = 0 --Specifies how the ON [FileGroup] is being scripted
43-
,@scriptPrimaryKey bit = 1 --Specifies whether Primary key should be scripted
44-
,@scriptUniqueConstraints bit = 1 --Specifies whether Unique Constraints should be scripted
45-
,@scriptIndexes bit = 1 --Specifies wheteher Indexes should be scripted
46-
,@scriptDisabledIndexes bit = 0 --Specifies whether to script disabled indexes
47-
,@scriptAlterIndexDisable bit = 1 --For Disabled indexes scripts the ALTER INDEX DISABLE
47+
@tableName nvarchar(261) = NULL --Table which indexes should be scripted
48+
,@newTableName nvarchar(261) = NULL --New table Name. If NULL then Indexes are scripted for original table
49+
,@partitionID int = 0 --Specifies how the ON [FileGroup] is being scripted
50+
,@scriptPrimaryKey bit = 1 --Specifies whether Primary key should be scripted
51+
,@scriptUniqueConstraints bit = 1 --Specifies whether Unique Constraints should be scripted
52+
,@scriptIndexes bit = 1 --Specifies wheteher Indexes should be scripted
53+
,@scriptDisabledIndexes bit = 0 --Specifies whether to script disabled indexes
54+
,@scriptAlterIndexDisable bit = 1 --For Disabled indexes scripts the ALTER INDEX DISABLE
4855
,@scriptDropExisting bit = 1 --Scripts DROP_EXISTING = ON or DROP_EXISTING = OFF
49-
,@scriptIndexTypes nvarchar(100) = N'1,2,3,5,6' --Bitmask of Index types to Script Currently 1,2,5,6
56+
,@scriptIndexTypes nvarchar(100) = N'1,2,3,5,6' --Bitmask of Index types to Script Currently 1,2,5,6
5057
,@indexNames nvarchar(max) = '%' --Comma Separated List of Index Names to Script. Supports LIKE wildcards
5158
,@noInfoMsg bit = 0 --Disbles printing of header and informational messages
5259
,@outputScript nvarchar(max) = NULL OUTPUT --Outputs script to the @outputScript parameters. Allows utilization of the script in other stored procedures
@@ -58,26 +65,26 @@ Parameters:
5865
5966
6067
Index types and corresponding bit positions
61-
1 = Clustered
62-
2 = Nonclustered
63-
3 = XML
64-
4 = Spatial
65-
5 = Clustered columnstore index - Applies to: SQL Server 2014 and above
66-
6 = Nonclustered columnstore index - Applies to: SQL Server 2012 and above
67-
7 = Nonclustered hash index - Applies to: SQL Server 2014 and above
68+
1 = Clustered
69+
2 = Nonclustered
70+
3 = XML
71+
4 = Spatial
72+
5 = Clustered columnstore index - Applies to: SQL Server 2014 and above
73+
6 = Nonclustered columnstore index - Applies to: SQL Server 2012 and above
74+
7 = Nonclustered hash index - Applies to: SQL Server 2014 and above
6875
6976
* ***************************************************** */
7077
ALTER PROCEDURE [dbo].[sp_tblScriptIndexes]
71-
@tableName nvarchar(261) = NULL --Table which indexes should be scripted
72-
,@newTableName nvarchar(261) = NULL --New table Name. If NULL then Indexes are scripted for original table
73-
,@partitionID int = 0 --Specifies how the ON [FileGroup] is being scripted
74-
,@scriptPrimaryKey bit = 1 --Specifies whether Primary key should be scripted
75-
,@scriptUniqueConstraints bit = 1 --Specifies whether Unique Constraints should be scripted
76-
,@scriptIndexes bit = 1 --Specifies wheteher Indexes should be scripted
77-
,@scriptDisabledIndexes bit = 0 --Specifies whether to script disabled indexes
78-
,@scriptAlterIndexDisable bit = 1 --For Disabled indexes scripts the ALTER INDEX DISABLE
78+
@tableName nvarchar(261) = NULL --Table which indexes should be scripted
79+
,@newTableName nvarchar(261) = NULL --New table Name. If NULL then Indexes are scripted for original table
80+
,@partitionID int = 0 --Specifies how the ON [FileGroup] is being scripted
81+
,@scriptPrimaryKey bit = 1 --Specifies whether Primary key should be scripted
82+
,@scriptUniqueConstraints bit = 1 --Specifies whether Unique Constraints should be scripted
83+
,@scriptIndexes bit = 1 --Specifies wheteher Indexes should be scripted
84+
,@scriptDisabledIndexes bit = 0 --Specifies whether to script disabled indexes
85+
,@scriptAlterIndexDisable bit = 1 --For Disabled indexes scripts the ALTER INDEX DISABLE
7986
,@scriptDropExisting bit = 1 --Scripts DROP_EXISTING = ON or DROP_EXISTING = OFF
80-
,@scriptIndexTypes nvarchar(100) = N'1,2,3,5,6' --Bitmask of Index types to Script Currently 1,2,5,6
87+
,@scriptIndexTypes nvarchar(100) = N'1,2,3,5,6' --Bitmask of Index types to Script Currently 1,2,5,6
8188
,@indexNames nvarchar(max) = '%' --Comma Separated List of Index Names to Script. Supports LIKE wildcards
8289
,@noInfoMsg bit = 0 --Disbles printing of header and informational messages
8390
,@outputScript nvarchar(max) = NULL OUTPUT --Outputs script to the @outputScript parameters. Allows utilization of the script in other stored procedures
@@ -87,7 +94,7 @@ ALTER PROCEDURE [dbo].[sp_tblScriptIndexes]
8794
,@dataCompression nvarchar(5) = NULL --When specified the overrides the current index data compression
8895
,@columnstoreDataCompression nvarchar(50) = NULL --When specified then overrides the current columnstore data compression type
8996
AS
90-
BEGIN
97+
BEGIN
9198
SET NOCOUNT ON;
9299
SET XACT_ABORT ON;
93100

@@ -186,7 +193,7 @@ BEGIN
186193
RAISERROR(N'', 0, 0) WITH NOWAIT;
187194
RAISERROR(N'Udage:
188195
sp_tblScriptIndexes <parameters>', 0,0) WITH NOWAIT;
189-
RAISERROR(N'', 0, 0);
196+
RAISERROR(N'', 0, 0);
190197
RAISERROR(N'Script is returned as XML processing-instruction to allow easy complete script retrieval.', 0, 0) WITH NOWAIT;
191198
RAISERROR(N'If not a complete script is returned, check "Maximum Characters Retrieved" for XML in the Query Options of SSMS', 0, 0) WITH NOWAIT;
192199
RAISERROR(N'', 0, 0) WITH NOWAIT;
@@ -203,11 +210,11 @@ sp_tblScriptIndexes <parameters>', 0,0) WITH NOWAIT;
203210
For Non-Partitioned indexes script ON [FileGroupName]
204211
>0 - For Partitioned Indexes script ON [FileGroupOfPartitionID]
205212
For Non-Partitioned indexes is the same as 0
206-
,@scriptPrimaryKey bit = 1 --Specifies whether Primary key should be scripted
207-
,@scriptUniqueConstraints bit = 1 --Specifies whether Unique Constraints should be scripted
208-
,@scriptIndexes bit = 1 --Specifies wheteher Indexes should be scripted',0, 0) WITH NOWAIT;
209-
RAISERROR(N' ,@scriptDisabledIndexes bit = 0 --Specifies whether to script disabled indexes
210-
,@scriptAlterIndexDisable bit = 1 --For Disabled indexes scripts the ALTER INDEX DISABLE
213+
,@scriptPrimaryKey bit = 1 --Specifies whether Primary key should be scripted
214+
,@scriptUniqueConstraints bit = 1 --Specifies whether Unique Constraints should be scripted
215+
,@scriptIndexes bit = 1 --Specifies wheteher Indexes should be scripted',0, 0) WITH NOWAIT;
216+
RAISERROR(N' ,@scriptDisabledIndexes bit = 0 --Specifies whether to script disabled indexes
217+
,@scriptAlterIndexDisable bit = 1 --For Disabled indexes scripts the ALTER INDEX DISABLE
211218
,@scriptDropExisting bit = 1 --Scripts DROP_EXISTING = ON or DROP_EXISTING = OFF
212219
,@scriptIndexTypes nvarchar(100) = N''1,2,3,5,6'' --Bitmask of Index types to Script Currently 1,2,5,6
213220
,@indexNames nvarchar(max) = ''%%'' --Comma Separated List of Index Names to Script. Supports LIKE wildcards
@@ -225,21 +232,21 @@ RAISERROR(N' ,@noXml bit = 0
225232
', 0, 0) WITH NOWAIT;
226233
RAISERROR(N'', 0, 0) WITH NOWAIT;
227234
RAISERROR(N'Supported Index Types:
228-
1 = Clustered
229-
2 = Nonclustered
230-
3 = XML
231-
4 = Spatial
232-
5 = Clustered columnstore index - Applies to: SQL Server 2014 and above
233-
6 = Nonclustered columnstore index - Applies to: SQL Server 2012 and above
234-
7 = Nonclustered hash index - Applies to: SQL Server 2014 and above', 0, 0) WITH NOWAIT;
235+
1 = Clustered
236+
2 = Nonclustered
237+
3 = XML
238+
4 = Spatial
239+
5 = Clustered columnstore index - Applies to: SQL Server 2014 and above
240+
6 = Nonclustered columnstore index - Applies to: SQL Server 2012 and above
241+
7 = Nonclustered hash index - Applies to: SQL Server 2014 and above', 0, 0) WITH NOWAIT;
235242

236243
RETURN
237244
END
238245

239-
--Split @newTableName into Schema and Table
240-
SELECT
241-
@newTbName = PARSENAME(@newTableName, 1)
242-
,@newTableSchema = PARSENAME(@newTableName,2)
246+
--Split @newTableName into Schema and Table
247+
SELECT
248+
@newTbName = PARSENAME(@newTableName, 1)
249+
,@newTableSchema = PARSENAME(@newTableName,2)
243250

244251
SELECT
245252
@newTable = QUOTENAME(ISNULL(@newTableSchema, SCHEMA_NAME(t.schema_id))) + N'.' + QUOTENAME(ISNULL(@newTbName, t.name))
@@ -253,7 +260,7 @@ RAISERROR(N' ,@noXml bit = 0
253260

254261
INSERT INTO @scripts(IndexScript)
255262
SELECT
256-
CASE --Script CREATE INDES or APPROPRIATE ALTER TABLE for CONSTRAINTS
263+
CASE --Script CREATE INDES or APPROPRIATE ALTER TABLE for CONSTRAINTS
257264
WHEN i.is_primary_key = 1 THEN N'ALTER TABLE ' + @newTable + N' ADD CONSTRAINT ' + QUOTENAME(ISNULL(@newTbName + '_', '') + i.name) + N' PRIMARY KEY ' + CASE WHEN i.[type] = 1 THEN N'CLUSTERED' ELSE N'NONCLUSTERED' END
258265
WHEN i.is_unique_constraint = 1 THEN N'ALTER TABLE ' + @newTable + N' ADD CONSTRAINT ' + QUOTENAME(ISNULL(@newTbName + '_', '') + i.name) + N' UNIQUE ' + CASE WHEN i.[type] = 1 THEN N'CLUSTERED' ELSE N'NONCLUSTERED' END
259266
ELSE N'CREATE ' + CASE WHEN i.is_unique = 1 THEN N'UNIQUE ' ELSE N'' END
@@ -267,10 +274,10 @@ RAISERROR(N' ,@noXml bit = 0
267274
END + ' INDEX ' + QUOTENAME(ISNULL(@newTbName + '_', '') + i.name) + N' ON ' + @newTable
268275
END + CASE WHEN i.type <> 5 THEN N' (' + NCHAR(13) + NCHAR(10) ELSE N'' END
269276
+
270-
ISNULL( --Script Key columns for INDEX or CONSTRAINT
277+
ISNULL( --Script Key columns for INDEX or CONSTRAINT
271278
STUFF((
272279
SELECT
273-
(
280+
(
274281
SELECT
275282
N' ,' + QUOTENAME(c.name) + CASE WHEN ic.is_descending_key = 1 THEN N' DESC' ELSE '' END + NCHAR(13)-- + NCHAR(10)
276283
FROM sys.index_columns ic
@@ -294,7 +301,7 @@ RAISERROR(N' ,@noXml bit = 0
294301
+ N' FOR ' + CASE xi.secondary_type WHEN 'P' THEN N'PATH' WHEN 'V' THEN N'VALUE' WHEN 'R' THEN N'PROPERTY' END
295302
ELSE N''
296303
END
297-
+ ISNULL( --Script Included columns for INDEXES
304+
+ ISNULL( --Script Included columns for INDEXES
298305
CASE WHEN i.type NOT IN (3, 5, 6) THEN N' INCLUDE (' + NCHAR(13) ELSE N'' END +
299306
STUFF((
300307
SELECT
@@ -319,11 +326,11 @@ RAISERROR(N' ,@noXml bit = 0
319326
+ N')'
320327
,N''
321328
) + NCHAR(13) + NCHAR(10)
322-
+ ISNULL(N'WHERE ' + i.filter_definition + NCHAR(13) + NCHAR(10), '') --Script filter ondition for INDEX
323-
+ N'WITH (' --Script Options for INDEXES AND CONSTRAINTS
329+
+ ISNULL(N'WHERE ' + i.filter_definition + NCHAR(13) + NCHAR(10), '') --Script filter ondition for INDEX
330+
+ N'WITH (' --Script Options for INDEXES AND CONSTRAINTS
324331
+ CASE
325332
WHEN i.[type] IN (5, 6) THEN N'DROP_EXISTING = OFF'
326-
ELSE
333+
ELSE
327334
N'PAD_INDEX = ' + CASE WHEN i.is_padded = 1 THEN N'ON' ELSE N'OFF' END
328335
+ N', SORT_IN_TEMPDB = OFF'
329336
+ CASE WHEN i.is_primary_key = 1 OR i.is_unique_constraint = 1 THEN N'' ELSE CASE WHEN @scriptDropExisting = 1 THEN N', DROP_EXISTING = ON' ELSE N', DROP_EXISTING = OFF' END END
@@ -379,17 +386,17 @@ RAISERROR(N' ,@noXml bit = 0
379386
+ N';'
380387
+ CASE
381388
WHEN i.is_disabled = 1 AND @scriptAlterIndexDisable = 1
382-
THEN NCHAR(13) + NCHAR(10) + N'ALTER INDEX ' + QUOTENAME(ISNULL(@newTbName + '_', '') + i.name) + N' ON ' + @newTable + N' DISABLE'
389+
THEN NCHAR(13) + NCHAR(10) + N'ALTER INDEX ' + QUOTENAME(ISNULL(@newTbName + '_', '') + i.name) + N' ON ' + @newTable + N' DISABLE'
383390
ELSE N''
384391
END AS IndexScript
385392
FROM sys.indexes i
386393
INNER JOIN #indexNames ixn ON i.name = ixn.IndexName
387394
LEFT JOIN sys.xml_indexes xi ON xi.object_id = i.object_id and xi.index_id = i.index_id
388-
LEFT JOIN sys.stats s ON s.object_id = i.object_id AND s.stats_id = i.index_id --Statistics to get the NO RECOMPUTE information
389-
INNER JOIN sys.data_spaces ds ON i.data_space_id = ds.data_space_id AND ds.type <> 'FX' --dsta_spaces to find out the FileGroup/partition scheme informaiton (exclude MEMORY OPTIMIZED data Spaces
390-
LEFT JOIN sys.partition_schemes ps ON i.data_space_id = ps.data_space_id --details of partition scheme
395+
LEFT JOIN sys.stats s ON s.object_id = i.object_id AND s.stats_id = i.index_id --Statistics to get the NO RECOMPUTE information
396+
INNER JOIN sys.data_spaces ds ON i.data_space_id = ds.data_space_id AND ds.type <> 'FX' --dsta_spaces to find out the FileGroup/partition scheme informaiton (exclude MEMORY OPTIMIZED data Spaces
397+
LEFT JOIN sys.partition_schemes ps ON i.data_space_id = ps.data_space_id --details of partition scheme
391398
LEFT JOIN sys.destination_data_spaces dds ON dds.partition_scheme_id = i.data_space_id AND ds.[type] = N'PS' AND dds.destination_id = @partitionID --Partitions and destinations
392-
LEFT JOIN sys.filegroups dfg ON dfg.data_space_id = dds.data_space_id --Destination file groups for partitioned index
399+
LEFT JOIN sys.filegroups dfg ON dfg.data_space_id = dds.data_space_id --Destination file groups for partitioned index
393400
LEFT JOIN sys.partitions p ON p.object_id = i.object_id AND p.index_id = i.index_id AND (p.partition_number = @partitionID OR (ds.[type] = N'FG' AND p.partition_number = 1)) --To determine compression
394401
WHERE
395402
i.object_id = OBJECT_ID(@tableName)
@@ -400,11 +407,11 @@ RAISERROR(N' ,@noXml bit = 0
400407
AND
401408
i.type IN (SELECT IndexType FROM @indexes) --Script only supported indexes
402409
AND
403-
(@scriptPrimaryKey = 1 OR i.is_primary_key = 0) --Script Primary Key
410+
(@scriptPrimaryKey = 1 OR i.is_primary_key = 0) --Script Primary Key
404411
AND
405-
(@scriptUniqueConstraints = 1 OR i.is_unique_constraint = 0) --Script Unique Constraints
412+
(@scriptUniqueConstraints = 1 OR i.is_unique_constraint = 0) --Script Unique Constraints
406413
AND
407-
(@scriptIndexes = 1 OR i.is_primary_key = 1 OR i.is_unique_constraint = 1) --Script Indexes
414+
(@scriptIndexes = 1 OR i.is_primary_key = 1 OR i.is_unique_constraint = 1) --Script Indexes
408415

409416
SET @xml = (
410417
SELECT
@@ -432,5 +439,8 @@ RAISERROR(N' ,@noXml bit = 0
432439

433440
END
434441
GO
435-
EXECUTE sp_ms_marksystemobject 'dbo.sp_tblScriptIndexes'
442+
443+
--Mark Stored Procedure as system object, so it executes in the context of current database.
444+
IF SERVERPROPERTY('EngineEdition') IN (1, 2, 3, 4, 8)
445+
EXEC(N'EXECUTE sp_ms_marksystemobject ''dbo.sp_tblScriptIndexes''');
436446
GO

0 commit comments

Comments
 (0)