我正在运行以下存储的precedure.
结果集在gridView控件上绑定.
@EmbossLine varchar(20),
@TransactionTypeID int,
@DateFrom datetime,
@DateTo datetime
AS
Select
pt.TransactionDate,
m.MerchantName1,
pt.TerminalID,
pt.SequenceNumber,
tt.TransactionType,
case TT.TransactionTypeID when 0 then PT.TotalAmount else 'null' end 'PointsEarned',
case TT.TransactionTypeID when 2 then PT.TotalAmount else 'null' end 'PointsRedemeed'
from POS_Transactions PT
inner join TransactionType TT on TT.TransactionTypeID = PT.TransactionTypeID
inner join CardBalance CB on CB.PAN = PT.PAN
inner join Terminal T on T.TerminalID = PT.TerminalID
inner join Merchant M on M.MerchantID = T.MerchantID
where
(PT.TransactionDate>=@DateFrom and PT.TransactionDate<=@DateTo)
and (PT.TransactionTypeID = @TransactionTypeID or @TransactionTypeID ='-999')
and(PT.PAN=@EmbossLine or PT.PAN='-999')
order by PT.TransactionDate desc
实际上我想要做的是,每当我的TransactionTypeID不等于0或2时我想打印null但是它会抛出一个运行时异常
Cannot convert a char value to money. The char value has incorrect syntax.
我知道这是一个转换问题,想要知道当条件评估为false时如何打印一些值
最佳答案 改变这两行:
case TT.TransactionTypeID when 0 then PT.TotalAmount else 'null' end 'PointsEarned',
case TT.TransactionTypeID when 2 then PT.TotalAmount else 'null' end 'PointsRedemeed'
对此:
case TT.TransactionTypeID when 0 then PT.TotalAmount else null end 'PointsEarned',
case TT.TransactionTypeID when 2 then PT.TotalAmount else null end 'PointsRedemeed'
您希望在其他条件中生成空值.您当前正在生成单词’null’,这是转换失败的地方 – 您无法将单词(char数据类型)’null’转换为money数据类型.
编辑:
您需要将值强制转换为字符串才能执行此操作.但这意味着不是将money数据类型返回给您的客户端,而是返回一个字符串.
这种事情应该在客户端而不是在SQL Server中处理.在任何情况下尝试将所有内容转换为字符串:
case TT.TransactionTypeID when 0 then Cast(PT.TotalAmount as varchar(50)) else 'null' end 'PointsEarned',
case TT.TransactionTypeID when 2 then Cast(PT.TotalAmount as varchar(50)) else 'null' end 'PointsRedemeed'