I have two columns (TSD_BALANCEUNITS and TSD_UNITSINCERT). TSD_BALANCEUNITS has numeric values and TSD_UNITSINCERT has 0.00
By using cursor I want to update values of TSD_BALANCEUNITS to TSD_UNITSINCERT until the total sum of values updated in from TSD_BALANCEUNITS to TSD_UNITSINCERT is equal to 199.00
and the values of TSD_BALANCEUNITS which is being updated becomes 0.00 and moved to TSD_UNITSINCERT when total sum of moved values is equal to 199.00
Below is the script that I am using ...
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
This command:
UPDATE IS_TRX_TRX_PROCESSED
SET TSD_UNITSINCERT = TSD_BALANCEUNITS
WHERE (TSD_ACCCODE = 8775)
AND TSD_BALANCEUNITS > 0
update all records where TSD_ACCCODE = 8775 and TSD_BALANCEUNITS > 0, it doesn't matter if its sum 999, you need to identify in some way wich records must be updated.
Second, according the result, this never happens. That means that IF (SUM(@CHECKFLAG) <= 199.00)
is never true. I suppose because you never initializes it value to 0.
SET @CHECKFLAG = 0;