问题
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.
- Create a new connection in Excel: In the Get External Data section choose From Other Sources -> SQL Server (NOT Analysis Services).
- Enter connection information for any SQL Server to which you can connect (we'll change this info in a later step).
- Pick any database and table to which you have access. Move through the wizard and choose Only save connection at the end.
- Click Connections. Find your connection and click the Properties button.
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
Change the Command Type to Default
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] < ''
Click OK and then click Close.
Click Existing Connections. Choose your connection.
Choose Table on the Import Data Window. Choose to Put your table on a new worksheet.
Your table should be in columns A through E. In cell G2 put
Prior Row Count:
In cell G3 put
Current Row Count:
In cell H2 put 0.
In cell H3, enter the following formula:
=COUNTA(Table_ExternalData_1[SSAS_Database_Name])
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
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