在前面两篇博客中,介绍了动态分区(按照月份)和动态执行分区(每天 执行),但是在实际项目中会有问题:我们分区的CUBE是每天要执行的,但是动态分区每天不会重新生成!
脚本任务中的这段代码,决定了存在的分区将不会执行,造成数据缺失。
//如果分区存在将返回false,不在生成分区,如果是3月1号执行的动态生成分区SSIS包,那么以后的3月数据都将加载不进来
if (aMeasureGroup.Partitions.Contains(sPartition)) { Dts.Variables["User::IsNotePresent"].Value = false; Dts.Variables["User::Xmla_script"].Value = ""; Dts.TaskResult = (int)ScriptResults.Success; }
如何解决?
简单的做法是:我们可以在动态生成分区任务执行之前,对当前月份的分区动态的删除了,这样当前月份的分区就不存了,此时在执行生成分区代码,就会判断当前分区不存在而建立分区。参数的准备就按照上篇的动态分区准备即可!
1、在上面第一个“执行SQL任务”中,按照以下配置:
SQLStatment语句:得到当前月份
SELECT ‘Drug DW‘ AS DataSoureID,--数据源 ‘Drug_SSAS‘ AS DatabaseID,--分区来自哪一个cube ‘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 ‘ +left(convert(varchar(10),getdate(),112),6) AS Partition--分区名称=度量值组名称+年月
返回值:
结果集:
2、执行下的脚本:主要是删除当前月份的分区,当前月份分区存在就删掉,不存在则执行生成分区的任务!
/* Microsoft SQL Server Integration Services Script Task Write scripts using Microsoft Visual C# 2008. The ScriptMain is the entry point class of the script. */ using System; using System.Data; using Microsoft.SqlServer.Dts.Runtime; using System.Windows.Forms; using Microsoft.AnalysisServices; namespace ST_c86e3263ba3a4082bcf1335a1946efbb.csproj { [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")] public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase { #region VSTA generated code enum ScriptResults { Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success, Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure }; #endregion public void Main() { // TODO: Add your code here String sPartition =(String) Dts.Variables["Partition"].Value; String sCubeName = (String) Dts.Variables["CubeName"].Value; String sMeasureGroup = (String) Dts.Variables["MeasureGroup"].Value; String sServer = (String)Dts.Variables["IPserver"].Value; String sDataBaseID = (String) Dts.Variables["DatabaseID"].Value; String sCubeID = (String) Dts.Variables["CubeID"].Value; String sMeasureGroupID = (String) Dts.Variables["MeasureGroupID"].Value; String sDataSoureID = (String)Dts.Variables["DataSourceID"].Value; ConnectionManager cm = Dts.Connections.Add("MSOLAP100"); cm.ConnectionString = "Provider=MSOLAP.4;Data Source="+sServer+";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["XmlStr"].Value = "<Delete xmlns=\"http://schemas.microsoft.com/analysisservices/2003/engine\">" + " <Object>" + "<DatabaseID>" + sDataBaseID + "</DatabaseID>" + "<CubeID>" + sCubeID + "</CubeID>" + "<MeasureGroupID>" + sMeasureGroupID + "</MeasureGroupID>" + "<PartitionID>" + sPartition + "</PartitionID>" + "</Object>" + "</Delete> "; Dts.TaskResult = (int)ScriptResults.Success; } } } }
时间: 2024-10-30 02:49:43