2012-05-07 103 views
0

我只是将一些计算列添加到返回结果集的存储过程中。我从C#/ .NET调用它。新列(对端)都返回值0。这里是什么,我回来的第一行,并在它下面的存储过程:在SQL Server 2008 sprocs中不返回列别名'/计算列吗?

Daily Pricing and Volume  5/4/2012 5229 5249 5256 0 0 


ALTER PROCEDURE [dbo].[spProcedureStatsCollectionCountComparisons] 

AS 

DECLARE @MaxDataDownloadDate date 

SET @MaxDataDownloadDate = dbo.MostRecentTradeDateForDataDownload() 

SELECT ProcedureName, TradeDate, LatestCount, PriorCount, AvgAllCounts, 
    ((LatestCount-PriorCount)/PriorCount) AS PctChgLatestVsPrior, 
    ((LatestCount-AvgAllCounts)/AvgAllCounts) AS PctChgLatestVsAvg 
FROM vwProcedureStatsCollectionCountComparisons 
WHERE TradeDate = @MaxDataDownloadDate 

我的问题是,我不能使用列别名'从存储过程的结果集中,还是有一些其他问题在工作中,我错过了?在此先感谢..

按照要求,相关的C#代码:

SqlDataReader collectionCounts = dal.ProcedureStatsCollectionCountComparisons(); 
int rowCounter = 4; 
while (collectionCounts.Read()) 
{ 

    wkhst.Cells[rowCounter, 1] = collectionCounts["ProcedureName"]; 
    wkhst.Cells[rowCounter, 2] = collectionCounts["TradeDate"]; 
    wkhst.Cells[rowCounter, 3] = collectionCounts["LatestCount"]; 
    wkhst.Cells[rowCounter, 4] = collectionCounts["PriorCount"]; 
    wkhst.Cells[rowCounter, 5] = collectionCounts["AvgAllCounts"]; 
    wkhst.Cells[rowCounter, 6] = collectionCounts["PctChgLatestVsPrior"]; 
    wkhst.Cells[rowCounter, 7] = collectionCounts["PctChgLatestVsAvg"]; 

    rowCounter++; 
} 

excelApp.Visible = true; 
+0

向我们展示你的C#代码。应该没有问题,使用别名列 –

+0

谢谢,刚刚添加它。 – StatsViaCsh

+0

它必须是其他问题,我有SPs谁使用列别名在你的例子在2008年服务器,并且他们工作正常。你的*计数变量是什么数据类型?他们可以是无符号类型,并且由于(LatestCount-PriorCount)的结果<0,它的计算结果为0? – Treb

回答

2

按我的知识,并根据提供的数据,它是按预期工作。这是因为,你在做下面的事情。

SELECT ((5229 - 5249)/5249) 

总是返回“0”,因为第一个数字小于第二个数字。

编辑:

SELECT(CAST((5229 - 5249)AS FLOAT)/ CAST(5249 AS FLOAT))

+0

Praveen,谢谢,但它不是我所期望的。 ;)你知道如何从这些计算列中找回带符号的小数吗? – StatsViaCsh

+0

您可以将其转换为另一种数据类型:http://msdn.microsoft.com/en-us/library/ms187928(v=sql.105).aspx。抛出两个* count变量,结果应该自动具有匹配的数据类型。 – Treb

+0

这样做Praveen,谢谢。 – StatsViaCsh