[SQL Server 2005 BI]在.NET中创建SQL 2005 KPI
项目中要从Web端根据维护的参数在Cube中创建KPI,查了一天多资料,只查到MSDN中有一个"CREATE KPI..."的写法比较靠谱,可是把它拷到Analysis Services MDXQuery中执行,发现KPI根本就不是关键字,这怎么能执行得了。急死人了。到最后才发现"CREATE KPI..."是SQL Server 2008才新增的功能。2005中当然不能用了。晕。
最后终于查到解决方法如下:
SQL Server 2008中可以用下面的MDX语句创建KPI,但是在SQL Server 2005中却不行。
[,Property_Name = Property_Value, n]
所以对SQL 2005没办法通过在.NET中执行MDX语句的方法来创建KPI了。
SQL Server 2005可以用AMO对象创建KPI,如下:
{
Server testServer = new Server();
testServer.Connect("Data Source=myServer;Catalog=Analysis Services Tutorial;ConnectTo=9.0;Integrated Security=SSPI");
if (testServer != null && testServer.Connected)
{
Database testDatabase = testServer.Databases.FindByName("Analysis Services Tutorial");
if (testDatabase != null)
{
Cube testCube = testDatabase.Cubes.FindByName("Analysis Services Tutorial");
if (testCube != null)
{
KpiCollection kpiCollection = testCube.Kpis;
Kpi testKPI = kpiCollection.Add("TestKPI");
testKPI.Value = "[Measures].[Reseller Sales-Sales Amount]";
testKPI.Goal = "[Measures].[Sales Amount Quota]";
testKPI.StatusGraphic = "Gauge";
testKPI.Status = "case when KPIVALUE(\"Reseller Revenue\")/KPIGOAL( \"Reseller Revenue\") >= 0.95 then 1 when KPIVALUE(\"Reseller Revenue\")/KPIGOAL( \"Reseller Revenue\") < 0.95 and KPIVALUE(\"Reseller Revenue\")/KPIGOAL( \"Reseller Revenue\") >= 0.85 then 0 else -1 end";
testKPI.TrendGraphic = "Standard arrow";
testKPI.Trend = "1";
testCube.Update();
}
}
testDatabase.Process();
testServer.Disconnect();
}
}
可以通过下面的MDX语句查询KPI:
from [Analysis Services Tutorial]
查询结果如下:
其它参考资料:
AMO OLAP 高级对象的编程(有CreateKPIs方法)(http://msdn.microsoft.com/zh-cn/library/ms345086.aspx)
CREATE KPI 语句 (MDX)(http://msdn.microsoft.com/zh-cn/library/bb510608.aspx)
How to retreive KPI from Analysis Services 2005 (http://blogs.msdn.com/olivier_pieri/archive/2004/12/06/275811.aspx)
Where do I create my KPIs?(http://nickbarclay.blogspot.com/2007/05/where-do-i-create-my-kpis.html)
发布一个C#+ADOMD.NET实现查看分析服务数据库信息的类对象(http://www.cnblogs.com/xuanfeng/archive/2008/07/13/1241878.html)
check this series of articles on Key Performance Indicators (http://www.databasejournal.com/features/mssql/article.php/3604206)
Kpi Class (http://technet.microsoft.com/zh-cn/library/microsoft.analysisservices.kpi.aspx)