{"id":109,"date":"2023-05-11T12:21:47","date_gmt":"2023-05-11T10:21:47","guid":{"rendered":"http:\/\/130.61.57.200\/?p=109"},"modified":"2023-10-24T10:29:20","modified_gmt":"2023-10-24T08:29:20","slug":"partition-automatic-handling","status":"publish","type":"post","link":"http:\/\/130.61.57.200\/index.php\/2023\/05\/11\/partition-automatic-handling\/","title":{"rendered":"Partition Automatic Handling"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Automatic Partition<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>SET ANSI_NULLS, QUOTED_IDENTIFIER ON; &lt;br&gt;GO &lt;br&gt;use &lt;Database&gt; &lt;br&gt;Declare  &lt;br&gt;@nextRange date, &lt;br&gt;@sql nvarchar(3000), &lt;br&gt;@maxBoundary date, &lt;br&gt;@tableList varchar(1000) =&#039;Table1,Table2,Table3&#039;, &lt;br&gt;@tableName varchar(100), &lt;br&gt;@partitionFunction varchar(100), &lt;br&gt;@partitionScheme varchar(100), &lt;br&gt;@PartitionedTables as cursor , &lt;br&gt;@debug bit =0, &lt;br&gt;@monthsPartitionFuture smallint =7,&lt;br&gt;@filegroup = &#039;Primary&#039; ; &lt;br&gt;set @PartitionedTables = cursor for  &lt;br&gt;select * from STRING_SPLIT (@tableList,&#039;,&#039;); &lt;br&gt; &lt;br&gt;OPEN @PartitionedTables; &lt;br&gt;FETCH NEXT FROM @PartitionedTables into @tableName; &lt;br&gt;WHILE @@FETCH_STATUS=0 &lt;br&gt;BEGIN &lt;br&gt;IF @debug=1 print @tableName; &lt;br&gt; &lt;br&gt;SELECT  @maxBoundary=max(cast (rv.value as date))  ,@nextRange=dateadd(MONTH,1,cast (max(value) as date)),@partitionFunction=f.name,@partitionScheme=ps.name &lt;br&gt; FROM sys.partitions p &lt;br&gt;INNER JOIN sys.indexes i ON p.object_id = i.object_id AND p.index_id = i.index_id &lt;br&gt;INNER JOIN sys.objects o ON p.object_id = o.object_id &lt;br&gt;INNER JOIN sys.system_internals_allocation_units au ON p.partition_id = au.container_id &lt;br&gt;INNER JOIN sys.partition_schemes ps ON ps.data_space_id = i.data_space_id &lt;br&gt;INNER JOIN sys.partition_functions f ON f.function_id = ps.function_id &lt;br&gt;INNER JOIN sys.destination_data_spaces dds ON dds.partition_scheme_id = ps.data_space_id AND dds.destination_id = p.partition_number &lt;br&gt;LEFT OUTER JOIN sys.partition_range_values rv ON f.function_id = rv.function_id AND p.partition_number = rv.boundary_id &lt;br&gt;WHERE o.name=@tableName &lt;br&gt;group by f.name,ps.name &lt;br&gt; &lt;br&gt;IF @debug=1 print cast(@maxBoundary as varchar) +&#039;---&#039;+@TableName &lt;br&gt;WHILE @maxBoundary&lt;dateadd(month,@monthsPartitionFuture,getdate())  &lt;br&gt;BEGIN &lt;br&gt;select @sql = &#039;ALTER PARTITION SCHEME &#039;+@partitionScheme +&#039; NEXT USED &#039;+ @filegroup +&#039; ; &lt;br&gt;ALTER PARTITION FUNCTION &#039;+@partitionFunction+&#039;() SPLIT RANGE (N&#039;&#039;&#039;+cast (@nextRange as varchar)+&#039;&#039;&#039;);&#039;; &lt;br&gt;print  @sql; &lt;br&gt;exec sp_executesql @sql; &lt;br&gt;SELECT  @maxBoundary=max(cast (rv.value as date))  ,@nextRange=dateadd(MONTH,1,cast (max(value) as date)) &lt;br&gt; FROM sys.partitions p &lt;br&gt;INNER JOIN sys.indexes i ON p.object_id = i.object_id AND p.index_id = i.index_id &lt;br&gt;INNER JOIN sys.objects o ON p.object_id = o.object_id &lt;br&gt;INNER JOIN sys.system_internals_allocation_units au ON p.partition_id = au.container_id &lt;br&gt;INNER JOIN sys.partition_schemes ps ON ps.data_space_id = i.data_space_id &lt;br&gt;INNER JOIN sys.partition_functions f ON f.function_id = ps.function_id &lt;br&gt;INNER JOIN sys.destination_data_spaces dds ON dds.partition_scheme_id = ps.data_space_id AND dds.destination_id = p.partition_number &lt;br&gt;LEFT OUTER JOIN sys.partition_range_values rv ON f.function_id = rv.function_id AND p.partition_number = rv.boundary_id &lt;br&gt;WHERE o.name=@tableName &lt;br&gt;group by f.name,ps.name &lt;br&gt;IF @debug=1 print @maxBoundary &lt;br&gt;END; &lt;br&gt; &lt;br&gt;FETCH NEXT FROM @PartitionedTables into @tableName; &lt;br&gt;END; &lt;br&gt;CLOSE @PartitionedTables &lt;br&gt; &lt;br&gt; &lt;br&gt;----------------------------------------------- &lt;br&gt;<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Automatic Partition SET ANSI_NULLS, QUOTED_IDENTIFIER ON; &lt;br&gt;GO &lt;br&gt;use &lt;Database&gt; &lt;br&gt;Declare &lt;br&gt;@nextRange date, &lt;br&gt;@sql nvarchar(3000), &lt;br&gt;@maxBoundary date, &lt;br&gt;@tableList varchar(1000) =&#039;Table1,Table2,Table3&#039;, &lt;br&gt;@tableName varchar(100), &lt;br&gt;@partitionFunction varchar(100), &lt;br&gt;@partitionScheme varchar(100), &lt;br&gt;@PartitionedTables as cursor , &lt;br&gt;@debug bit =0, &lt;br&gt;@monthsPartitionFuture smallint =7,&lt;br&gt;@filegroup = &#039;Primary&#039; ; &lt;br&gt;set @PartitionedTables = cursor for &lt;br&gt;select * from STRING_SPLIT (@tableList,&#039;,&#039;); &lt;br&gt; &lt;br&gt;OPEN @PartitionedTables; &lt;br&gt;FETCH NEXT FROM @PartitionedTables [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":104,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[50],"class_list":["post-109","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mssql","tag-partitions"],"blocksy_meta":[],"_links":{"self":[{"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/posts\/109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/comments?post=109"}],"version-history":[{"count":2,"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/posts\/109\/revisions"}],"predecessor-version":[{"id":117,"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/posts\/109\/revisions\/117"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/media\/104"}],"wp:attachment":[{"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/media?parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/categories?post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/tags?post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}