|
/********************************************************************* ' 文件:ClonePart.vbs ' '说明:根据 FoodMart 2000 Sales 多维数据集中的最新分区, ' 这个脚本示例会在该多维数据集中创建新的分区。 ' 此脚本的目的是显示用于复制一个分区的 DSO 呼叫类型。 ' 产生的分区将经过处理,但多维数据集中 ' 不会增加任何数据 ' ' 脚本用户可在运行脚本和查看结果后 ' 删除产生的分区。 ' ' 参数: 无 '*********************************************************************/
Call ClonePart
Sub ClonePart()
On Error Resume Next
Dim intDimCounter, intErrNumber Dim strOlapDB, strCube, strDB, strAnalysisServer, strPartitionNew Dim dsoServer, dsoDB, dsoCube, dsoPartition, dsoPartitionNew
' 初始化服务器、数据库和多维数据集名变量。 strAnalysisServer = "LocalHost" strOlapDB = "FoodMart 2000" strCube = "Sales"
' VBScript 不支持直接使用枚举常量。. ' 然而,可定义常量以取代枚举。 Const stateFailed = 2 Const olapEditionUnlimited = 0
' 连接到分析服务器。 Set dsoServer = CreateObject("DSO.Server") dsoServer.Connect strAnalysisServer
' 如果连接失败,则结束枚举。 If dsoServer.State = stateFailed Then MsgBox "Error-Not able to connect to '" & strAnalysisServer _ & "' Analysis server.", ,"ClonePart.vbs" Err.Clear Exit Sub End if
' 某些分区管理功能只有在分析服务的 ' Enterprise 和 Developer 发行版中 ' 才可使用。 If dsoServer.Edition <> olapEditionUnlimited Then MsgBox "Error-This feature requires Enterprise or " & _ "Developer Edition of SQL Server to " & _ "manage partitions.", , "ClonePart.vbs" Exit Sub End If
' 确定数据库中有有效的数据源。 Set dsoDB = dsoServer.mdStores(strOlapDB) If dsoDB.Datasources.Count = 0 Then MsgBox "Error-No data sources found in '" & _ strOlapDB & "' database.", , "ClonePart.vbs" Err.Clear Exit Sub End If
' 查找多维数据集。 If (dsoDB.mdStores.Find(strCube)) = 0 then MsgBox "Error-Cube '" & strCube & "' is missing.", , _ "ClonePart.vbs" Err.Clear Exit Sub End If
' 将 dsoCube 变量设置到所要的多维数据集。 Set dsoCube = dsoDB.MDStores(strCube)
' 查找分区 If dsoCube.mdStores.Count = 0 Then MsgBox "Error-No partitions exist for cube '" & strCube & _ "'.", , "ClonePart.vbs" Err.Clear Exit Sub End If
' 将 dsoPartition 变量设置到所要的分区。 Set dsoPartition = dsoCube.MDStores(dsoCube.MDStores.Count) MsgBox "New partition will be based on existing partition: " _ & chr(13) & chr(10) & _ dsoDB.Name & "." & dsoCube.Name & "." & _ dsoPartition.Name, , "ClonePart.vbs"
' 从数据源获得引用字符,因为 ' 不同的数据源使用不同的引用字符。 Dim sLQuote, sRQuote sLQuote = dsoPartition.DataSources(1).OpenQuoteChar sRQuote = dsoPartition.DataSources(1).CloseQuoteChar
'********************************************************************* ' 根据所要的分区创建新分区。 '*********************************************************************
' 创建新的暂时分区。 strPartitionNew = "NewPartition" & dsoCube.MDStores.Count Set dsoPartitionNew = dsoCube.MDStores.AddNew("~temp")
' 从所要的分区复制属性到 ' 新分区。 dsoPartition.Clone dsoPartitionNew
' 将 "~temp" 分区名更改为 ' 想要供新分区使用的名称。 dsoPartitionNew.Name = strPartitionNew dsoPartitionNew.AggregationPrefix = strPartitionNew & "_"
' 设置新分区事实表。 dsoPartitionNew.SourceTable = _ sLQuote & "sales_fact_dec_1998" & sRQuote
' 设置新分区的 FromClause 和 JoinClause ' 属性。 dsoPartitionNew.FromClause = Replace(dsoPartition.FromClause, _ dsoPartition.SourceTable, dsoPartitionNew.SourceTable)
dsoPartitionNew.JoinClause = Replace(dsoPartition.JoinClause, _ dsoPartition.SourceTable, dsoPartitionNew.SourceTable)
' 将受影响的层次和维度的 SliceValue 属性 ' 更改为所要的值,以更改新分区使用的 ' 数据切片定义。 dsoPartitionNew.Dimensions("Time").Levels("Year").SliceValue = "1998" dsoPartitionNew.Dimensions("Time").Levels("Quarter").SliceValue = "Q4" dsoPartitionNew.Dimensions("Time").Levels("Month").SliceValue = "12"
' 估计行数。 dsoPartitionNew.EstimatedRows = 18325
' 添加另一筛选。SourceTableFilter 提供另外的 ' 机会,可将 WHERE 从句加入要填充的 SQL 查询。 ' 我们使用此筛选程序以确保新分区中未包含 ' 任何数据行。基于此示例代码的目的,我们不想 ' 更改 FoodMart 多维数据集中的数据。如果您想要新分区中 ' 看数据,请去掉此行。
dsoPartitionNew.SourceTableFilter = dsoPartitionNew.SourceTable _ & "." & sLQuote & "time_id" & sRQuote & "=100"
' 将分区定义保存在元数据知识库中。 dsoPartitionNew.Update
'检查新分区结构的有效性。 IF NOT dsoPartitionNew.IsValid Then MsgBox "Error-New partition structure is invalid." Err.Clear Exit Sub End If
MsgBox "New partition " & strPartitionNew & " has been created and " _ & "processed. To see the new partition in Analysis Manager, you " _ & "may need to refresh the list of partitions in the Sales cube " _ & "of FoodMart 2000. The new partition contains no data.", , _ "ClonePart.vbs"
' 下一语句已去掉,将处理分区。 ' 在实际分区管理系统中,这可能是一个单独的 ' 过程,可能由 DTS 管理。 ' dsoPartitionNew.Process
' 清理。 Set dsoPartition = Nothing Set dsoPartitionNew = Nothing Set dsoCube = Nothing Set dsoDB = Nothing dsoServer.CloseServer Set dsoServer = Nothing
End Sub |