Informix Error -374
-374 Can only use column number in ORDER BY clause with UNION.
This query has both a UNION clause and an ORDER BY clause. In a union query, in which multiple SELECT statements exist and the names of the selected columns in each statement are not necessarily the same, you cannot use column names or expressions in the ORDER BY clause.
Instead, you must use column position numbers, with 1 representing the first selected column, 2 representing the second, and so on. Rewrite the query to use only numbers in the ORDER BY clause.
Oninit® Troubleshooting Guidance
Reasons / Common Causes
-374 is a UNION-specific ORDER BY restriction: once multiple SELECT statements are combined
with UNION, the combined result no longer has a single, unambiguous set of column names or
expressions to sort by — since each branch could in principle use different column names or
expressions for the same output position — so ORDER BY must use positional column numbers
instead.
- An
ORDER BYclause using a column name or expression on aUNIONquery — the direct cause. - A query originally written as a single
SELECT(whereORDER BY column_nameworked fine), later extended into aUNION, without updating theORDER BYclause to use position numbers. - Copy-pasted
ORDER BYsyntax from a non-UNIONquery applied to aUNIONquery without adjustment.
Solutions / Resolution
- Rewrite the
ORDER BYclause to use column position numbers (1 for the first column, 2 for the second, and so on), per the official guidance, instead of column names or expressions. - When adding a
UNIONto a previously single-SELECTquery, review and convert itsORDER BYclause to positional form as part of the same change.
Examples
Column name in ORDER BY on a UNION query
SELECT customer_id, name FROM current_customers
UNION
SELECT customer_id, name FROM archived_customers
ORDER BY name;
-- -374: name isn't allowed here; use a position number
Fix — use the column's position instead:
SELECT customer_id, name FROM current_customers
UNION
SELECT customer_id, name FROM archived_customers
ORDER BY 2;
Diagnostic Checks
- Check whether the query uses
UNION(orINTERSECT/MINUS) — this restriction is specific to combined queries. - Check the
ORDER BYclause for column names or expressions rather than plain position numbers.
Related Errors / Related Topics
- -309 — "ORDER BY column or expression must be in SELECT list." A related
ORDER BYrestriction, in this case forDISTINCT/UNIQUEqueries rather thanUNION. - -308 — "The statement failed because corresponding column data types must be compatible for
each UNION, INTERSECT, or MINUS query." Another
UNION-specific restriction in the same general combined-query family.
ORDER BY on a UNION query must always use column position numbers — column names and
expressions aren't accepted there, even if they'd work fine in a plain single-SELECT query.