主要参考:动态分区

一、前提准备:

1、一个日期存储过程,注意代码可以得到一个月中的最后一天,最终生成时间维度。

USE [DrugDW]
GO
/****** Object:  StoredProcedure [dbo].[PROC_DATETIME]    Script Date: 2/28 星期二 14:16:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GOALTER PROCEDURE [dbo].[PROC_DATETIME]AS
BEGIN/****** Object:  StoredProcedure [dbo].[proc_Dim_date]    Script Date: 05/20/2016 11:35:58 ******/IF OBJECT_ID('dbo.Dim_Date') IS NOT NULLDROP TABLE dbo.[Dim_Date]CREATE TABLE [dbo].[Dim_Date]([DateKey] [int] NULL,[Date] [datetime] NULL,[Year] [float] NULL,[Month] [float] NULL,[Month EN] [nvarchar](50) NULL,[Month Short EN] [nvarchar](50) NULL,[Month CN] [nvarchar](50) NULL,[Day] [float] NULL,[Quarter] [float] NULL,[Quarter EN] [nvarchar](50) NULL,[Quarter CN] [nvarchar](50) NULL,[Weekday] [float] NULL,[Weekday CN] [nvarchar](50) NULL,[Weekday Short EN] [nvarchar](50) NULL,[Week of Year] [float] NULL,[Day of Year] [float] NULL,[SemiYearly] [nvarchar](50) NULL,[Period of Ten Days] [nvarchar](10) NULL,[Period of Index] [nvarchar](2) NULL,[Weekend] [nvarchar](5) NULL
) ON [PRIMARY]SET DATEFIRST 7 --设周日为每周的第一天--向日期表插入数据
DECLARE @b1 DATETIME
set @b1='2015-01-01'     --设置起始日期 WHILE @b1< dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0))   --设置截止日期
BEGININSERT INTO dbo.[Dim_Date] ([DateKey],[Date], [Year],[Month],[Month EN],[Month Short EN],[Month CN],[Day],[Quarter],[Quarter EN],[Quarter CN],[Weekday],[Weekday CN],[Weekday Short EN],[Week of Year],[Day of Year],[SemiYearly],[Period of Ten Days],[Period of Index] ,[Weekend]) VALUES( CONVERT(NVARCHAR(10),@b1,112),  --DateKey 1@b1, --Date 2DATEPART(year, @b1), --Year 3DATEPART(month, @b1), --Month 4CASE --Month EN 5when (DATEPART(month, @b1))='1' then 'January' when (DATEPART(month, @b1))='2' then 'February'when (DATEPART(month, @b1))='3' then 'March'when (DATEPART(month, @b1))='4' then 'April'when (DATEPART(month, @b1))='5' then 'May'when (DATEPART(month, @b1))='6' then 'June'when (DATEPART(month, @b1))='7' then 'July'when (DATEPART(month, @b1))='8' then 'August'when (DATEPART(month, @b1))='9' then 'September'when (DATEPART(month, @b1))='10' then 'October'when (DATEPART(month, @b1))='11' then 'November'else 'December'END, CASE --Month Short En 6when (DATEPART(month, @b1))='1' then 'Jan' when (DATEPART(month, @b1))='2' then 'Feb'when (DATEPART(month, @b1))='3' then 'Mar'when (DATEPART(month, @b1))='4' then 'Apr'when (DATEPART(month, @b1))='5' then 'May'when (DATEPART(month, @b1))='6' then 'Jun'when (DATEPART(month, @b1))='7' then 'Jul'when (DATEPART(month, @b1))='8' then 'Aug'when (DATEPART(month, @b1))='9' then 'Sep'when (DATEPART(month, @b1))='10' then 'Oct'when (DATEPART(month, @b1))='11' then  'Nov'else 'Dec'END,CASE --Month CN 7when (DATEPART(month, @b1))='1' then N'一月' when (DATEPART(month, @b1))='2' then N'二月'when (DATEPART(month, @b1))='3' then N'三月'when (DATEPART(month, @b1))='4' then N'四月'when (DATEPART(month, @b1))='5' then N'五月'when (DATEPART(month, @b1))='6' then N'六月'when (DATEPART(month, @b1))='7' then N'七月'when (DATEPART(month, @b1))='8' then N'八月'when (DATEPART(month, @b1))='9' then N'九月'when (DATEPART(month, @b1))='10' then N'十月'when (DATEPART(month, @b1))='11' then  N'十一月'else N'十二月'END,DATEPART(day, @b1),--day  8DATEName (qq, @b1),--quarter 9CASE   --quarter en  10when DATEName (qq, @b1)='1' then 'Q1'when DATEName (qq, @b1)='2' then 'Q2' when DATEName (qq, @b1)='3' then 'Q3'else  'Q4'END,CASE  --quarter cn  11when DATEName (qq, @b1)='1' then N'一季度'when DATEName (qq, @b1)='2' then N'二季度' when DATEName (qq, @b1)='3' then N'三季度'else  N'四季度'END,    DATEPART(dw, @b1),--Weekday 12CASE --Weekday CN  13when DATEPART(dw, @b1)=1 then  N'星期日'when DATEPART(dw, @b1)=2 then  N'星期一'when DATEPART(dw, @b1)=3 then  N'星期二'when DATEPART(dw, @b1)=4 then  N'星期三'when DATEPART(dw, @b1)=5 then  N'星期四'when DATEPART(dw, @b1)=6 then  N'星期五'  else N'星期六'END,CASE --Weekday Short EN 14  --注意,周日是第一天.when DATEPART(dw, @b1)='1' then 'Sun'when DATEPART(dw, @b1)='2' then 'Mon'when DATEPART(dw, @b1)='3' then 'Tue'when DATEPART(dw, @b1)='4' then 'Wed'when DATEPART(dw, @b1)='5' then 'Thu'when DATEPART(dw, @b1)='6' then 'Fri'else 'Sat'END, DATEName (wk, @b1),--week of year 15DATEName (dy, @b1),--day of year  16CASE --SemiYearly 17when DATEPART(month, @b1)<=6 then N'上半年'else N'下半年'END,CASE  --Period of Ten Days 18when DATEName (dd, @b1)<=10 then N'上旬' when DATEName (dd, @b1)>20  then N'下旬'else N'中旬'END,CASE  --Period of Ten Days 19when DATEName (dd, @b1)<=10 then N'1' when DATEName (dd, @b1)>20  then N'3'else N'2'END,CASE --Is it Weekend? 20when DATEPART(dw, @b1)='1' then '周末'when DATEPART(dw, @b1)='7' then '周末'else '平时'END
)
--日期加1天set @b1=DATEADD(day, 1, @b1)
END
end

