Skip to main content
Skip table of contents

Riverbird Cleanup Script for a c-entron database

(As of: April 2026)

Re:

  • ⚙️ Administration

Note: This applies only if you are a customer of the Third-party provider Riverbird RMM are or were.

Purpose of the script

This script clears (TRUNCATE) all Riverbird/RMM-related tables based on their name prefixes. The tables themselves remain intact.

exception: This table is not cleared because it contains the RMM references for contract billing.

CODE
AssetManagementArticleAssignment

Requirements

  • News Backup The c-entron database is available.

  • The Riverbird and c-entron databases are already separate (see Separate databases).

  • Version with sufficient permissions (at least db_owner (in the c-entron database).

Test run vs. production run

By default, the script runs as Test run in a single transaction and executes it at the end ROLLBACK off—so no changes are written. In SQL Server Management Studio, you can PRINT-Check the entries in the "Messages" tab to see which tables and foreign keys would be affected.

For the Live run Swap the last two lines at the end of the script:

SQL
COMMIT TRANSACTION;
-- ROLLBACK TRANSACTION;

Script

SQL
/*  ═══════════════════════════════════════════════════════════════════════
    Riverbird Cleanup-Skript für eine c-entron Datenbank
    ───────────────────────────────────────────────────────────────────────
    Leert (TRUNCATE) alle Riverbird/RMM-bezogenen Tabellen anhand ihrer
    Namens-Präfixe. Die Tabellen selbst bleiben erhalten.

    AUSNAHME (wird NICHT geleert):
      AssetManagementArticleAssignment
        → enthält RMM-Referenzen für die Vertragsabrechnung.

    VORAUSSETZUNG:
      • Backup der c-entron-Datenbank vorhanden
      • Riverbird- und c-entron-Datenbank sind bereits getrennt

    TESTLAUF / ECHTLAUF:
      Standardmäßig läuft das Skript als TESTLAUF (ROLLBACK am Ende).
      Für den ECHTLAUF die letzten beiden Zeilen tauschen:
          COMMIT TRANSACTION;
          -- ROLLBACK TRANSACTION;
    ═══════════════════════════════════════════════════════════════════════ */

SET NOCOUNT ON;
SET XACT_ABORT ON;

BEGIN TRANSACTION;

BEGIN TRY

    DECLARE @table          NVARCHAR(256);
    DECLARE @schema         NVARCHAR(128);
    DECLARE @parentTable    NVARCHAR(256);
    DECLARE @parentSchema   NVARCHAR(128);
    DECLARE @foreignKey     NVARCHAR(256);
    DECLARE @sql            NVARCHAR(MAX);

    /* -------------------------------------------------------------------
       Phase 1: Relevante Tabellen anhand der Präfixe ermitteln
       ------------------------------------------------------------------- */
    PRINT 'Phase 1: Suche nach Tabellen mit den definierten Präfixen ...';

    DECLARE @tableList TABLE
    (
        schemaName  NVARCHAR(128),
        tableName   NVARCHAR(256)
    );

    INSERT INTO @tableList (schemaName, tableName)
    SELECT  s.name, t.name
    FROM    sys.tables  AS t
    JOIN    sys.schemas AS s ON s.schema_id = t.schema_id
    WHERE
    (
            t.name LIKE 'AssetManagement%'
        OR  t.name LIKE 'DocumentationWizard%'
        OR  t.name LIKE 'DocuWizard%'
        OR  t.name LIKE 'Mon%'
        OR  t.name LIKE 'Deployable%'
        OR  t.name LIKE 'Remote%'
        OR  t.name LIKE 'RBSync%'
        OR  t.name  =  'DocumentationPageOldVersions'
        OR  t.name LIKE 'PatchManagementDeployable%'
        OR  t.name LIKE 'RiverbirdAgentDeployment%'
    )
    AND     t.name <> 'AssetManagementArticleAssignment';

    /* -------------------------------------------------------------------
       Phase 2: Foreign Keys löschen
       Es werden BEIDE Richtungen berücksichtigt:
         a) FKs, die VON den Tabellen ausgehen
         b) FKs anderer Tabellen, die AUF die Tabellen verweisen
            (sonst schlägt TRUNCATE mit Fehler 4712 fehl)
       ------------------------------------------------------------------- */
    PRINT 'Phase 2: Lösche Foreign Keys (eingehend und ausgehend) ...';

    DECLARE table_cursor CURSOR LOCAL FAST_FORWARD FOR
        SELECT schemaName, tableName FROM @tableList;

    OPEN table_cursor;
    FETCH NEXT FROM table_cursor INTO @schema, @table;

    WHILE @@FETCH_STATUS = 0
    BEGIN
        DECLARE foreignKey_cursor CURSOR LOCAL FAST_FORWARD FOR
            SELECT
                fk.name,
                OBJECT_SCHEMA_NAME(fk.parent_object_id),
                OBJECT_NAME(fk.parent_object_id)
            FROM sys.foreign_keys AS fk
            WHERE   OBJECT_NAME(fk.parent_object_id)     = @table   -- ausgehend
                OR  OBJECT_NAME(fk.referenced_object_id) = @table;  -- eingehend

        OPEN foreignKey_cursor;
        FETCH NEXT FROM foreignKey_cursor
            INTO @foreignKey, @parentSchema, @parentTable;

        WHILE @@FETCH_STATUS = 0
        BEGIN
            SET @sql =
                N'ALTER TABLE '   + QUOTENAME(@parentSchema)
                + N'.'            + QUOTENAME(@parentTable)
                + N' DROP CONSTRAINT ' + QUOTENAME(@foreignKey) + N';';

            PRINT 'Executing: ' + @sql;
            EXEC sp_executesql @sql;

            FETCH NEXT FROM foreignKey_cursor
                INTO @foreignKey, @parentSchema, @parentTable;
        END;

        CLOSE foreignKey_cursor;
        DEALLOCATE foreignKey_cursor;

        FETCH NEXT FROM table_cursor INTO @schema, @table;
    END;

    CLOSE table_cursor;
    DEALLOCATE table_cursor;

    /* -------------------------------------------------------------------
       Phase 3: Tabellen leeren (TRUNCATE TABLE)
       ------------------------------------------------------------------- */
    PRINT 'Phase 3: Leere Tabellen ...';

    DECLARE truncate_cursor CURSOR LOCAL FAST_FORWARD FOR
        SELECT schemaName, tableName
        FROM   @tableList
        ORDER BY tableName;

    OPEN truncate_cursor;
    FETCH NEXT FROM truncate_cursor INTO @schema, @table;

    WHILE @@FETCH_STATUS = 0
    BEGIN
        SET @sql =
            N'TRUNCATE TABLE ' + QUOTENAME(@schema)
            + N'.' + QUOTENAME(@table) + N';';

        PRINT 'Executing: ' + @sql;
        EXEC sp_executesql @sql;

        FETCH NEXT FROM truncate_cursor INTO @schema, @table;
    END;

    CLOSE truncate_cursor;
    DEALLOCATE truncate_cursor;

    PRINT 'Operation erfolgreich abgeschlossen.';

END TRY
BEGIN CATCH
    PRINT 'FEHLER aufgetreten – Transaktion wird zurückgerollt.';
    PRINT ERROR_MESSAGE();

    IF CURSOR_STATUS('local','foreignKey_cursor') >= 0
    BEGIN
        CLOSE foreignKey_cursor;
        DEALLOCATE foreignKey_cursor;
    END;
    IF CURSOR_STATUS('local','table_cursor') >= 0
    BEGIN
        CLOSE table_cursor;
        DEALLOCATE table_cursor;
    END;
    IF CURSOR_STATUS('local','truncate_cursor') >= 0
    BEGIN
        CLOSE truncate_cursor;
        DEALLOCATE truncate_cursor;
    END;

    IF @@TRANCOUNT > 0
        ROLLBACK TRANSACTION;
    RETURN;
END CATCH;

/*  ═══════════════════════════════════════════════════════════════════════
    Für den ECHTLAUF die nächsten beiden Zeilen tauschen
    (COMMIT aktiv, ROLLBACK auskommentiert):
    ═══════════════════════════════════════════════════════════════════════ */

-- COMMIT TRANSACTION;
ROLLBACK TRANSACTION;

Keywords (internal): Database cleanup, cleanup, database separation, separate database

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.