Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL CASE usage in functions

Can't we use CASE condition outside SQL SELECT statements?

E.g.:

CASE 
  WHEN old.applies_to = 'admin' THEN _applies_to = 'My Self'
  ELSE _applies_to = initcap(old.applies_to)
END
 _summary = _summary || '<li>Apply To: ' || _applies_to || '</li>';

I get the following error:

ERROR:  syntax error at or near "_summary"
LINE 86: _summary = _summary || '<li>Apply To: ' || _applies ...
like image 273
Yohan Hirimuthugoda Avatar asked Jan 19 '12 09:01

Yohan Hirimuthugoda


People also ask

What is $$ in PostgreSQL function?

It can be used to replace single quotes enclosing string literals (constants) anywhere in SQL scripts. The body of a function happens to be such a string literal. Dollar-quoting is a PostgreSQL-specific substitute for single quotes to avoid escaping of nested single quotes (recursively).

How does CASE statement work in PostgreSQL?

Introduction to PostgreSQL CASEEach condition is a boolean expression and based on its output the result is chosen. If all the expressions corresponding to WHEN are evaluated to be False , then the result respective to the ELSE part is shown. In case, you don't specify the ELSE part; the query will return null.

Can we use commit in function in PostgreSQL?

PostgreSQL commit is used to save the transaction changes to the database, which the user made. The default value of commit is ON in PostgreSQL, which means we need not have to execute a commit statement to save the transaction; it will automatically save the transaction into the database.

Can we use if condition in PostgreSQL query?

In PostgreSQL, the if-statement is one of the most valuable and frequently used control statements. The If statement executes only those expressions that satisfy the specified condition. If an expression doesn't satisfy the specified criteria, then the if-statement moves the control to the next statement.


1 Answers

This concerns the conditional control structure CASE of the procedural language PL/pgSQL, to be used in plpgsql functions or DO statements.
Not to be confused with the CASE expression of SQL. Different language! And subtly different syntax rules, too.

While SQL CASE can be embedded in SQL expressions inside PL/pgSQL code (which is mostly just glue for SQL commands), you cannot have stand-alone SQL CASE expressions (would be nonsense).

-- inside plpgsql code block: CASE     WHEN old.applies_to = 'admin' THEN       _applies_to := 'My Self';    ELSE       _applies_to := initcap(old.applies_to); END CASE; 

You have to use fully qualified statements, terminated with semicolon (;) and END CASE to close it.

Answer to additional question in comment

According to documentation the ELSE keyword of a CASE statement is not optional. I quote from the link above:

If no match is found, the ELSE statements are executed; but if ELSE is not present, then a CASE_NOT_FOUND exception is raised.

However, you can use an empty ELSE:

CASE     WHEN old.applies_to = 'admin' THEN       _applies_to := 'My Self';    ELSE       --  do nothing END CASE; 

This is different from SQL CASE expressions where ELSE is optional, but if the keyword is present, an expression has to be given, too!

like image 161
Erwin Brandstetter Avatar answered Oct 06 '22 06:10

Erwin Brandstetter