-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet_PartitionColumn.dsql
More file actions
56 lines (46 loc) · 2.73 KB
/
Get_PartitionColumn.dsql
File metadata and controls
56 lines (46 loc) · 2.73 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
CREATE PROC [dbo].[Get_PartitionColumn] @Table_Name [VARCHAR](200),@Table_PartitionColumn [VARCHAR](100) OUT AS
/*
IF OBJECT_ID ('dbo.Get_PartitionColumn', 'P') IS NOT NULL DROP PROCEDURE dbo.Get_PartitionColumn
GO
###################################################################################################################
Example:
-------------------------------------------------------------------------------------------------------------------
DECLARE @SQL_String VARCHAR(MAX)
EXEC dbo.Get_PartitionColumn 'dbo.PartitionedTable', @SQL_String OUTPUT
EXEC dbo.LongPrint @SQL_String
===================================================================================================================
Proc Description:
-------------------------------------------------------------------------------------------------------------------
This proc creating and returning a string with the name of partition column for Partitioned table. If the table is not Partitioned - returning empty string
@Table_Name - Name of the table to pick column from
@Table_PartitionColumn - Param to return string.
###################################################################################################################
*/
BEGIN
/*====================================== TESTING =======================================================================*/
--DECLARE @Table_Name VARCHAR(200) = 'dbo.PartitionedTable', @Table_PartitionColumn VARCHAR(100)
/*====================================== TESTING =======================================================================*/
DECLARE @Error VARCHAR(MAX) = ''
IF @Table_Name IS NULL SET @Error = @Error + 'Table name cannot be NULL'
SET @Table_PartitionColumn = '';
IF LEN(@Error) > 0
BEGIN
PRINT @Error
END
ELSE
BEGIN
DECLARE @Schema VARCHAR(100), @Table VARCHAR(200)
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
SELECT @Table_PartitionColumn = '[' +CAST(c.name AS VARCHAR(100)) + ']'
FROM sys.Tables AS t
JOIN sys.schemas s ON t.[schema_id] = s.[schema_id] AND s.name = @Schema
JOIN sys.indexes AS i ON t.[object_id] = i.[object_id] AND I.index_id <=1
JOIN sys.partition_schemes AS ps ON ps.data_space_id = i.data_space_id
JOIN sys.index_columns AS ic ON ic.[object_id] = i.[object_id] AND ic.index_id = i.index_id AND ic.partition_ordinal >= 1 -- because 0 = non-partitioning column
JOIN sys.columns AS c ON t.[object_id] = c.[object_id] AND ic.column_id = c.column_id
WHERE t.[name] = @Table
END
END