-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbo.iTVF_TablesPartitionsInfo.UserDefinedFunction.sql
337 lines (334 loc) · 6.46 KB
/
dbo.iTVF_TablesPartitionsInfo.UserDefinedFunction.sql
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
USE [MonthlyPartitionsTest]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE FUNCTION [dbo].[iTVF_TablesPartitionsInfo]
(
-- Add the parameters for the function here
@TableName varchar(128)
)
RETURNS TABLE
AS
RETURN
(
WITH TBINFO
AS
(
SELECT
SchemaName = S.name
, TableName = TB.name
, PartitionScheme = PS.name
, PartitionFunction = PF.name
, PartitionFunctionRangeType =
CASE
WHEN
boundary_value_on_right = 0
THEN 'LEFT'
ELSE
'RIGHT'
END
, PartitionFunctionFanout = PF.fanout
, SchemaID = S.schema_id
, ObjectID = TB.object_id
, PartitionSchemeID = PS.data_space_id
, PartitionFunctionID = PS.function_id
--, partition_number = p.partition_number
FROM
sys.schemas S
INNER JOIN
sys.tables TB
ON
S.schema_id = TB.schema_id
INNER JOIN
sys.indexes IDX
on
TB.object_id = IDX.object_id
AND
IDX.index_id < 2
--JOIN
-- sys.partitions as p
-- on
-- IDX.object_id = p.object_id
-- and
-- IDX.index_id = p.index_id
INNER JOIN
sys.partition_schemes PS
ON
PS.data_space_id = IDX.data_space_id
INNER JOIN
sys.partition_functions PF
ON
PS.function_id = PF.function_id
)
, PF1
AS
(
SELECT
PFP.function_id
, PFR.boundary_id
, PFR.value
, Type = CONVERT
(
sysname
,
CASE
T.name
WHEN
'numeric'
THEN
'decimal'
WHEN
'real'
THEN
'float'
ELSE
T.name
END
+
CASE
WHEN
T.name IN('decimal', 'numeric')
THEN
QUOTENAME
(
RTRIM(PFP.precision)
+
CASE
WHEN
PFP.scale > 0
THEN
',' + RTRIM(PFP.scale)
ELSE
''
END
,
'()'
)
WHEN
T.name IN('float', 'real')
THEN
QUOTENAME(PFP.precision, '()')
WHEN
T.name LIKE 'n%char'
THEN
QUOTENAME(PFP.max_length / 2, '()')
WHEN
T.name LIKE '%char'
OR
T.name LIKE '%binary'
THEN
QUOTENAME(PFP.max_length, '()')
ELSE
''
END
)
FROM
sys.partition_parameters PFP
LEFT JOIN
sys.partition_range_values PFR
ON
PFR.function_id = PFP.function_id
AND
PFR.parameter_id = PFP.parameter_id
INNER JOIN
sys.types T
ON
PFP.system_type_id = T.system_type_id
)
, PF2
AS
(
SELECT
*
FROM
PF1
UNION ALL
SELECT
function_id
, boundary_id = boundary_id - 1
, value
, type
FROM
PF1
WHERE
boundary_id = 1
),
PF
AS
(
SELECT
B.function_id
, boundary_id = ISNULL(B.boundary_id + 1, 1)
, b.value as LeftValue
, a.value as RightValue
, value =
STUFF
(
CASE
WHEN
A.boundary_id = 1
THEN ''
ELSE
' AND [partition_column_name] '
+ PF.MoreThan
+ ' '
+ CONVERT(varchar(max), B.value)
END
+
CASE
WHEN
A.boundary_id IS NULL
THEN ''
ELSE
' AND [partition_column_name] '
+ PF.LessThan
+ ' '
+ CONVERT(varchar(max), A.value)
END
, 1
, 5
, ''
)
, B.Type
FROM
PF1 A
RIGHT JOIN
PF2 B
ON
A.function_id = B.function_id
AND
(
A.boundary_id - 1 = B.boundary_id
OR
(
A.boundary_id IS NULL
AND
B.boundary_id IS NULL
)
)
INNER JOIN
(
SELECT
function_id
, LessThan =
CASE
WHEN
boundary_value_on_right = 0
THEN
'<='
ELSE
'<'
END
, MoreThan =
CASE
WHEN
boundary_value_on_right = 0
THEN '>'
ELSE
'>='
END
FROM
sys.partition_functions
) PF
ON
B.function_id = PF.function_id
)
, PS
AS
(
SELECT
DDS.partition_scheme_id
, DDS.destination_id
, FileGroupName = FG.name
, IsReadOnly = FG.is_read_only
FROM
sys.destination_data_spaces DDS
INNER JOIN
sys.filegroups FG
ON
DDS.data_space_id = FG.data_space_id
)
, PINFO
AS
(
SELECT
RowID = ROW_NUMBER() OVER(ORDER BY SchemaID, ObjectID, PS.destination_id)
, TB.SchemaName
, TB.TableName
, TB.PartitionScheme
--, tb.partition_number
, PS.destination_id
, PS.FileGroupName
, PS.IsReadOnly
, TB.PartitionFunction
, TB.PartitionFunctionRangeType
, TB.PartitionFunctionFanout
, PF.boundary_id
, PF.Type
, PF.value
, pf.RightValue
, pf.LeftValue
FROM
TBINFO TB
INNER JOIN
PS
ON
TB.PartitionSchemeID = PS.partition_scheme_id
LEFT JOIN
PF
ON
TB.PartitionFunctionID = PF.function_id
AND
PS.destination_id = PF.boundary_id
)
SELECT
----v1
--@MergeDate = cast(min(v2) as date) --as [Merge]
--, @SplitDate = cast(max(v2) as date) --as [Split]
--, @Years = (count(1) - 1)/12
a.TableName
, a.PartitionFunction
, a.PartitionScheme
, a.FileGroupName
, a.LeftValue
, a.RightValue
, Min(a.RightValue) over (partition by a.PartitionFunction) as MinRightValue
, Max(a.RightValue) over (partition by a.PartitionFunction) as MaxRightValue
, Min(a.LeftValue) over (partition by a.PartitionFunction) as MinLeftValue
, Max(a.LeftValue) over (partition by a.PartitionFunction) as MaxLeftValue
--, a.partition_number
, ISNULL(CONVERT(varchar(20), a.boundary_id), '') as boundary_id
, a.destination_id
, a.RowID
, a.SchemaName
, a.IsReadOnly
, a.PartitionFunctionRangeType
, a.PartitionFunctionFanout
, ISNULL(a.Type, N'') as Type
,
CASE
PartitionFunctionFanout
WHEN
1
THEN
'<ALL Data>'
ELSE
ISNULL(a.value, N'<NEXT USED>')
END as [value]
FROM
PINFO a
where
(
@TableName is null
or
a.TableName = @TableName
)
)
GO