FORMAT 'x12.x12'
COPY … FROM · import COPY … TO · exportX12 COPY. Complete X12 segment rows (read_x12 schema)
-- Export an X12 interchange's segment rows to a file through the X12 COPY format, in document and segment order.
COPY (SELECT * FROM x12.main.read_x12('ISA*00* *00* *ZZ*SENDER *ZZ*RECEIVER *240101*1200*^*00501*000000001*0*P*:~GS*XX*SEND*RECV*20240101*1200*1*X*005010~ST*997*0001~AK1*PO*1~AK9*A*1*1*1~SE*4*0001~GE*1*1~IEA*1*000000001~') ORDER BY document_index, segment_index) TO 'acknowledgment.x12' (FORMAT 'x12.x12');
Options
Pass these inside the COPY statement's parentheses, after FORMAT. Unknown options are rejected.
- recalculate_countsBOOLEAN
- Recompute SE01, GE01, and IEA01 after edits. Defaults to false (validate existing counts). Never changes control numbers or synthesizes missing envelopes.
Full reference
X12 COPY format
The registered handler is invoked by DuckDB's COPY machinery. It cannot be called directly as a table function; name it as FORMAT '<alias>.x12' ('x12.x12' when attached as x12).
Import. Create the destination table from the schema of read_x12(input) with zero rows, then import a worker-local file or glob. Column names, order, and types must match. Reads retain malformed segments for inspection and materialize the input in memory.
Export. Supply the four named columns segment_id, elements, element_separator, and segment_terminator, ordered by document and segment index. Other columns are informational. ISA11 and ISA16 preserve repetition and component delimiters. Empty element strings are retained; NULL values are rejected.
Validation and publication. The writer checks ASCII text, fixed-width ISA, envelope nesting, counts, and control matches across batches. The explicit recalculate_counts option updates SE01/GE01/IEA01 only. A same-directory temporary file replaces the target after validation completes; failure preserves the old target. Empty input writes an empty file. This is structural validation; transaction-specific content rules remain outside scope. Formatting whitespace is normalized. EDIFACT and binary payloads are unsupported. See the repository README for the complete edit-and-export workflow.