CURSORを利用したデータ更新において、
前行のデータを保持しておく目的で、FETCHしないパラメータもFETCHするパラメータの後に宣言して使用しています。
そのデータ型はMONEYとDECIMAL(6,3)です。
さて、FETCHしないパラメータもデータを保持できるのですが、小数点以下を含む値を代入すると少数点以下第一位で四捨五入されて整数に丸められてしまいます。
基本的な質問にて恐縮ですが、本現象は仕様になりますでしょうか?
アドバイスなどいただけますと幸いです。
何卒よろしくお願いいたします。
どうにも先が見えない状況にてソースを掲載させていただきます。
問題点は@current_value_with_point_before_roundの小数点第一位が四捨五入されてしまうことでしたが、ISNULL(@current_point, 0)の@current_pointにNULLでない数値が代入された次の行からは、小数点以下が切り上げられず(0にならず)入力されることが分かりました。
説明不足かもしれませんが取り急ぎ。何卒よろしくお願いいたします。
CREATE TABLE [dbo].[t_history]
([code] CHAR(6) NOT NULL
,[date] DATETIME NOT NULL
,[value] MONEY
,[compared_previous_day_yen] MONEY DEFAULT NULL
,[compared_previous_day_percent] DECIMAL(5,2) DEFAULT NULL
,[compared_previous_day_percent_before_round] DECIMAL(6,3) DEFAULT NULL
,[value_with_point] MONEY DEFAULT NULL
,[value_with_point_before_round] DECIMAL(19,9) DEFAULT NULL
,[initial_date] INT
,[point] MONEY
CONSTRAINT [pk_t_history] PRIMARY KEY([code], [date]))
DECLARE current_day CURSOR
FOR SELECT [date]
,[value]
,[initial_date]
,[point]
FROM [dbo].[t_history]
ORDER BY [code]
,[date]
DECLARE @current_date DATETIME
,@current_value MONEY
,@previous_value MONEY
,@current_initial_date INT
,@current_point MONEY
,@previous_point MONEY
,@current_value_with_point MONEY
,@current_value_with_point_before_round DECIMAL(19,9)
,@previous_value_with_point MONEY
,@previous_value_with_point_before_round DECIMAL(19,9)
OPEN current_day
FETCH NEXT FROM current_day
INTO @current_date
,@current_value
,@current_initial_date
,@current_point
WHILE @@FETCH_STATUS = 0
BEGIN
IF @current_initial_date = YEAR(@current_date) * 10000 + MONTH(@current_date) * 100 + DAY(@current_date)
BEGIN
SET @current_value_with_point_before_round = @current_value
SET @current_value_with_point = @current_value
UPDATE [dbo].[t_history]
SET [compared_previous_day_yen] = NULL
,[compared_previous_day_percent] = NULL
,[value_with_point] = @current_value_with_point
,[value_with_point_before_round] = @current_value_with_point_before_round
WHERE CURRENT OF current_day
END
ELSE
BEGIN
SET @current_value_with_point_before_round = @previous_value_with_point_before_round * ((1.000000000 * (@current_value + ISNULL(@current_point, 0))) / @previous_value)
SET @current_value_with_point = ROUND(@current_value_with_point_before_round, 0)
UPDATE [dbo].[t_history]
SET [compared_previous_day_yen] = @current_value - @previous_value
,[compared_previous_day_percent] = ROUND((1.000000000 * @current_value / @previous_value - 1) * 100, 2)
,[compared_previous_day_percent_before_round] = (1.000000000 * @current_value / @previous_value - 1) * 100
,[value_with_point] = @current_value_with_point
,[value_with_point_before_round] = @current_value_with_point_before_round
WHERE CURRENT OF current_day
END
SET @previous_value = @current_value SET @previous_point = @current_point SET @previous_value_with_point = @current_value_with_point SET @previous_value_with_point_before_round = @current_value_with_point_before_round FETCH NEXT FROM current_day INTO @current_date ,@current_value ,@current_initial_date ,@current_point
END
CLOSE current_day
DEALLOCATE current_day