码迷,mamicode.com
首页 > 数据库 > 详细

SQL Server ->> 生成时间类型的Partition Function和Partition Scheme代码

时间:2015-09-29 12:50:19      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

有时工作中要建个分区函数,可是像日期这种分区函数要是搞个几百个的值那不是要搞死我。于是写了点代码自动生成一个从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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!