注意:我讀到了類似的線程,但對此查詢沒有任何幫助。將數據類型varchar轉換爲浮點時出錯-ISNULL相關
我已經複製了以下代碼,其中類似於我的真實問題。我必須使用這種語法,因爲它已經在數據庫中使用過,除非有令人信服的建議,這裏的邏輯是錯誤的。
在我的實際問題中,此查詢工作大部分的時間,但在一定的時間內失敗。經過調查,我發現這個問題是
ISNULL(amount,0)
這是因爲,如果任何一類既具有值0,0或兩個值NULL,NULL,ISNULL
使他們的字符串,因此我得到
錯誤轉換數據類型爲varchar浮動
以下是我的測試碼帶註釋
create table #table1 (
id int not null primary key identity,
category varchar(10),
amount float null
)
insert into #table1 values('A',23)
insert into #table1 values('A',23)
insert into #table1 values('B',NULL)
insert into #table1 values('B',0)
insert into #table1 values('C',NULL)
insert into #table1 values('C',NULL)
insert into #table1 values('D',0)
insert into #table1 values('D',0)
select * from #table1 -- works
select category, sum1 -- works
from
(select category, SUM(Round(ISNULL(amount,0),0)) as Sum1 from #table1
group by category) D
select category, sum2 = -- does not work
case Sum1
when 'A' then Sum1 * 1 -- my problem is here
when 'B' then Sum1 * 2 -- this is the logic in my actual code
when 'C' then Sum1 * 3 -- would like to make this query work
when 'D' then Sum1 * 4
else Sum1
end
from
(select category, SUM(Round(ISNULL(amount,0),0)) as Sum1 from #table1
group by category) D
我在做什麼錯?
那是我在實際的代碼所做的確切的錯誤。謝謝。這是答案 – 2012-01-17 14:20:27