SSIS Foreach through a table, insert into another and delete the source row

前端 未结 2 423
猫巷女王i
猫巷女王i 2020-12-21 13:08

I have an SSIS routine that reads from a very dynamic table and inserts whichever rows it finds into a table in a different database, before truncating the original source t

2条回答
  •  星月不相逢
    2020-12-21 13:52

    A option, that might sound stupid but it works, is to delete first and use the OUTPUT clause.

    control flow setup

    I created a simple control flow that populates a table for me.

    IF EXISTS
    (
        SELECT 1 FROM sys.tables AS T WHERE T.name = 'DeleteFirst'
    )
    BEGIN
        DROP TABLE dbo.DeleteFirst;
    END
    
    CREATE TABLE dbo.DeleteFirst
    (
        [name] sysname
    );
    
    INSERT INTO
        dbo.DeleteFirst
    SELECT
        V.name
    FROM
        master.dbo.spt_values V
    WHERE
        V.name IS NOT NULL;
    

    dataflow

    In my OLE DB Source, instead of using a SELECT, DELETE the data you want to go down the pipeline and OUTPUT the DELETED virtual table. Somethinng like

    DELETE
        DF
    OUTPUT
        DELETED.*
    FROM
        dbo.DeleteFirst AS DF;
    

    results

    It works, it works!

提交回复
热议问题