我有兩列(TSD_BALANCEUNITS和TSD_UNITSINCERT)。 TSD_BALANCEUNITS有數值和TSD_UNITSINCERT有0.00通過SQL光標更新和計算值
通過使用光標我想的TSD_BALANCEUNITS值更新到TSD_UNITSINCERT直到TSD_BALANCEUNITS更新值來TSD_UNITSINCERT總和等於到199.00
和的值正在更新的TSD_BALANCEUNITS變爲0.00並且移動到TSD_UNITSINCERT當移動值的總和等於199.00
下面是我使用的腳本...
DECLARE @cnt INT = 0;
DECLARE @TSD_BALANCEUNITS AS DECIMAL(25, 4)
DECLARE @TSD_UNITSINCERT AS DECIMAL(25, 4)
DECLARE @CHECKFLAG AS DECIMAL(25, 4)
DECLARE LOCK_CERT_UNITS CURSOR LOCAL STATIC
FOR
SELECT TSD_BALANCEUNITS
,TSD_UNITSINCERT
FROM IS_TRX_TRX_PROCESSED
WHERE (TSD_ACCCODE = 8775)
AND TSD_BALANCEUNITS > 0
ORDER BY TSD_REALIZEDDATE ASC
OPEN LOCK_CERT_UNITS
FETCH NEXT
FROM LOCK_CERT_UNITS
INTO @TSD_BALANCEUNITS
,@TSD_UNITSINCERT
WHILE @@FETCH_STATUS = 0 --AND @cnt < 199.00
BEGIN
SET @TSD_UNITSINCERT = @TSD_BALANCEUNITS
SET @CHECKFLAG = + @TSD_BALANCEUNITS
IF (SUM(@CHECKFLAG) <= 199.00)
BEGIN
UPDATE IS_TRX_TRX_PROCESSED
SET TSD_UNITSINCERT = TSD_BALANCEUNITS
WHERE (TSD_ACCCODE = 8775)
AND TSD_BALANCEUNITS > 0
END
PRINT 'BALANCE UNIT =' + CONVERT(VARCHAR(50), @TSD_BALANCEUNITS) + ' ==> ' + 'CERT IN UNITS =' + CONVERT(VARCHAR(50), @TSD_UNITSINCERT);
FETCH NEXT
FROM LOCK_CERT_UNITS
INTO @TSD_BALANCEUNITS
,@TSD_UNITSINCERT
END
CLOSE LOCK_CERT_UNITS
DEALLOCATE LOCK_CERT_UNITS
GO
能否請您把輸入數據的SQL查詢工作 –
您還沒有添加表結構請加所有相關信息,使一些可以看看在問題上。 –
兩列均爲十進制(24,8) –