BEGIN TRANSACTION
DECLARE @TBL TABLE(TRANS_ID INT, STOCK_LOC_ID INT);
INSERT INTO @TBL (TRANS_ID, STOCK_LOC_ID)
SELECT dbo.TRANS.TRANS_ID, dbo.TRANS_CONT.STOCK_LOC
FROM dbo.TRANS INNER JOIN
dbo.TRANS_CONT ON dbo.TRANS.TRANS_ID = dbo.TRANS_CONT.TRANS_ID INNER JOIN
dbo.REGISTER ON dbo.TRANS_CONT.ITEM_ID = dbo.REGISTER.REG_ID INNER JOIN
dbo.CODES ON dbo.TRANS_CONT.STOCK_LOC = dbo.CODES.CODE_ID
WHERE (dbo.TRANS.TRANS_TYPE = 2 OR dbo.TRANS.TRANS_TYPE = 22 OR dbo.TRANS.TRANS_TYPE = 46) AND (dbo.REGISTER.REG_CODE <> 'LINE DISCOUNT') AND (dbo.REGISTER.REG_CODE <> '0000') AND 
(dbo.REGISTER.REG_CODE <> 'INTEREST') AND (dbo.REGISTER.REG_CODE <> '*') AND (dbo.REGISTER.REG_CODE <> 'TRANSPORT') AND (dbo.REGISTER.REG_CODE <> 'SHIPPING CHARGES') 
AND (dbo.REGISTER.REG_CODE <> 'SERVICE CHARGES') AND (dbo.REGISTER.REG_CODE <> 'CREDIT CARD PROCESSING FEE')
GROUP BY dbo.TRANS.TRANS_ID, dbo.TRANS_CONT.STOCK_LOC, dbo.CODES.CODE_1, dbo.TRANS.TRANS_NO
WHILE((SELECT COUNT(*) FROM @TBL) > 0)
BEGIN
DECLARE @TMP_TRANS_ID INT; SET @TMP_TRANS_ID = 0;
DECLARE @TMP_STORE_ID INT; SET @TMP_STORE_ID = 0;
SELECT TOP 1 @TMP_TRANS_ID = TRANS_ID, @TMP_STORE_ID = STOCK_LOC_ID FROM @TBL;
UPDATE TRANS SET STORE_LOC = @TMP_STORE_ID WHERE TRANS_ID = @TMP_TRANS_ID;
DELETE FROM @TBL WHERE TRANS_ID = @TMP_TRANS_ID;
END
COMMIT