• Ok, If you state there are no duplicates in the DAT_STAGE then thats fine, I'm always suspicious of data loaded from files as they can easily have duplicates in them, and neither of the solutions below will prevent duplicates occuring if there are duplicated rows in the source file.

    There are a couple of options, using the merge statement is one,

    MERGE INTO DAT_DES as Target

    USING DAT_STAGE as Source

    ON Target.ds_nme=Source.ds_nme

    and Targer.lvl_nme = LTRIM(REPLACE(Source.lvl_nme, 'D', ''))

    When NOT MATCHED BY TARGET THEN

    INSERT (lvl_nme, ds_nme, ds_size, create_dt, rating_dt)

    VALUES LTRIM(REPLACE(Source.lvl_nme, 'D', '')), Source.ds_nme, Source.ds_size, Source.create_dt, substring(Source.ds_nme, 3, 8));

    The other is to use a NOT EXISTS, Something like

    insert into DAT_DES

    (lvl_nme, ds_nme, ds_size, create_dt, rating_dt)

    SELECT LTRIM(REPLACE(DAT_STAGE.lvl_nme, 'D', '')), ds_nme, ds_size, create_dt, substring(ds_nme, 3, 8)

    from DAT_STAGE s

    WHERE NOT EXISTS

    (SELECT 1

    FROM DAT_DES d

    WHERE d.ds_nme=s.ds_nme

    AND d.lvl_nme=LTRIM(REPLACE(s.lvl_nme, 'D', '')))

    I'm not sure how this will perform as I've seen strange query performances when modifying data in a NOT EXISTS where clause.

    _________________________________________________________________________
    SSC Guide to Posting and Best Practices