Hi All,
I am working on extracting Cube Metadata as we have to do a massive refactoring.
I have written a c# to extract list of Tables/Views/Named Queries from DSV but also looking for Data Source name for each table/View/NamedQuery.
For Example: In My Cube Database, I have 2 connections, One pointing to SQL Server DB (DataSource_SQL) and 2nd one to Oracle DB (DataSource_Oracle).
I have a DSV (as we know a single DSV can have Tables/Views/NamedQuery from multiple Data Sources ) which has tables from both the Data Sources (DataSource_SQL) & DataSource_Oracle
Using below code I am getting the list of Tables/Views/Named Queries from each DSV in a SSAS Database. but want to include Data source for each table.
This code is inside a SSIS Script Task
#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
using Microsoft.AnalysisServices;
using System.Data.SqlClient;
#endregion
/************************************************************************************
* This Code is to Export SSAS Data Source View to a SQL Server Table
************************************************************************************/
namespace ST_ce1153fb3fc948db939934521530439e
{
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
public void Main()
{
string olapServerName = Dts.Variables["$Package::pSSASServerName"].Value.ToString();
string olapDatabaseName = Dts.Variables["$Package::pSSASDataBaseName"].Value.ToString();
string dbServerConnectionString = Dts.Variables["$Package::pBDConnectionString"].Value.ToString();
using (SqlConnection sqlDbCon = new SqlConnection(dbServerConnectionString))
{
sqlDbCon.Open();
string sSSASDatabaseName;
string sDataSourceViewName;
string sTableFriendlyName;
string sTableType;
string sTableName;
string sSchema;
// connect to the OLAP server
Server olapServer = new Server();
olapServer.Connect(olapServerName);
if (olapServer != null)
{
// connected to server ok, so obtain reference to the OLAP database
Database olapDatabase = olapServer.Databases.FindByName(olapDatabaseName);
if (olapDatabase != null)
{
sSSASDatabaseName = olapDatabaseName;
// export SQL from each data source view (usually only one, but can be many!)
foreach (DataSourceView dsv in olapDatabase.DataSourceViews)
{
Console.WriteLine(string.Format("Exporting SQL from DSV '{0}'", dsv.Name));
sDataSourceViewName = dsv.Name;
// for each table in the DSV, export the SQL in a file
foreach (DataTable dt in dsv.Schema.Tables)
{
sTableName = string.Empty;
sSchema = string.Empty;
// get name of the table in the DSV
// use the FriendlyName as the user inputs this and therefore has control of it
string queryName = dt.ExtendedProperties["FriendlyName"].ToString();
sTableFriendlyName = queryName;
sTableType = dt.ExtendedProperties["TableType"].ToString();
if (dt.ExtendedProperties["QueryDefinition"] != null)
{
sTableType = "Named Query";
sTableName = dt.ExtendedProperties["QueryDefinition"].ToString();
}
else
{
sTableName = dt.ExtendedProperties["DbTableName"].ToString();
sSchema = dt.ExtendedProperties["DbSchemaName"].ToString();
}
string cmdString = "INSERT INTO [SSASMultidimensionalStaging].[DataSourceView] " +
"(SSASDatabaseName,DataSourceViewName,TableFriendlyName,TableType,[TableName],[Schema],ModifiedDateTime,CreatedDateTime)" +
"VALUES (@val1, @val2, @val3, @val4, @val5, @val6, @val7, @val8)";
using (SqlCommand comm = new SqlCommand())
{
comm.Connection = sqlDbCon;
comm.CommandText = cmdString;
comm.Parameters.AddWithValue("@val1", sSSASDatabaseName);
comm.Parameters.AddWithValue("@val2", sDataSourceViewName);
comm.Parameters.AddWithValue("@val3", sTableFriendlyName);
comm.Parameters.AddWithValue("@val4", sTableType);
comm.Parameters.AddWithValue("@val5", sTableName);
comm.Parameters.AddWithValue("@val6", sSchema);
comm.Parameters.AddWithValue("@val7", DateTime.Now);
comm.Parameters.AddWithValue("@val8", DateTime.Now);
comm.ExecuteNonQuery();
}
}
}
}
}
sqlDbCon.Close();
}
Dts.TaskResult = (int)ScriptResults.Success;
}
#region ScriptResults declaration
/// <summary>
/// This enum provides a convenient shorthand within the scope of this class for setting the
/// result of the script.
///
/// This code was generated automatically.
/// </summary>
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
}
}
Thanks Shiven:) If Answer is Helpful, Please Vote