# Resolving Snowflake MERGE Issues for Versioned AppsFlyer Data with dbt Incremental Models

**URL:** <https://discourse.getdbt.com/t/resolving-snowflake-merge-issues-for-versioned-appsflyer-data-with-dbt-incremental-models/17601>\
**Category:** Help\
**Tags:** incremental, snowflake, dbt-cloud\
**Created:** [January 3, 2025, 3:15pm UTC](https://discourse.getdbt.com/t/resolving-snowflake-merge-issues-for-versioned-appsflyer-data-with-dbt-incremental-models/17601 "2025-01-03T15:15:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hediketari](https://avatars.discourse-cdn.com/v4/letter/h/59ef9b/32.png) [@hediketari](https://discourse.getdbt.com/u/hediketari)\
**Post date:** [January 3, 2025, 3:15pm UTC](https://discourse.getdbt.com/t/resolving-snowflake-merge-issues-for-versioned-appsflyer-data-with-dbt-incremental-models/17601/1 "2025-01-03T15:15:50Z")

</div>

## The problem I’m having

I’m trying to load versioned data from AppsFlyer into Snowflake using an incremental dbt model that leverages a MERGE statement. However, dbt keeps generating a statement like:

create or replace temporary view tilt.bronze.raw\_af\_ssot\_user\_acquisition\_\_dbt\_tmp  
as (  
merge into tilt.bronze.raw\_af\_ssot\_user\_acquisition …  
);

Snowflake doesn’t allow MERGE statements inside a CREATE VIEW (temporary or otherwise), so I get a syntax error:  
SQL compilation error:  
syntax error line 7 at position 0 unexpected ‘merge’.  
syntax error line 12 at position 28 unexpected ‘over’.

## The context of why I’m trying to do this

| | • | AppsFlyer Data: AppsFlyer provides “versioned” reports, and I only want the latest version for each install date. |
| --- | --- | --- |
| | • | Snowflake + dbt: I use dbt for transformations and want an incremental model that merges new data into a final table while discarding older versions. |
| | • | Versioned Ingestion: By capturing the version from AppsFlyer’s S3 path, I can sort out the most recent rows for each install date. A MERGE fits this logic perfectly in Snowflake. |

## What I’ve already tried

| | 1. | Incremental Strategy: |
| --- | --- | --- |
| | • | I set {{ config(materialized=‘incremental’, incremental\_strategy=‘merge’, unique\_key=‘install\_date’) }} in my dbt model. |
| | • | Ensured the model’s logic uses WHEN MATCHED THEN UPDATE and WHEN NOT MATCHED THEN INSERT. |
| | 2. | Schema and Permissions: |
| | • | Verified my dbt role has CREATE TABLE, USAGE, and SELECT on tilt.bronze. |
| | • | Confirmed the model was set to produce a table in tilt.bronze schema. |
| | 3. | Searching for Tests or References: |
| | • | Looked for any tests, ephemeral models, or macros referencing raw\_af\_ssot\_user\_acquisition. |
| | • | Ran dbt ls --select raw\_af\_ssot\_user\_acquisition+, which shows no direct downstream models. |
| | • | Double-checked .yml files to ensure no store\_failures: true or hidden references. |
| | 4. | Trying Standalone Runs: |
| | • | Ran dbt run -m raw\_af\_ssot\_user\_acquisition directly (no success). |
| | • | Confirmed that the “create or replace temporary view” snippet is still produced, implying something is wrapping my MERGE statement. |
| | 5. | Reviewing dbt/Environment Versions: |
| | • | I’m on a custom dbt build (e.g., dbt\_version: 2024.xx.xx+), so I can’t easily see if older or newer standard dbt versions would behave differently. |

---

<div class="post-metadata">

**Author:** ![a\_slack\_user](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/a_slack_user/32/1629_2.png) [@a\_slack\_user](https://discourse.getdbt.com/u/a_slack_user)\
**Post date:** [January 6, 2025, 11:04am UTC](https://discourse.getdbt.com/t/resolving-snowflake-merge-issues-for-versioned-appsflyer-data-with-dbt-incremental-models/17601/2 "2025-01-06T11:04:25Z")

</div>

It sound like you literally wrote `merge into…` in your dbt model. Is that correct? If so, you don’t write the merge yourself in dbt. dbt will write it for you (at a later step - after it creates that temporary view).

Note: `@Deena Bernett` originally [posted this reply in Slack](https://getdbt.slack.com/archives/CBSQTAPLG/p1736161277606839?thread_ts=1735917375.253659&cid=CBSQTAPLG). It might not have transferred perfectly.
