# difficulty in insert+delete

**URL:** https://discourse.getdbt.com/t/difficulty-in-insert-delete/9067
**Category:** Help
**Created:** [July 10, 2023, 7:02am UTC](https://discourse.getdbt.com/t/difficulty-in-insert-delete/9067 "2023-07-10T07:02:49Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![unknownfakir1](https://avatars.discourse-cdn.com/v4/letter/u/a4c791/32.png) [@unknownfakir1](https://discourse.getdbt.com/u/unknownfakir1)
#### Post date: [July 10, 2023, 7:02am UTC](https://discourse.getdbt.com/t/difficulty-in-insert-delete/9067/1 "2023-07-10T07:02:49Z")

</div>

– macros/get\_incremental\_strategies.sql

{% macro insert\_delete\_strategy(source\_table, destination\_table, unique\_key\_columns) %}

{% if is\_incremental() %}  
– Delete outdated rows from the destination table  
DELETE FROM {{ ref(‘destination\_details’) }}  
WHERE EXISTS (  
SELECT 1  
FROM {{ ref(‘source\_details’) }}  
WHERE {{ ref(‘destination\_details’) }}.{{ unique\_key\_columns }} = {{ ref(‘source\_details’) }}.{{ unique\_key\_columns }}  
);

```
-- Insert new rows into the destination table
INSERT INTO {{ref('destination_details') }}
SELECT *
FROM {{ ref('source_details') }}
WHERE NOT EXISTS (
    SELECT 1
    FROM {{ ref('destination_details') }}
    WHERE {{ ref('destination_details') }}.{{ unique_key_columns }} = {{ ref('source_details') }}.{{ unique_key_columns }}
);

```

{% else %}  
– Full load from the source table to the destination table  
TRUNCATE TABLE {{ ref(‘destination\_details’) }};  
INSERT INTO {{ ref(‘destination\_details’) }}  
SELECT \*  
FROM {{ ref(‘source\_details’)}};  
{% endif %}

{% endmacro %}  
this insert\_delete\_strategy in my macros

This is dstinsedel.sql under models folder  
– depends\_on: {{ ref(‘destination\_details’) }}  
{{ config(materialized=‘incremental’, unique\_key=“Id”, incremental\_strategy=‘delete+insert’) }}

{{insert\_delete\_strategy(‘source\_details’, ‘destination\_details’, ‘Id’) }}

 ![image](https://us1.discourse-cdn.com/flex020/uploads/getdbt/original/2X/2/29b243ffff49abecff5b9f878c6dbb7ac34165b2.png)  
error getting like this  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/getdbt/original/2X/e/ecb5a6f1778a2f4a8e5860f9427be760f1d4ce02.png)
