Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Peter Petruš 9 posts 113 karma points
    Aug 19, 2021 @ 09:08
    Peter Petruš
    0

    Delete Umbraco Form Record from Umbraco 8 with SQL Procedure / Query

    Hi,

    I would like to ask you, if anybody solve the problem to delete Records from Umbraco Forms (v 8.7.6) in SQL stored procedure?

    I found that there was solution for Forms in Umbraco 6: http://siempresolutions.co.uk/blog/DeletingOldUmbracoFormSubmissions

    but it is not working for newest Umbraco 8.

    It is falling with this error: Msg 547, Level 16, State 0, Procedure DeleteOldContourFormRecords, Line 48 [Batch Start Line 2] The DELETE statement conflicted with the REFERENCE constraint "FKUFRecordFieldsUFRecords_Record". The conflict occurred in database "Web8Umb", table "dbo.UFRecordFields", column 'Record'.

    According to my findings, this falls on the last part of "finally the record", where it tries to delete older records. However, when I looked at Dependencies of UFRecodFiels table, it looks like I have all the tables mapped.

    Any idea?

    Thanks, Peter

  • Peter Petruš 9 posts 113 karma points
    Aug 19, 2021 @ 12:47
    Peter Petruš
    100

    SOLVED

    The problem is with constraint and DELETE_RULE at table [UFRecordFields], which is CASCADE - set on 1.

    For me is the best way uncheck constrains at table [UFRecordFields] additing command to procedure / SQL command. It is like this: ALTER TABLE dbo.[UFRecordFields] NOCHECK CONSTRAINT ALL

    When procedure finished, it will set to default setting.

  • Andy Finn 51 posts 183 karma points
    Aug 22, 2021 @ 13:45
    Andy Finn
    0

    @peter , i am also seeing the same issue , any chance of more details what you did to resolve this ? or full sql stored proc ? did u modify the oldsubmissions proc ? we had issue with orginal proc orphaning UFRecordFields , so did u have the same issues . Andy

    ps. if you want to check for orphaned files

     select * from UFRecordFields where Record not in (select id from UFRecords)
    

    Just in case your getting them too.

  • Peter Petruš 9 posts 113 karma points
    Aug 23, 2021 @ 08:17
    Peter Petruš
    2

    I just updated procedure from this example: http://siempresolutions.co.uk/blog/DeletingOldUmbracoFormSubmissions

     CREATE PROCEDURE dbo.Umbraco8Db
        @Days int = 2000
    AS
    
    BEGIN
        DECLARE @DeleteDate AS DateTime
        SET @DeleteDate = DATEADD(DAY, -@Days, GETDATE())
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;
        ALTER TABLE dbo.[UFRecordFields] NOCHECK CONSTRAINT ALL
        ---hack for umbraco8 forms, different DELETE_RULE as in umbraco6 forms
        print 'start';
        -- databit
        DELETE db
        FROM dbo.[UFRecords] AS r 
        INNER JOIN dbo.[UFRecordFields]     AS rf ON r.[Id] = rf.[Record]
        INNER JOIN dbo.[UFRecordDataBit] AS db ON rf.[Key] = db.[Key]
        WHERE r.[Updated] < @DeleteDate;
            print 'databite';
        -- datetime
        DELETE dt
        FROM dbo.[UFRecords] AS r 
        INNER JOIN dbo.[UFRecordFields]     AS rf ON r.[Id] = rf.[Record]
        INNER JOIN dbo.[UFRecordDataDateTime] AS dt ON rf.[Key] = dt.[Key]
        WHERE r.[Updated] < @DeleteDate;
        print 'datetime';
        -- integer
        DELETE di
        FROM dbo.[UFRecords] AS r 
        INNER JOIN dbo.[UFRecordFields]     AS rf ON r.[Id] = rf.[Record]
        INNER JOIN dbo.[UFRecordDataInteger] AS di ON rf.[Key] = di.[Key]
        WHERE r.[Updated] < @DeleteDate;
        print 'integer';
        -- longstring
        DELETE dls
        FROM dbo.[UFRecords] AS r 
        INNER JOIN dbo.[UFRecordFields]     AS rf ON r.[Id] = rf.[Record]
        INNER JOIN dbo.[UFRecordDataLongString] AS dls ON rf.[Key] = dls.[Key]
        WHERE r.[Updated] < @DeleteDate;
        print 'longstring';
        -- string
        DELETE ds
        FROM dbo.[UFRecords] AS r 
       INNER JOIN dbo.[UFRecordFields]     AS rf ON r.[Id] = rf.[Record]
        INNER JOIN dbo.[UFRecordDataString] AS ds ON rf.[Key] = ds.[Key]
        WHERE r.[Updated] < @DeleteDate;
        print 'string';
        -- finally the record
        DELETE r    
        FROM dbo.[UFRecords] AS r 
        WHERE r.[Updated] < @DeleteDate;
        print 'final';
    END
    GO
    
  • Andy Finn 51 posts 183 karma points
    Aug 23, 2021 @ 08:35
    Andy Finn
    0

    Thanks @peter

  • Peter Petruš 9 posts 113 karma points
    Dec 14, 2023 @ 12:19
    Peter Petruš
    0

    Update for Umbraco 12

    USE [dbname]
    GO
    /****** Object:  StoredProcedure [dbo].[procedurename]    Script Date: 2023-12-14 13:10:33 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE [dbo].[procedurename]
        @Days int = 180
    AS
    --EXEC sp_fkeys @pktable_name = 'UFRecordFields', @pktable_owner = 'dbo' 
    --show all dependencies
    BEGIN
        DECLARE @DeleteDate AS DateTime
        SET @DeleteDate = DATEADD(DAY, -@Days, GETDATE())
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;
        ALTER TABLE dbo.[UFRecordFields] NOCHECK CONSTRAINT ALL
        ALTER TABLE dbo.[UFRecordAudit] NOCHECK CONSTRAINT ALL
        ---hack for umbraco8 forms, different DELETE_RULE as in umbraco6 forms
        print 'start';
        -- SELECT @DeleteDate; 
        -- databit
        DELETE db
        FROM dbo.[UFRecords] AS r 
        INNER JOIN dbo.[UFRecordFields]     AS rf ON r.[Id] = rf.[Record]
        INNER JOIN dbo.[UFRecordDataBit] AS db ON rf.[Key] = db.[Key]
        WHERE r.[Updated] < @DeleteDate;
            print 'databite';
        -- datetime
        DELETE dt
        FROM dbo.[UFRecords] AS r 
        INNER JOIN dbo.[UFRecordFields]     AS rf ON r.[Id] = rf.[Record]
        INNER JOIN dbo.[UFRecordDataDateTime] AS dt ON rf.[Key] = dt.[Key]
        WHERE r.[Updated] < @DeleteDate;
        print 'datetime';
        -- integer
        DELETE di
        FROM dbo.[UFRecords] AS r 
        INNER JOIN dbo.[UFRecordFields]     AS rf ON r.[Id] = rf.[Record]
        INNER JOIN dbo.[UFRecordDataInteger] AS di ON rf.[Key] = di.[Key]
        WHERE r.[Updated] < @DeleteDate;
        print 'integer';
        -- longstring
        DELETE dls
        FROM dbo.[UFRecords] AS r 
        INNER JOIN dbo.[UFRecordFields]     AS rf ON r.[Id] = rf.[Record]
        INNER JOIN dbo.[UFRecordDataLongString] AS dls ON rf.[Key] = dls.[Key]
        WHERE r.[Updated] < @DeleteDate;
        print 'longstring';
        -- string
        DELETE ds
        FROM dbo.[UFRecords] AS r 
       INNER JOIN dbo.[UFRecordFields]     AS rf ON r.[Id] = rf.[Record]
        INNER JOIN dbo.[UFRecordDataString] AS ds ON rf.[Key] = ds.[Key]
        WHERE r.[Updated] < @DeleteDate;
        print 'string';
        -- finally the record
        DELETE r    
        FROM dbo.[UFRecords] AS r 
        WHERE r.[Updated] < @DeleteDate;
        print 'final';
    END
    

    Add ALTER TABLE dbo.[UFRecordAudit] NOCHECK CONSTRAINT ALL to solve problem with conflict like this.

    The DELETE statement conflicted with the REFERENCE constraint "FKUFRecordAuditUFRecords_Id". The conflict occurred in database "B365WebUmbProd", table "dbo.UFRecordAudit", column 'Record'.

Please Sign in or register to post replies

Write your reply to:

Draft