# Databricks - insert\_overwrite with SQLWarehouses

**URL:** <https://discourse.getdbt.com/t/databricks-insert-overwrite-with-sqlwarehouses/9111>\
**Category:** Help\
**Tags:** databricks\
**Created:** [July 12, 2023, 3:09pm UTC](https://discourse.getdbt.com/t/databricks-insert-overwrite-with-sqlwarehouses/9111 "2023-07-12T15:09:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![renan.costa](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/renan.costa/32/2597_2.png) [@renan.costa](https://discourse.getdbt.com/u/renan.costa)\
**Post date:** [July 12, 2023, 3:09pm UTC](https://discourse.getdbt.com/t/databricks-insert-overwrite-with-sqlwarehouses/9111/1 "2023-07-12T15:09:05Z")

</div>

## The problem I’m having

Hello,

I have an incremental model being implemented with Databricks, the data should be replaced using a column which is not unique.

I know the `insert_overwrite` strategy with `partition_by` should work, but I’m using Databricks SQLWarehouses, which doesn’t support `insert_overwrite`.

I also tried to use `replace_where`. [This strategy](https://docs.databricks.com/sql/language-manual/sql-ref-syntax-dml-insert-into.html#syntax) replaces the data based on a boolean condition; however, in our use case, we would need to define the condition based on a subquery, which is not possible:

```auto
INSERT INTO <target_table>
REPLACE WHERE key IN (select key from <new_data>) // <- not possible to use a subquery
select * from <new_data>;

```

To expand on the use case, let’s assume a table and the `date` column being used to update the table, the current state of the table is:

```auto
| date | value |
|--------------|-------|
| "2023-01-01" | a |
| "2023-01-02" | b1 |
| "2023-01-02" | b2 |

```

and the new data is:

```auto
| date | value |
|--------------|-------|
| "2023-01-02" | b3 |

```

The final result should be:

```auto
| date | value |
|--------------|-------|
| "2023-01-01" | a |
| "2023-01-02" | b3 |

```

* * *

Does anyone have any other ideas of what I could use to implement this model?

Thank you

---

<div class="post-metadata">

**Author:** ![rvp13](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rvp13](https://discourse.getdbt.com/u/rvp13)\
**Post date:** [July 12, 2023, 5:09pm UTC](https://discourse.getdbt.com/t/databricks-insert-overwrite-with-sqlwarehouses/9111/2 "2023-07-12T17:09:45Z")

</div>

Hi within databricks you can see if your use case can utilize the MERGE INTO statement

> **[Upsert into a Delta Lake table using merge](https://docs.databricks.com/delta/merge.html)**
>
> Upsert data into a Delta Lake table using merge on Databricks.

---

<div class="post-metadata">

**Author:** ![renan.costa](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/renan.costa/32/2597_2.png) [@renan.costa](https://discourse.getdbt.com/u/renan.costa)\
**Post date:** [July 13, 2023, 3:59pm UTC](https://discourse.getdbt.com/t/databricks-insert-overwrite-with-sqlwarehouses/9111/3 "2023-07-13T15:59:16Z")

</div>

> [@rvp13](#):
>
> Hi within databricks you can see if your use case can utilize the MERGE INTO stat

Hi @rvp13 unfortunately it doesn’t work for this use case because the column used to update the table is not unique.

---

<div class="post-metadata">

**Author:** ![brunoszdl](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/brunoszdl/32/4113_2.png) [@brunoszdl](https://discourse.getdbt.com/u/brunoszdl)\
**Post date:** [July 14, 2023, 3:14am UTC](https://discourse.getdbt.com/t/databricks-insert-overwrite-with-sqlwarehouses/9111/4 "2023-07-14T03:14:13Z")

</div>

Even if you user MERGE INTO ON FALSE?

---

<div class="post-metadata">

**Author:** ![renan.costa](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/renan.costa/32/2597_2.png) [@renan.costa](https://discourse.getdbt.com/u/renan.costa)\
**Post date:** [July 25, 2023, 9:26am UTC](https://discourse.getdbt.com/t/databricks-insert-overwrite-with-sqlwarehouses/9111/5 "2023-07-25T09:26:04Z")

</div>

Hi @brunoszdl, the `merge into` won’t work because the unique\_key is not unique in this case.

---

<div class="post-metadata">

**Author:** ![hendrikfrentrup](https://avatars.discourse-cdn.com/v4/letter/h/51bf81/32.png) [@hendrikfrentrup](https://discourse.getdbt.com/u/hendrikfrentrup)\
**Post date:** [May 29, 2025, 1:47am UTC](https://discourse.getdbt.com/t/databricks-insert-overwrite-with-sqlwarehouses/9111/6 "2025-05-29T01:47:20Z")

</div>

@renan.costa the `REPLACE WHERE` key being dynamic is in deed tricky, especially because dbt generally creates a temporary view for the \<new\_data\> part.

How about passing in the predicates dynamically via CLI? Would that work in your (admittedly quite old) case? I’ve just tested the following and it works really well with a SQL datawarehouse:

```auto
{{ config(
   materialized='incremental',
   incremental_strategy='replace_where',
   incremental_predicates=["date in (" ~ var('date', '1999-12-31') ~ ")"],
   partition_by=['date']
) }}

select * from {{ ref('table_upstream') }}
where date in ({{ var("date", "1999-12-31") }})

```

and run it like this:  
`dbt run --vars "{\"date\": \"'2023-01-03', '2023-01-02'\"}"`

which compiles to

```auto
insert into `table_downstream`
  replace where
    date in ('2023-01-03', '2023-01-02')
  table `table_downstream__dbt_tmp`

```

So I think the `replace_where` strategy is the right one to take there.
