SIGN UP MEMBER LOGIN:    
ARTICLE

Querying SQL Cube from .NET application using Stored Procedure

Posted by Shinuraj Articles | SQL Server 2012 May 06, 2011
Since the SQL cube is located inside a SQL Analytical database we need to add that as a linked server to our working SQL Server.
Reader Level:


Since the SQL cube is located inside a SQL Analytical database we need to add that as a linked server to our working SQL Server.

Following are the steps for linking the SQL Analytical service database to our local SQL Server:

Step 1: Open SQL Server Management studio and connect to our local/remote SQL Server

Step 2: Expand Server Object -> right click the Linked Severs -> click the "New Linked Server" option from the context menu. (See the figure below.)

SqlCube1.gif

Step 3: The following popup will open for adding a "New Linked Server". Make sure that you select the provider as "Microsoft OLE DB Provider for Analysis Services 10.0"

SqlCube2.gif

Step 4: Now you can view your cubes from the "Catalogs" folder in the linked server.

Step 5: Use the Openquery SQL statement as shown below to query the data from the cube:

SELECT * FROM Openquery ("LinkedServerName",'MDX Query for fetching data from cube')

Step 6: Create one stored procedure with this Openquery SQL statement and call this SP from a .Net application.
Sample Stored Procedure:

CREATE PROCEDURE [dbo].[SP_FetchDataFromCube]

AS
 
BEGIN

declare @mdx_query as varchar(max),
@open_query as nvarchar(max),
@linked_server as varchar(max)

set @mdx_query = 'SELECT NON EMPTY {[Measures].[Premium Paid]} ON COLUMNS,
NON EMPTY {([Branch].[Branch].[City])} DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS FROM [Comm_Per]'

set @linked_server = 'linked server OLAP'

set @open_query = 'SELECT * FROM OpenQuery
("'+@linked_server+'",'''+ @mdx_query + ''')'

execute sp_executesql @open_query

END

GO
 

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Become a Sponsor