标签:
有时工作中要建个分区函数,可是像日期这种分区函数要是搞个几百个的值那不是要搞死我。于是写了点代码自动生成一个从1990年开始的按月的分区函数和对应的分区主题
USE [TestDB] GO DECLARE @STR NVARCHAR(MAX) SET @STR = ‘CREATE PARTITION FUNCTION [PF_RangeByMonth_FromYear1990](DATETIME) AS RANGE LEFT FOR VALUES (‘ DECLARE @STR2 NVARCHAR(MAX) SET @STR2 = ‘CREATE PARTITION SCHEME [PS_RangeByMonth_FromYear1990] AS PARTITION [PF_RangeByMonth_FromYear1990] TO (‘ --generate corresponding partition function script SELECT @STR + STUFF((SELECT ‘‘‘,‘‘‘ + CONVERT(VARCHAR, DATEADD(MONTH,ID-1,‘1990-01-01‘), 121) FROM [dbo].[Numbers] WHERE ID <= DATEDIFF(MONTH,‘1990-01-01‘,‘2040-01-01‘)+1 ORDER BY [ID] FOR XML PATH(‘‘), TYPE).value(‘.‘, ‘varchar(max)‘) ,1,2,‘‘) + ‘‘‘)‘ + CHAR(13) as ‘partition function script‘ --generate corresponding partition scheme script SELECT @STR2 + STUFF((SELECT +‘, [PRIMARY]‘ FROM [dbo].[Numbers] WHERE ID <= DATEDIFF(MONTH,‘1990-01-01‘,‘2040-01-01‘)+2 FOR XML PATH(‘‘), TYPE).value(‘.‘, ‘varchar(max)‘) ,1,2,‘‘) + ‘)‘ + CHAR(13) as ‘partition scheme script‘
SQL Server ->> 生成时间类型的Partition Function和Partition Scheme代码
标签:
原文地址:http://www.cnblogs.com/jenrrychen/p/4845812.html