2012-05-09 65 views
0

我想要做什么似乎是一个基本的int到小数,并且它不工作。我已经试过在插入之前执行演员,没有更好的。我想使用十进制值来显示百分比变化。代码如下,以及输出。任何帮助将是巨大的..SQL Server 2008 R2,麻烦铸造int到十进制

DECLARE @tblVar TABLE (LatestCount INT, PriorCount INT, 
         PctChgLatestVsPrior DECIMAL, PctChgLatestVsAvg DECIMAL) 

DECLARE @tbl CHAR(30) 
DECLARE @dte DATE 
DECLARE @dte2 DATE 
DECLARE @latestCount INT 
DECLARE @priorCount INT 
DECLARE @avgAllCounts INT 

SET @tbl = 'tblDailyPricingAndVol' 
SET @dte = '5/8/12' 
SET @dte2 = '5/7/12' 

EXEC spAvgOfCountByDateAddedByTable @tbl, @avg = @avgAllCounts OUTPUT 
EXEC spCountOfDateAddedByTableAndDate @tbl, @dte, @count = @latestCount OUTPUT 
EXEC spCountOfDateAddedByTableAndDate @tbl, @dte2, @count = @priorCount OUTPUT 

INSERT INTO @tblVar 
VALUES (@latestCount, @priorCount, 
    CAST((@[email protected]) AS DECIMAL)/CAST(@priorCount AS DECIMAL), 
    CAST((@[email protected]) AS DECIMAL)/CAST(@avgAllCounts AS DECIMAL)) 

SELECT * FROM @tblVar 

输出:

19548 
7107 
8483 

LatestCount PriorCount PctChgLatestVsPrior PctChgLatestVsAvg 
7107  8483  0     -1 
+2

“它不工作”不是对实际问题的很好描述。 **什么**不起作用?究竟? – Oded

+0

评论的部分先于“它不工作”。演员。 – StatsViaCsh

回答

6

你需要给小数秤/精密

在这里看到:Decimal and Numeric problems when you don't specify precision and scale

一些例子:十进制(30, 10)或DECIMAL(20)

查看更多书籍在线http://msdn.microsoft.com/en-us/library/ms187746.aspx

+2

+1 OP应该试试这个:'SELECT CONVERT(DECIMAL,3.567);'并且读取它(当然同样适用于'DECIMAL'):http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10 /09/bad-habits-to-kick-declaring-varchar-without-length.aspx –

+0

谢谢...我在一个简单的select语句中有非常相似的代码,它的工作方式与我的预期相同,也许它在存储过程中执行的方式不同。 – StatsViaCsh