我不知道为什么这段代码输出0.34(我预计0.335)。 TSQL游标是否自动舍入货币类型变量? 请帮助我了解这一点。
DECLARE @Rate MONEY;
DECLARE csrRate
CURSOR FOR
SELECT 0.335
OPEN csrRate;
FETCH NEXT FROM
csrRate
INTO
@Rate
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @Rate
FETCH NEXT FROM csrRate INTO @Rate
END
CLOSE csrRate
DEALLOCATE csrRate
答案 0 :(得分:3)
这是使用PRINT
时的默认行为。它将money
转换为string
。 default是:
所以,你可以像这样投射:
DECLARE @Rate MONEY;
DECLARE csrRate
CURSOR FOR
SELECT $0.335
OPEN csrRate;
FETCH NEXT FROM
csrRate
INTO
@Rate
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @Rate
PRINT convert(varchar(30), @Rate, 2)
--SELECT @RATE
FETCH NEXT FROM csrRate INTO @Rate
END
CLOSE csrRate
DEALLOCATE csrRate
此外,如果您尝试SELECT
该值,则不会转换为字符串。