# wrangler d1 execute failed: near syntax error

## TL;DR

The SQL sent to D1 has a syntax error near the reported token. D1 speaks SQLite dialect: check for MySQL/Postgres-isms (backticks are fine, but LIMIT syntax, types, and functions differ). Fix the statement and re-run.

```text
wrangler d1 execute failed: near "syntax error"
```

## Steps

1. Read the error's 'near' token - the problem is at or just before it.
   Expected: The offending spot is located.

2. Check for dialect mismatches: SQLite does not have some MySQL/Postgres functions or types.
   Expected: The non-SQLite construct is found.

3. Test the statement locally with sqlite3 before sending it to D1.
   Expected: It runs clean locally.

4. Re-run wrangler d1 execute with the fixed SQL.
   Expected: It succeeds.

5. Keep migrations in files and test them against local SQLite in CI.
   Expected: Syntax errors are caught pre-deploy.

## When to use

- d1 execute fails with near syntax error
- migrating SQL from another database
- hand-written migrations

## When not to use

- the error is a constraint violation (different problem)
- the SQL runs locally (check what wrangler actually sent)
- using the D1 API directly (same SQL rules apply)

## Tool compatibility

- wrangler
- Cloudflare D1 (SQLite)

## Variant phrasings

- "d1 execute near syntax error"
- "wrangler d1 sql syntax error"
- "d1 migration syntax error"

## Why it happens

D1 is SQLite, and SQL written for other databases often uses functions, types, or syntax SQLite rejects. The 'near' pointer is precise; the fix is translating the statement into SQLite dialect. Testing against real sqlite3 locally catches it before the round trip.

## Edge cases

- D1 has its own limits (statement size, bound parameters); syntax is only the first gate
- --local vs remote D1 can differ in version; test against the right one
- batch statements split on semicolons; one bad statement fails the batch

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_8Qo81nuAWIIhC3h2CwtmFg