View Code

2、一个事实表:

3、构建好的Cube,并且无分区(有分区也无妨,这里只是排除干扰,容易理解):

4、准备SSIS参数:

二、利用SSIS动态构建分区

最终效果

2.1 、执行SQL任务

SQLStatement代码:

SELECT 'DrugDW'                                                    AS DataSoureID,--数据源'Drug DW'                                                       AS CubeName,--分区来自哪一个cube'Drug DW'                                                       AS CubeID,'Fact OP Fee Detail'                                           AS MeasureGroup,--指定是一个度量值组'Fact OP Fee Detail'                                      AS MeasureGroupID,'Fact OP Fee Detail ' + Cast(MonthInfo.YearMonth AS VARCHAR(6)) AS Partition,--分区名称=度量值组名称+年月'SELECT *  FROM [dbo].[Fact_OPFeeDetail]    where_clause'   AS SQL,--要进行分区的SQLcast(MinDateKey as varchar(8)) as MinDateKey,--最小datekeycast(MaxDateKey as varchar(8)) as MaxDateKey--最大datekeyFROM   (SELECT t1.YearMonth,(SELECT Min(datekey)   FROM   dim_date t2 WHERE  CONVERT(VARCHAR(6), t2.Date, 112) = t1.YearMonth) AS MinDateKey,(SELECT Max(datekey) FROM   dim_date t2 WHERE  CONVERT(VARCHAR(6), t2.Date, 112) = t1.YearMonth) AS MaxDateKeyFROM  (SELECT DISTINCT CONVERT(VARCHAR(6), Date, 112) AS YearMonth   FROM   dim_date) AS t1)MonthInfoWHERE  EXISTS(SELECT *   FROM   [dbo].[Fact_OPFeeDetail]   WHERE [VisitDateID] BETWEEN MonthInfo.MinDateKey AND MonthInfo.MaxDateKey)

View Code

SQLStatement执行结果:

设置结果集:

2.2、Foreach 循环容器便利结果集

2.3、编写脚本任务,处理每次的遍历结果:

2.4、 点击“编辑脚本”:  引用:Analysis Management Obejcts包

脚本代码(在SQL SERVER 2008请使用语句:(String)Dts.Variables["Partition"].Value;进行强制,不要使用Dts.Variables["User::Partition"].Value.ToString(); ):

#region Help:  Introduction to the script task
/* The Script Task allows you to perform virtually any operation that can be accomplished in* a .Net application within the context of an Integration Services control flow. * * Expand the other regions which have "Help" prefixes for examples of specific ways to use* Integration Services features within this script task. */
#endregion#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using Microsoft.AnalysisServices;
#endregionnamespace ST_4038a8110570463994b546d9f7d48b3d
{[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase{#region ScriptResults declaration/// <summary>/// This enum provides a convenient shorthand within the scope of this class for setting the/// result of the script./// /// This code was generated automatically./// </summary>enum ScriptResults{Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure};#endregionpublic void Main(){// TODO: Add your code hereString sPartition = Dts.Variables["User::Partition"].Value.ToString();     String sCubeName = Dts.Variables["User::CubeName"].Value.ToString();String sMeasureGroup = Dts.Variables["User::MeasureGroup"].Value.ToString();          String sServer = "localhost";String sDataBaseID = Dts.Variables["User::DatabaseID"].Value.ToString();           String sCubeID = Dts.Variables["User::CubeID"].Value.ToString();         String sMeasureGroupID = Dts.Variables["User::MeasureGroupID"].Value.ToString();         String sDataSoureID = Dts.Variables["User::DataSoureID"].Value.ToString();String sSQL = Dts.Variables["User::SQL"].Value.ToString();String sMaxDateKey = Dts.Variables["User::MaxDateKey"].Value.ToString();        String sMinDateKey = Dts.Variables["User::MinDateKey"].Value.ToString();String aSql = sSQL.Replace("where_clause", "where VisitDateID  &gt;=" + sMinDateKey + " and VisitDateID &lt;=" + sMaxDateKey);ConnectionManager cm = Dts.Connections.Add("MSOLAP100");cm.ConnectionString = "Provider=MSOLAP.4;Data Source=localhost;IntegratedSecurity=SSPI;Initial Catalog=" + sDataBaseID;Microsoft.AnalysisServices.Server aServer = new Server();            aServer.Connect(sServer);           Microsoft.AnalysisServices.Database aDatabase = aServer.Databases.FindByName(sDataBaseID);       CubeCollection cubeCollection = aDatabase.Cubes;foreach (Cube item in cubeCollection){string name = item.Name;}Microsoft.AnalysisServices.Cube aCube = aDatabase.Cubes.FindByName(sCubeName);Microsoft.AnalysisServices.MeasureGroup aMeasureGroup;try{MeasureGroupCollection collection = aCube.MeasureGroups;aMeasureGroup = collection.FindByName(sMeasureGroup);}catch (Exception ex){throw ex;}if (aMeasureGroup.Partitions.Contains(sPartition)){Dts.Variables["User::IsNotePresent"].Value = false;Dts.Variables["User::Xmla_script"].Value = "";Dts.TaskResult = (int)ScriptResults.Success;}else{Dts.Variables["User::IsNotePresent"].Value = true;Dts.Variables["User::Xmla_script"].Value ="<Create xmlns=\"http://schemas.microsoft.com/analysisservices/2003/engine\">"+ "<ParentObject>"+ "<DatabaseID>" + sDataBaseID + "</DatabaseID>"+ "<CubeID>" + sCubeID + "</CubeID>"+ "<MeasureGroupID>" + sMeasureGroupID + "</MeasureGroupID>"+ "</ParentObject>"+ "<ObjectDefinition>"+ "<Partition xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:ddl2=\"http://schemas.microsoft.com/analysisservices/2003/engine/2\" xmlns:ddl2_2=\"http://schemas.microsoft.com/analysisservices/2003/engine/2/2\" xmlns:ddl100_100=\"http://schemas.microsoft.com/analysisservices/2008/engine/100/100\" xmlns:ddl200=\"http://schemas.microsoft.com/analysisservices/2010/engine/200\" xmlns:ddl200_200=\"http://schemas.microsoft.com/analysisservices/2010/engine/200/200\">"+ "<ID>" + sPartition + "</ID>"+ "<Name>" + sPartition + "</Name>"+ "<Source xsi:type=\"QueryBinding\">"+ "<DataSourceID>" + sDataSoureID + "</DataSourceID>"+ "<QueryDefinition>" + aSql + "</QueryDefinition>"+ "</Source>"+ "<StorageMode>Molap</StorageMode><ProcessingMode>Regular</ProcessingMode>"+ "<ProactiveCaching><SilenceInterval>-PT1S</SilenceInterval><Latency>-PT1S</Latency><SilenceOverrideInterval>-PT1S</SilenceOverrideInterval><ForceRebuildInterval>-PT1S</ForceRebuildInterval>"+ "<Source xsi:type=\"ProactiveCachingInheritedBinding\" /></ProactiveCaching>"+ "</Partition>"+ "</ObjectDefinition>"+ "</Create>";Dts.TaskResult = (int)ScriptResults.Success;}}}
}

View Code

处理脚本返回值:如果@IsNotePresent是ture:就会将参数Xmla_script中的字符串传给下面的"Analysis Services 执行DDL任务"由它为度量值组生成分区;如果是false就返回null,不生成分区;

2.5、添加Analysis Services 执行 DDL 任务,处理返回值(即为度量值组生成分区):

2.6、执行SSIS任务,查看分区生成情况:    

转载于:https://www.cnblogs.com/java-oracle/p/6478853.html

SSIS添加分区-动态相关推荐

  1. mysql 添加分区_创建,增加,删除mysql表分区

    1.测试添加分区和删除分区 ###添加删除range分区 (1)创建一个分区: CREATE TABLE titles ( emp_no      INT NOT NULL, title        ...

  2. linu逻辑分区动态调整大小

    linu逻辑分区动态调整大小 注意: 这个动态调整的方法是有丢数据风险的,要确保调整的源分区没有使用或者使用率很低.源分区中如果有重要的文件最好先备份 在centos 6.5上操作过 lvdispla ...

  3. html5表单动态添加,js动态添加表单实例

    前段时间因为域名关系网站停了一段时间.别的不多说了,我前端结合php做一个批量建栏目的模块,用到这个 对了下点,这个添加input动态添加 name也是可设成不同有,我用的是"text&qu ...

  4. mysql添加分区健_MySQL添加分区添加索引

    添加新用户    今天在为mysql添加新用户时,找到下面这条指令:    insert into mysql.user(Host,User,Password) values("localh ...

  5. mysql如何增加分区maxvalue_oracle range分区表已经有了MAXVALUE 分区,如何添加分区?要不能删除MAXVALUE分区里的数据,不影响在线应用。...

    来做个实验说明该问题: 1.创建个分区表 SQL> create table p_range_test 2 (id number,name varchar2(100)) 3 partition ...

  6. android 动态单选,添加单选按钮动态︰android

    我需要添加单选按钮动态,意味着单选按钮可能是3,4,5或6,它将被添加水平和一行包含最多3单选按钮,如果有更多然后3那么它会出现在网格视图上方的单选按钮的下方,我在这里工作2天,但没有得到任何解决方案 ...

  7. Windows 10 (Win10) 将绿色免安装软件,添加到动态磁贴

    1.Windows10 自带的动态磁贴,大部分是我们不想要的,这就需要我们自己调整 2.对于刚安装的软件,我们可以在最近添加里面找到,直接拖到动态磁贴即可 3.对于绿色面免安装的软件,怎么将图标添加到 ...

  8. u-boot-2012.04.01移植到TQ2440(七):添加分区信息并启动jffs2文件系统

    一.添加分区信息并启动jffs2文件系统 烧写之前的内核看输出分区信息 TQ2440 # tftp 32000000uImage dm9000 i/o: 0x20000000, id:0x90000a ...

  9. PostgreSQL/pgsql自动添加分区子表

    网上之前能找到的例子,都是按数量创建分区子表. 而实际业务中,按日期分区应该是主流,如果照搬按数量创建分区子表的话,感觉不太合用,毕竟实施运维人员也不想动脑子,你居然要求不能频繁执行某个函数,然后又要 ...

最新文章

  1. cout 数组_C语言学习笔记(十)二维数组内存预设
  2. 收藏 | 2021 十大机器学习库
  3. SpringBoot 集成ip2region
  4. Underscore.js Version (1.2.3) 中文文档
  5. D-News|英特尔首推融合现实,亚马逊云服务市场占比超3成
  6. 汇编代码调用main和分配内存
  7. 十四、传智书城前台程序设计
  8. jQuery ajax post put 请求
  9. 用Python 开发您的第一个 XGBoost 模型(收藏)
  10. SQL数据库快速入门基础
  11. css实现背景颜色半透明的两种方法
  12. 仿苹果 底部弹窗 选择列表
  13. 设计之星 ai_漫画 |《钢铁之星》:AI肆虐的时代,夹杂人类阴谋
  14. 联通沃云 服务器使用点滴
  15. 「文末送书」如何让数据分析不脱离业务?
  16. AList和RaiDrive对百度网盘进行本地挂载(可上传下载)
  17. 2016-09-01[关于Paint笔触的了解]
  18. linux 打包压缩文件夹命令/压缩文件
  19. 【转贴】GCC内联汇编基础
  20. #python+open3d 3D模型的读取与应用

热门文章

  1. 三、Express 路由
  2. 04.卷积神经网络 W1.卷积神经网络(作业:手动/TensorFlow 实现卷积神经网络)
  3. LeetCode MySQL 1549. The Most Recent Orders for Each Product
  4. LeetCode 1169. 查询无效交易
  5. LeetCode 861. 翻转矩阵后的得分(贪心)
  6. 服务器系统网卡驱动装不上,网卡驱动装不上去怎么办?
  7. python 图片背景前景分离_【绝了】用 Python 把朋友头像变表情包!
  8. python爬虫爬微信红包_python 微信红包
  9. python 百度ocr安装_Python调用百度OCR实现图片文字识别的示例代码
  10. python开发自己的工具包_爬虫开发python工具包介绍 (4)