Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This concerns the <a href="http://www.postgresql.org/docs/current/interactive/plpgsql-control-structures.html#PLPGSQL-CONDITIONALS" rel="nofollow noreferrer">conditional control structure <code>CASE</code></a> of the <a href="https://www.postgresql.org/docs/current/static/plpgsql.html" rel="nofollow noreferrer">procedural language PL/pgSQL</a>, to be used in <a href="https://www.postgresql.org/docs/current/static/sql-createfunction.html" rel="nofollow noreferrer">plpgsql functions</a> or <a href="http://www.postgresql.org/docs/current/interactive/sql-do.html" rel="nofollow noreferrer"><code>DO</code> statements</a>. Not to be confused with the <a href="http://www.postgresql.org/docs/current/interactive/functions-conditional.html#FUNCTIONS-CASE" rel="nofollow noreferrer"><code>CASE</code> expression of SQL</a>. Different language! And subtly different syntax rules, too. </p> <p>While SQL <code>CASE</code> can be embedded in SQL expressions inside PL/pgSQL code (which is mostly just glue for SQL commands), you cannot have stand-alone SQL <code>CASE</code> expressions (would be nonsense).</p> <pre><code>-- inside plpgsql code block: CASE WHEN old.applies_to = 'admin' THEN _applies_to := 'My Self'; ELSE _applies_to := initcap(old.applies_to); END CASE; </code></pre> <p>You have to use fully qualified statements, terminated with semicolon (<code>;</code>) and <code>END CASE</code> to close it.</p> <h3>Answer to <a href="https://stackoverflow.com/questions/8924035/postgresql-case-usage-in-functions/8924119#comment11167507_8924119">additional question in comment</a></h3> <p>According to documentation the <code>ELSE</code> keyword of a <code>CASE</code> statement is <strong>not</strong> optional. I quote from the link above:</p> <blockquote> <p>If no match is found, the <code>ELSE</code> statements are executed; but if <code>ELSE</code> is not present, then a <code>CASE_NOT_FOUND</code> exception is raised.</p> </blockquote> <p>However, you can use an empty <code>ELSE</code>:</p> <pre><code>CASE WHEN old.applies_to = 'admin' THEN _applies_to := 'My Self'; ELSE -- do nothing END CASE; </code></pre> <p>This is different from SQL <code>CASE</code> expressions where <code>ELSE</code> is optional, but if the keyword is present, an expression has to be given, too!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload