In this syntax, the case statement evaluates the boolean expressions sequentially from top to bottom until it finds an expression that evaluates to true The following syntax shows syntax of the searched case statement: case when boolean-expression -1 thenĮnd case Code language: PostgreSQL SQL dialect and PL/pgSQL ( pgsql ) The following flowchart illustrates the simple case statement in this example: In case the price is not 0.99, 2.99 or 4.99, the case statement assigns the film the price segment as unspecified. Based on the rental rate, it assigns a price segment to the film that can be mass, mainstream, or high end. This example first selects the film with id 100. Output: NOTICE: High End Code language: HTTP ( http ) assign the price segment if found then case rateĮnd $$ Code language: PostgreSQL SQL dialect and PL/pgSQL ( pgsql ) do $$ declareīegin - get the rental rate select rental_rate into rate The following is an example of the simple case statement. If the result of the search-expression does not match expression in the when sections and the else section does not exist, the case statement will raise a case_not_found exception. If the case statement cannot find any match, it will execute the else section. Also, it stops comparing the result of the search-expression with the remaining expressions. If the case statement finds a match, it will execute the corresponding when section. The case statement compares the result of the search-expression with the expression in each when branch using equal operator ( =) from top to bottom. The search-expression is an expression that evaluates to a result. When expression_1 then when-statementsĮND case Code language: PostgreSQL SQL dialect and PL/pgSQL ( pgsql ) Let’s start with the syntax of the simple case statement: case search-expression The case expression evaluates to a value while the case statement selects a section to execute based on condition. Notice that you should not confuse about the case statement and case expression. The case statement selects a when section to execute from a list of when sections based on a condition. Summary: in this tutorial, you will learn about the PL/pgSQL case that executes statements based on a certain condition.īesides the if statement, PostgreSQL provides you with case statements that allow you to execute a block of code based on a condition.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |