I am currently making use of the code below:
ALTER PROCEDURE [dbo].[sp_TidyBasket]
@ReferenceNumber VARCHAR
AS
BEGIN
BEGIN TRANSACTION
DECLARE
@GUID VARCHAR,
@ErrorCode INT
--Get the First Reference Number of a basket item being duplicated
SET @GUID = (SELECT TOP 1 MIN(idx6)
FROM iwfAccountOpening
WHERE Idx29 = @ReferenceNumber
GROUP BY Idx37
HAVING COUNT(*) > 1)
--Executes a while loop whilst there is duplicates to be removed
WHILE (@GUID IS NOT NULL)
BEGIN
DELETE FROM iwfAccountOpening WHERE Idx6 = @GUID;
END
--Rollbacks transactions when any errors occur
SELECT @ErrorCode = @@ERROR
IF (@ErrorCode <> 0) GOTO PROBLEM
COMMIT TRANSACTION
PROBLEM:
IF (@ErrorCode <> 0) BEGIN
ROLLBACK TRANSACTION
END
END
I want it to loop through and delete duplicates (whilst retaining one row of the duplicate data) according to a defined value passed through to it.
Currently this is not showing any changes on my database table. I understand it would be due to the @GUID
value I am setting but I have no idea how to approach this otherwise.
This is not a duplicate question of the previously mentioned duplicate question as it focuses' on the deletion of constraints placed by the user WITH duplicates.