共用方式為


MDX 命名集 - 建立 Query-Scoped 具名集

適用於: SQL Server Analysis Services Azure Analysis Services Fabric/Power BI Premium

如果單一多維度表示式 (MDX) 查詢只需要具名集,您可以使用WITH關鍵詞來定義該具名集。 使用WITH關鍵詞建立的具名集,在查詢完成執行之後已不存在。

如本主題所討論,WITH 關鍵詞的語法相當有彈性,即使是使用函式來定義命名集。

注意

如需命名集的詳細資訊,請參閱 在 MDX 中建置具名集

WITH 關鍵詞語法

使用下列語法將 WITH 關鍵詞新增至 MDX SELECT 語句:

[ WITH <SELECT WITH clause> [ , <SELECT WITH clause> ... ] ]   
SELECT [ * | ( <SELECT query axis clause> [ , <SELECT query axis clause> ... ] ) ]  
FROM <SELECT subcube clause>   
[ <SELECT slicer axis clause> ]  
[ <SELECT cell property list clause> ]  
  
<SELECT WITH clause> ::=  
   ( SET Set_Identifier AS 'Set_Expression')  
  

在WITH關鍵詞的語法中,Set_Identifier 參數包含具名集的別名。 Set_Expression 參數包含具名集別名所參考的 set 表達式。

WITH 關鍵詞範例

下列 MDX 查詢會檢查 FoodMart 2000中各種 Chardonnay 和 Chablis 葡萄酒的單位銷售,這是 SQL Server 2000 Analysis Services Microsoft範例資料庫。 此查詢雖然在結果集方面相當簡單,但當您必須維護這類查詢時,會很冗長且不複雜。

SELECT  
   {[Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Wine].[Good].[Good Chardonnay],   [Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Wine].[Pearl].[Pearl Chardonnay],   [Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Wine].[Portsmouth].[Portsmouth Chardonnay],   [Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Wine].[Top Measure].[Top Measure Chardonnay],   [Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Wine].[Walrus].[Walrus Chardonnay],   [Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Wine].[Good].[Good Chablis Wine],   [Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Wine].[Pearl].[Pearl Chablis Wine],   [Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Wine].[Portsmouth].[Portsmouth Chablis Wine],   [Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Wine].[Top Measure].[Top Measure Chablis Wine],   [Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Wine].[Walrus].[Walrus Chablis Wine]} ON COLUMNS,  
   {Measures.[Unit Sales]} ON ROWS  
FROM Sales  

若要讓先前的 MDX 查詢更容易維護,您可以使用 WITH 關鍵詞建立查詢的具名集。 下列程式代碼示範如何使用WITH關鍵詞來建立具名集,[ChardonnayChablis],以及命名集如何讓SELECT語句變得較短且更容易維護。

WITH SET [ChardonnayChablis] AS  
   {[Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Wine].[Good].[Good Chardonnay],  
   [Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Wine].[Pearl].[Pearl Chardonnay],  
   [Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Wine].[Portsmouth].[Portsmouth Chardonnay],  
   [Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Wine].[Top Measure].[Top Measure Chardonnay],  
   [Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Wine].[Walrus].[Walrus Chardonnay],  
   [Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Wine].[Good].[Good Chablis Wine],  
   [Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Wine].[Pearl].[Pearl Chablis Wine],  
   [Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Wine].[Portsmouth].[Portsmouth Chablis Wine],  
   [Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Wine].[Top Measure].[Top Measure Chablis Wine],  
   [Product].[All Products].[Drink].[Alcoholic Beverages].[Beer and Wine].[Wine].[Walrus].[Walrus Chablis Wine]}  
  
SELECT  
   [ChardonnayChablis] ON COLUMNS,  
   {Measures.[Unit Sales]} ON ROWS  
FROM Sales  

搭配WITH關鍵詞使用函式

雖然您可以明確定義具名集的每個成員,但此方法可能會產生冗長的語句。 若要讓具名集的建立和維護變得更容易,您可以使用 MDX 函式來定義成員。

例如,下列 MDX 查詢範例會使用 FilterCurrentMemberName MDX 函式和 InStr VBA 函式來建立 [ChardonnayChablis] 命名集。 這個版本的 [ChardonnayChablis] 命名集與本主題先前顯示的明確定義版本相同。

WITH SET [ChardonnayChablis] AS  
   'Filter([Product].Members, (InStr(1, [Product].CurrentMember.Name, "chardonnay") <> 0) OR (InStr(1, [Product].CurrentMember.Name, "chablis") <> 0))'  
  
SELECT  
   [ChardonnayChablis] ON COLUMNS,  
   {Measures.[Unit Sales]} ON ROWS  
FROM Sales  
  

另請參閱

SELECT 語句 (MDX)
建立具名集 Session-Scoped 集 (MDX)