Using VBA in Excel, how do I get the names of all OLAP Cubes on a server?

余生长醉 提交于 2020-02-05 04:09:06

问题


The company I work for creates an OLAP Cube Database for each of our clients. I keep up with many things on a daily basis using values in the cubes. Whenever a new clients cube is set up, I add a sheet to the workbook I use, and create a pivot table using that cube. I want to create a macro that checks the server for any cubes that may have been added. I figured doing something like this, below, would be the best way

For Each Cube in Server.Cubes
    MsgBox Cube.Name
Next Cube

...but I cannot find anything of the sort. I have searched for an answer for a couple of days now with no luck. Pretty much any way to parse through the server looking at the available cubes would help me. Any ideas?


回答1:


The SSAS Server has DMVs that you can query to determine the number of cubes on a server. Then you can use VBA to compare that to the number of rows in the table before. Follow these instructions to make the connection, or see below.

  1. Create a new connection in Excel: In the Get External Data section choose From Other Sources -> SQL Server (NOT Analysis Services).
  2. Enter connection information for any SQL Server to which you can connect (we'll change this info in a later step).
  3. Pick any database and table to which you have access. Move through the wizard and choose Only save connection at the end.
  4. Click Connections. Find your connection and click the Properties button.
  5. On the Definition tab, update the connnection string to look like

    Provider=MSOLAP.5;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=SSASDB;Data Source=MyServer\MyInstance;MDX Compatibility=1;Safety Options=2;MDX Missing Member Mode=Error
    
  6. Change the Command Type to Default

  7. Change the command text to the following:

    SELECT 
    [CATALOG_NAME] AS SSAS_Database_Name,
    [CUBE_NAME] AS Cube_or_Perspective_Name,
    [CUBE_CAPTION] AS Cube_or_Perspective_Caption,
    [CUBE_TYPE] AS Cube_Type,
    [BASE_CUBE_NAME] AS Base_Cube
    FROM 
    $SYSTEM.MDSCHEMA_CUBES
    WHERE
    CUBE_SOURCE=1
    AND
    [BASE_CUBE_NAME] < ''
    
  8. Click OK and then click Close.

  9. Click Existing Connections. Choose your connection.

  10. Choose Table on the Import Data Window. Choose to Put your table on a new worksheet.

  11. Your table should be in columns A through E. In cell G2 put Prior Row Count:

  12. In cell G3 put Current Row Count:

  13. In cell H2 put 0.

  14. In cell H3, enter the following formula:

    =COUNTA(Table_ExternalData_1[SSAS_Database_Name])
    
  15. Write a macro that copies the value from cell H3 to H2 and then refreshes the data connection for the table. Mine looks like this:

    Sub UpdateCubeCount()
        Range("H3").Select
        Selection.Copy
        Range("H2").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Range("B2").Select
        Application.CutCopyMode = False
        Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
    End Sub
    
  16. As a bonus, add conditional formatting to cell H3. Format it to have pink background and red text for the rule Cell Value > $H$2.

You end up with something that looks like this:

Update: If you are looking for the SSAS databases rather than the cubes themselves, you can use this query instead of the one in step 7:

SELECT [catalog_name] AS SSAS_Database_Name, [date_modified] 
FROM $system.DBSCHEMA_CATALOGS

This would be useful if you can assume you only have one cube per database, which is fairly common.



来源:https://stackoverflow.com/questions/28864254/using-vba-in-excel-how-do-i-get-the-names-of-all-olap-cubes-on-a-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!