Applies to: a server-side SQL statement; the error text and the function syntax in the thread are PostgreSQL's, no version is named.
Problem
A conditional UPDATE repeated the same two sub-selects inside two CASE expressions. Shortened with with v1 as (select ...), v2 as (select ...) update ... set ... case when v1=2 and v2=2 ..., it failed with ERROR: column "v1" does not exist.
Solution
- A WITH name is a table-like result set, not a scalar value;
v1=2treats it as a column. A reply doubted the syntax could work at all, since there is no way to reference v2 that way. - Advice from the thread, in the order given: first write a SELECT that returns the records and fields to update; split the work into two or more simpler UPDATEs; or store the CASE logic as data (a table of criteria and result values) and join it to the table.
- The original poster, who also asked elsewhere, finished with a server function whose first CTE computes the flag per row (the two sub-selects combined into one boolean column), and a second CTE that computes the amounts from it.
- A reply suggested a cross join to the one cost row needed, with an ORDER BY when LIMIT is used, so the same row is always returned.
Caveats
- In one UPDATE,
set a = x, b = a * ycomputes b from the ORIGINAL value of a, not the new one. Use two updates or repeat the calculation. - Reply examples (a MERGE) were written for Oracle; the poster of that said MS SQL cannot work with tuples that way.
- The final function was posted only in part; the thread does not show it running.
Today (2026)
Likely unchanged, as this is general SQL behaviour; not re-tested.
Sources: Jose V. (Aug 2013, the question), Geoff B., Sean H., Dennis E., Jon W. (Aug 2013) in softvelocity.clarion.databasedrivers.