我怎样才能从MDX OLAP多维数据集“拉”的数据转换成一个标准的SQL Server表?(How

2019-10-18 16:32发布

我已经提供了进入一个立方体,需要知道,如果我可以设置(通过MDX查询)的存储过程可以连接到多维数据集并检索内容。 我想这能防止不必将数据从Management Studio或从Excel(通过PowerPivot的)出口。 我很新的多维数据集/ OLAP查询所以请原谅所有的天真我可能会显示。

Answer 1:

最简单的方法是创建一个链接的服务器,以多维数据集,然后从INSERT..SELECT OPENQUERY http://sqlblog.com/blogs/stacia_misner/archive/2010/11/30/31193.aspx

此选项有其局限性,

  • 它对MDX查询的8000字符限制
  • 您必须手动创建链接的服务器为每个数据库
  • 特殊的代码需要处理时,结果为空
  • 在结果集中的列过多的数据类型(NTEXT用于列和nvarchar(4000)为行)

高级选项是ExecuteOLAP CLR存储过程https://olapextensions.codeplex.com/



Answer 2:

如果您选择使用OPENQUERY(最简单的方法,但有由Brian规定的限制),下面的过程可能会派上用场:

    /* 
PARAMS:
    @mdx: mdx statement
    @mdx_columns: specifies the mdx columns to retrieve from the executed mdx
    @linkedServer: linked server to be used
    @resultsTable: temporary table to hold results from mdx
    @resultsCols: if only some columns should be filled in @resultsTable, specify them here (e.g. '(col1, col2, ... )' )
    @expectedColCount: expected column count for mdx result. If actual column count is different from the expected count, no data is filled in
    @actualColCount: actual column count. Specify NULL if not interesed in this value
    @Debug: outputs debug info
*/
ALTER PROCEDURE [dbo].[exec_mdx_over_linked_server] (
    @mdx NVARCHAR(MAX),
    @mdx_columns NVARCHAR(1024) = '*',
    @linkedServer VARCHAR(64),
    @resultsTable VARCHAR(64),
    @resultsCols VARCHAR(1024) = '',
    @expectedColCount SMALLINT,
    @actualColCount SMALLINT = NULL OUTPUT,
    @Debug BIT = 0
)
AS
BEGIN
    SET NOCOUNT ON

    if (@Debug = 1)
        PRINT 'Started exec_mdx_over_linked_server procedure for populating ' + @resultsTable

    IF LEN(@MDX)>8000 RAISERROR ('MDX too long for openquery (exec_mdx_over_linked_server)', 
                                16,
                                1);

    declare @SQL NVARCHAR(MAX)

    IF (@Debug = 1)
    BEGIN
        -- getting results from mdx
        SET @SQL = 'SELECT ''Mdx results for ' + @resultsTable + ''' AS ''Mdx results'', ' + '*' + ' 
            FROM OPENQUERY(' + @linkedServer + ', ''' + @mdx + ''')';
        IF LEN(@SQL)>8000 RAISERROR ('MDX too long for openquery (exec_mdx_over_linked_server)', 
                                    16,
                                    1);
        EXEC (@SQL)
    END

    SET @SQL = '
        SELECT ' + '*' + ' INTO #resultsWithWeirdNameToAvoidTempCollisions
        FROM OPENQUERY(' + @linkedServer + ', ''' + @mdx + ''');

        SELECT @colCount = COUNT(*)
        FROM tempdb.sys.columns
        WHERE object_id = object_id(''tempdb..#resultsWithWeirdNameToAvoidTempCollisions'');

        if (@colCount = @expectedColCount)
            INSERT INTO ' + @resultsTable + @resultsCols + '
            SELECT ' + @mdx_columns +  ' FROM #resultsWithWeirdNameToAvoidTempCollisions'


    IF LEN(@SQL)>8000 RAISERROR ('MDX too long for openquery (exec_mdx_over_linked_server)', 
                                    16,
                                    1);

    if (@Debug = 1)
        PRINT 'dbo.exec_mdx_over_linked_server SQL = ' + @SQL

    DECLARE @colCount INT
    EXECUTE sp_executesql @SQL, N'@expectedColCount SMALLINT, @ColCount SMALLINT OUTPUT', @expectedColCount = @expectedColCount, @colCount = @actualColCount OUTPUT

    if (@Debug = 1)
    BEGIN
        PRINT '@expectedColCount = '; PRINT @expectedColCount
        PRINT '@actualColCount = '; PRINT @actualColCount
    END

    -- correction for small float numbers (< 10E-10)

    DECLARE @UpdateSql NVARCHAR(MAX) = N''
    DECLARE @SmallThreshold FLOAT = 0.00000000001


    SELECT @UpdateSql += '
                            UPDATE ' + @resultsTable + ' 
                            SET ' + QUOTENAME(COLUMN_NAME) + ' = 0 
                            WHERE TRY_CONVERT (FLOAT, ' + QUOTENAME(COLUMN_NAME) + ') IS NOT NULL
                                                 AND ABS(' + QUOTENAME(COLUMN_NAME) + ') < ' + CAST(@SmallThreshold AS NVARCHAR(30))
    FROM tempdb.INFORMATION_SCHEMA.COLUMNS with(NOLOCK)
    -- WHERE table_name like @resultsTable + '[_][_][_]%'
    -- changed, in order not to take into consideration objects from other spids
    WHERE table_name = object_name(object_id('tempdb..' + @resultsTable), (select database_id from sys.databases where name = 'tempdb'))

    IF (@Debug = 1)
    BEGIN
        PRINT '@UpdateSql = '; PRINT @UpdateSql;
    END

    EXEC (@UpdateSql);

END

它具有以下优点:

  • 处理的情况下,当接收到的结果没有预期的列(执行任何操作)
  • 执行一些圆角的非常小的数字(因为立方体知道浮点数的工作,这可能只发生)
  • 所有MDX语句去通过单一程序

在进行分析时,我已经约注意到了。 100ms的开销(通过程序与对分析服务器直接执行执行)。

.NET开发人员可以使用ADOMD.NET框架,其允许运行参数化查询和具有较小的开销。



文章来源: How can I 'pull' data from an MDX OLAP Cube into a standard SQL Server table?