2016-01-03 21 views
0

編號:Grouping SSAS members into buckets using MDX與執行MDX WITH子句中的T-SQL中引發錯誤

感謝您與有關分組SSAS成員到使用MDX水桶問題的幫助。當在SSAS中獨立運行或直接在SSMS中運行時,它可以很好地工作。

然而,我試圖包相同的一個T-SQL過程中使用下面的記錄插入表,執行失敗的錯誤消息:

declare @query nvarchar(4500) 
set @query = 'insert into ssas_results 
       select * from OPENQUERY(ssaslink,'' 
            select * from 
(
WITH 
SET [RestOfWorld] AS 
    { 
    [Customer].[Customer Geography].[Country].&[United Kingdom] 
,[Customer].[Customer Geography].[Country].&[Germany] 
    } 
MEMBER [Customer].[Customer Geography].[All].[RestOfWorld] AS 
    Aggregate 
(
    { 
    [Customer].[Customer Geography].[Country].&[United Kingdom] 
    ,[Customer].[Customer Geography].[Country].&[Germany] 
    } 
) 
SET [CountriesMinusROW] AS 
[Customer].[Customer Geography].[Country].MEMBERS - [RestOfWorld] 
SELECT 
NON EMPTY 
{[Measures].[Internet Sales Amount]} ON 0 
,NON EMPTY 
    [Product].[Category].[Category] 
* 
    { 
    [CountriesMinusROW] 
    ,[Customer].[Customer Geography].[All].[RestOfWorld] 
    } ON 1 
FROM [Adventure Works] 
WHERE 
[Date].[Calendar Year].&[2007])'') AS SSASREC' 

exec (@query) 

When I executed the stored procedure, I get the error message: 

OLE DB provider "MSOLAP" for linked server "ssaslink" returned message  "Subselects and subcubes cannot define new calculations using the WITH clause.". 

消息7321,級別16,國家2,2號線 時發生錯誤,準備查詢「

是這種方法在SSAS不支持?什麼是我可以使用存儲過程插入查詢到表的結果的最佳方式是什麼?

回答

1

第二個參數因爲它將在定義的SSAS服務器實例上執行,因此OPENQUERY中的eter需要是有效的MDX。擺脫select * from (...) AS SSASREC構造,並保持內部引號MDX

編輯基於OP的評論

對於包括參數,MDX有特殊StrToMemberStrToSet功能。 注意:雖然在這種情況下使用StrToMember/StrToSet並不是真正需要的(正如爲什麼正確地說),但這些功能可以提供一定程度的安全性。當這些函數中的第二個參數是「CONSTRAINED」時,它將過濾掉注入攻擊。所以在安全方面是一個「加號」。

從BOL

當使用約束標誌,則部件名稱必須是直接解析爲一個合格的或不合格的構件名稱。此標誌用於通過指定的字符串降低注入攻擊的風險。如果提供的字符串不能直接解析爲合格或不合格的成員名稱,則會出現以下錯誤:「STRTOMEMBER函數中CONSTRAINED標誌施加的限制被違反。」

嘗試下面的內容。

declare @query nvarchar(4500) 
declare @date VARCHAR(30) 

set @date = '[Date].[Calendar Year].&[' + '2007' + ']' 

set @query = 'select * from OPENQUERY(ssaslink,         
''WITH 
SET [RestOfWorld] AS 
    { 
    [Customer].[Customer Geography].[Country].&[United Kingdom] 
,[Customer].[Customer Geography].[Country].&[Germany] 
    } 
MEMBER [Customer].[Customer Geography].[All].[RestOfWorld] AS 
    Aggregate 
(
    { 
    [Customer].[Customer Geography].[Country].&[United Kingdom] 
    ,[Customer].[Customer Geography].[Country].&[Germany] 
    } 
) 
SET [CountriesMinusROW] AS 
[Customer].[Customer Geography].[Country].MEMBERS - [RestOfWorld] 
SELECT 
NON EMPTY 
{[Measures].[Internet Sales Amount]} ON 0 
,NON EMPTY 
    [Product].[Category].[Category] 
* 
    { 
    [CountriesMinusROW] 
    ,[Customer].[Customer Geography].[All].[RestOfWorld] 
    } ON 1 
FROM [Adventure Works] 
WHERE 
StrToMember('''+CAST(@date AS VARCHAR(50))+''',CONSTRAINED)''' 


insert into ssas_results 
exec (@query) 
1

真的只是隨聲sourav已經說的 - 你可以簡化爲這樣的事情:

沒有必要在這種情況下使用StrToMember,因爲你是通過整個腳本作爲一個字符串...

DECLARE @Year INTEGER = YEAR(GETDATE()); 
DECLARE @DtString VARCHAR(50)= '[Date].[Calendar Year].&[' + CONVERT(VARCHAR(10),@Year) + ']'; 

INSERT INTO ssas_results 
SELECT 
    * 
FROM 
    OPENQUERY 
    (
    ssaslink, 
    ' 
    WITH 
    SET [RestOfWorld] AS 
     { 
     [Customer].[Customer Geography].[Country].&[United Kingdom] 
    ,[Customer].[Customer Geography].[Country].&[Germany] 
     } 
    MEMBER [Customer].[Customer Geography].[All].[RestOfWorld] AS 
     Aggregate 
    (
     { 
      [Customer].[Customer Geography].[Country].&[United Kingdom] 
     ,[Customer].[Customer Geography].[Country].&[Germany] 
     } 
    ) 
    SET [CountriesMinusROW] AS 
    [Customer].[Customer Geography].[Country].MEMBERS - [RestOfWorld] 
    SELECT 
    NON EMPTY 
    {[Measures].[Internet Sales Amount]} ON 0 
    ,NON EMPTY 
     [Product].[Category].[Category] 
    * 
     { 
      [CountriesMinusROW] 
     ,[Customer].[Customer Geography].[All].[RestOfWorld] 
     } ON 1 
    FROM [Adventure Works] 
    WHERE ' 
     + 
      @DtString 
     + 
     ';'  
    ); 
+0

謝謝whytheq和SouravA的迴應。我必須將它包裝在T-SQL過程中,因爲日期和其他一些字段是可變的,並插入表中以進行其他一些計算。儘管最初我嘗試過 - 將MDX直接放在引號內並得到相同的錯誤。會再次給它一次 - 也許我第一次沒有做對。 – Bee

+0

@Bee - 檢查我的編輯。 – SouravA

+0

提高了,指出了一點。我幾乎沒有'OPENQUERY'的經驗。你的建議是有效的。 – SouravA