# build_delta_stage2_sql

> **Function** in `langgraph.checkpoint.sqlite`

📖 [View in docs](https://reference.langchain.com/python/langgraph.checkpoint.sqlite/_delta/build_delta_stage2_sql)

Stage-2 per-channel UNION ALL fetching writes from `writes`.

One branch per channel with a non-empty chain. Each branch inlines its
own `IN (?, ?, ...)` placeholder list because sqlite has no array-bind
equivalent of postgres's `= ANY(%s)`. Caller passes parameters in
matching order: `[thread_id, checkpoint_ns, channel, *chain_cids]` per
branch.

Returns an empty string when no channel has a chain (caller skips
executing in that case). Per-channel UNION ALL avoids the over-fetch
of a single `channel = ANY(channels)` filter when channels have
different chain depths — same rationale as postgres.

## Signature

```python
build_delta_stage2_sql(
    *,
    chain_lens: Sequence[int],
) -> str
```

---

[View source on GitHub](https://github.com/langchain-ai/langgraph/blob/6f83cc9dc2a24c7884ae7da20c9f39f469be35d7/libs/checkpoint-sqlite/langgraph/checkpoint/sqlite/_delta.py#L42)