Skip to content

feat(merge): MSSQL OUTPUT clause on MERGE / INSERT / UPDATE / DELETE - #157

Merged
productdevbook merged 1 commit into
mainfrom
feat/mssql-output-merge
May 19, 2026
Merged

feat(merge): MSSQL OUTPUT clause on MERGE / INSERT / UPDATE / DELETE#157
productdevbook merged 1 commit into
mainfrom
feat/mssql-output-merge

Conversation

@productdevbook

Copy link
Copy Markdown
Owner

Summary

  • Wires up SQL Server's OUTPUT clause through the same MergeNode.returning slot the PG path uses (PR feat(merge): RETURNING on MERGE (PG 17+) #148 set up RETURNING on MERGE for PG 17+; MSSQL previously threw via MERGE_RETURNING feature flag). The MSSQL printer now rewrites the tail to OUTPUT <projections> instead.
  • Adds mergeActionMssql() builder helper for SQL Server's $action pseudo-column — kept distinct from PG's existing mergeAction() (which compiles to MERGE_ACTION()) because the two emit different SQL shapes (function call vs. pseudo-column). Documented the dialect-divergence in JSDoc on both helpers.
  • MERGE_RETURNING feature flag now includes mssql; the existing INSERT / UPDATE / DELETE OUTPUT paths got a small extension to respect explicit INSERTED / DELETED table qualifiers on column refs (so UPDATE OUTPUT INSERTED.id, DELETED.id works without the auto-prefix double-wrapping the user's choice).

Pseudo-table conventions on MERGE

  • Bare col("id")INSERTED.[id] (post-action row — the common case).
  • Explicit col("email", "DELETED")[DELETED].[email] (pre-action row).
  • star()INSERTED.*; pass star("DELETED") for the deleted set.
  • mergeActionMssql() → bare $action token (no parens — it's a pseudo-column).

Implementation notes

  • The $action token can't be a function call (validateFunctionName rejects $ prefixes) and a raw node would be flagged by the findRawNodes audit. We use a sentinel function name (__SUMAK_MSSQL_ACTION__) that the MSSQL printer recognizes and rewrites to $action; on other dialects the sentinel falls through and the engine errors at parse pointing at the offending call site (the desired "wrong but visible" behavior).
  • The MSSQL printer's printMerge re-uses the base printer's MERGE skeleton (target / source / ON / WHENs / CTEs) and only adds the OUTPUT <cols> tail — no duplication of MERGE-statement logic.
  • Cut: OUTPUT … INTO @var (writing to a table variable) — sumak doesn't model variable declarations.

Test plan

  • MERGE … OUTPUT $action, INSERTED.[id] for the printer surface (both untyped and typed builder).
  • PG path unchanged: RETURNING MERGE_ACTION(), "id", "name" still emits the same way; existing PGlite roundtrip passes.
  • INSERT / UPDATE / DELETE OUTPUT paths cover the mixed INSERTED + DELETED qualifier case (UPDATE projecting both pre- and post-image).
  • pnpm fmt && pnpm lint && pnpm typecheck && pnpm vitest run all green (2076 tests pass, no new lint warnings).

🤖 Generated with Claude Code

Wires up SQL Server's OUTPUT clause through the same MergeNode.returning
slot the PG path uses, so the typed MERGE builder works on MSSQL without
a separate surface. Bare column refs default to INSERTED.<col> (the
post-action row); pass an explicit col("name", "DELETED") for the
pre-action row. Adds mergeActionMssql() helper for $action — kept
distinct from PG's mergeAction() because the two compile to different
syntax (function call vs. pseudo-column).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@productdevbook
productdevbook merged commit 5133078 into main May 19, 2026
1 check passed
@productdevbook
productdevbook deleted the feat/mssql-output-merge branch May 19, 2026 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant